带有nargs>的Python argparse类型和选择限制1个 [英] Python argparse type and choice restrictions with nargs > 1
本文介绍了带有nargs>的Python argparse类型和选择限制1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标题几乎说明了一切.如果我的nargs大于1,是否有任何方法可以对已解析的单个args设置限制(例如,选择/类型)?
The title pretty much says it all. If I have nargs greater than 1, is there any way I can set restrictions (such as choice/type) on the individual args parsed?
这是一些示例代码:
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--credits', nargs=2,
help='number of credits required for a subject')
对于-c参数,我需要指定一个主题以及需要多少学分.该科目应限于预定义的科目列表,所需的学分数应为浮动学分.
For the -c argument I need to specify a subject and how many credits are required. The subject should be limited to a predefined list of subjects, and the number of credits required should be a float.
我可能可以使用子解析器执行此操作,但是因为它已经是子命令的一部分,所以我真的不希望事情变得更复杂.
I could probably do this with a subparser, but as it is this is already part of a sub-command so I don't really want things to get any more complicated.
推荐答案
查看全文