有没有办法通过Flask RESTful的reqparse实现Python的argparse“互斥组”?

时间:2019-09-18 13:40:00

标签: python flask argparse flask-restful

我正在将CLI转换为微服务,但是我坚持如何实现argparse的仅允许使用reqparse进行索引,工作或历史记录的功能。

argparse:

parser = argparse.ArgumentParser(description='An example program')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-i', '--index', type=str)
group.add_argument('-w', '--work',  type=str)
group.add_argument('-h', '--history', action='store_true')

parser.add_argument('-t', '-time', type=int)

reqparse:

parser = reqparse.RequestParser(trim=True)
parser.add_argument('index', type=str)
parser.add_argument('work', type=str)
parser.add_argument('history', type=bool)
parser.add_argument('time', type=int)

好的要求:

{
    "index": "my_str",
    "time" : 10
}

时间不属于互斥组,因此可以将其与互斥组中的另一个参数一起传递。

错误请求:

{
   "index": "my_str",
   "work": "another_str",
   "time": 10
}

工作和索引均来自互斥组。我不希望发送此邮件。

0 个答案:

没有答案