[英]Argparse won't parse any arguments I pass in the command line
I'm trying to run my program in Terminal: 我试图在终端中运行程序:
py program.py -t
and it's not setting the boolean to False. 并且没有将布尔值设置为False。 From what I can see, all of my code looks correct.
从我所看到的,我所有的代码看起来都是正确的。
isTitle = True
a = argparse.ArgumentParser(prog='program.py', usage='%(prog) [options]', description='The fooiest of bar')
a.add_argument('-t', '--title', action='store_const', const=False, dest='isTitle', help='show title')
r = a.parse_args()
I have a statement after the parse_args that prints the isTitle boolean to verify it's been set, and it always returns True. 我在parse_args之后有一条语句,该语句打印isTitle布尔值以验证是否已设置它,并且它始终返回True。 Do you guys have any recommendations?
你们有什么建议吗? I can add more code/diagnostic statements if need be.
如果需要,我可以添加更多代码/诊断语句。 Thank you!
谢谢!
dest='isTitle'
doesn't mean that a variable named isTitle
will be changed, it means the value will be stored in r.isTitle
, ie in the object the parse_args
call returns. dest='isTitle'
并不意味着将更改名为isTitle
的变量 ,这意味着该值将存储在r.isTitle
,即parse_args
调用返回的对象中。 It would be madness for argparse to modify any variables in the global scope. argparse修改全局范围内的任何变量将是疯狂的。
What you want is simply: 您想要的只是:
a.add_argument('-t', '--title', action='store_true', dest='isTitle', help='show title')
r = a.parse_args()
print(r.isTitle)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.