我有如下要求:
./xyifier --prox --lport lport --rport rport
Run Code Online (Sandbox Code Playgroud)
对于参数prox,我使用action ='store_true'来检查它是否存在.我不要求任何论据.但是,如果设置了--prox,我也需要 rport和lport.有没有一种简单的方法可以使用argparse执行此操作而无需编写自定义条件编码.
更多代码:
non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy')
non_int.add_argument('--lport', type=int, help='Listen Port.')
non_int.add_argument('--rport', type=int, help='Proxy port.')
Run Code Online (Sandbox Code Playgroud)
bor*_*ing 94
不,argparse中没有任何选项可以制作相互包含的选项集.
处理这个问题最简单的方法是:
if args.prox and (args.lport is None or args.rport is None):
parser.error("--prox requires --lport and --rport.")
Run Code Online (Sandbox Code Playgroud)
Mir*_*ira 47
你在谈论有条件要求的论点.就像@borntyping说你可以检查错误并执行parser.error()
,或者你可以只应用与--prox
添加新参数时相关的要求.
您的示例的简单解决方案可能是:
non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy')
non_int.add_argument('--lport', required='--prox' in sys.argv, type=int)
non_int.add_argument('--rport', required='--prox' in sys.argv, type=int)
Run Code Online (Sandbox Code Playgroud)
这种方式required
接收True
或False
取决于用户是否使用--prox
.这也保证了-lport
和-rport
相互之间的独立行为.
如果存在,如何使用parser.parse_known_args()
method然后添加args --lport
和--rport
args --prox
。
# just add --prox arg now
non_int = argparse.ArgumentParser(description="stackoverflow question",
usage="%(prog)s [-h] [--prox --lport port --rport port]")
non_int.add_argument('--prox', action='store_true',
help='Flag to turn on proxy, requires additional args lport and rport')
opts, rem_args = non_int.parse_known_args()
if opts.prox:
non_int.add_argument('--lport', required=True, type=int, help='Listen Port.')
non_int.add_argument('--rport', required=True, type=int, help='Proxy port.')
# use options and namespace from first parsing
non_int.parse_args(rem_args, namespace = opts)
Run Code Online (Sandbox Code Playgroud)
还要记住,您可以提供opts
第一次解析后生成的名称空间,而第二次解析其余参数。这样,最后,在完成所有解析之后,您将拥有一个包含所有选项的名称空间。
缺点:
--prox
不存在,则命名空间中甚至不存在其他两个从属选项。尽管根据您的用例(如果--prox
不存在),其他选项发生的情况无关紧要。--lport
并且--rport
不显示在帮助消息中你没有设置lport
时使用吗?prox
如果不是,为什么不lport
和rport
的争论prox
?例如
parser.add_argument('--prox', nargs=2, type=int, help='Prox: listen and proxy ports')
Run Code Online (Sandbox Code Playgroud)
这可以节省您的用户输入.它和测试if args.prox is not None:
一样容易if args.prox:
.
归档时间: |
|
查看次数: |
33324 次 |
最近记录: |