ARGPARSE添加选择参数并保存到变量(dest)
我正在尝试向我的代码添加参数,我可以使以下代码工作:
import argparse
parser = argparse.ArgumentParser()
parser.add_parser('title', help='Top level arg')
subparser = parser.add_subparsers(dest='my_var') # so I can access it later
subcommand_parser = subparser.add_parser('subtitle', help='Another option help', choices=['foo', 'bar'])
因此,我可以按如下方式调用我的程序:
./main.py title subtitle foo
然后访问 if my_var == 'foo': ... 因为
问题是我想摆脱字幕/解析器并使用 ./main.py title foo 调用我的程序并将该值存储在一个效果很好的变量中,
import argparse
parser = argparse.ArgumentParser()
parser.add_parser('title', help='Top level arg')
parser.add_argument('subtitle', choices=['foo', 'bar'])
我可以使用 ./main.py title foo 调用(或栏)
但我失去了对变量args.my_var
的访问权限
我尝试将dest='my_var'
添加到add_parser,但出现意外的参数错误。我尝试将其添加到 add_argument
但我得到了 dest 为位置参数提供了两次
(我可能可以通过在之前添加 --
来解决这个问题争论,但随后我又回到了最初的问题)。
有没有办法使用 ./main.py title foo
调用我的程序并将其保存到 args.my_var
等变量中?
I am trying to add arguments to my code and I could make the following code work:
import argparse
parser = argparse.ArgumentParser()
parser.add_parser('title', help='Top level arg')
subparser = parser.add_subparsers(dest='my_var') # so I can access it later
subcommand_parser = subparser.add_parser('subtitle', help='Another option help', choices=['foo', 'bar'])
So with this I can call my program as follow:
./main.py title subtitle foo
and then access the value passed in if my_var == 'foo': ...
The issue is that I want to get rid of the subtitle/parser and just call my program using ./main.py title foo
AND store that value in a variable
import argparse
parser = argparse.ArgumentParser()
parser.add_parser('title', help='Top level arg')
parser.add_argument('subtitle', choices=['foo', 'bar'])
which works great because I can call using ./main.py title foo (or bar)
but I loose access to the variable args.my_var
I tried adding dest='my_var'
to add_parser but I get an unexpected argument error. I tried adding it to add_argument
but I got a dest supplied twice for positional argument
(I can probably get around this by adding --
prior to the argument, but then I'm back at my initial problem).
Is there a way to call my program using ./main.py title foo
and save it to a variable such as args.my_var
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用:
“标题”是一个位置参数,所需的
“ cmd”是子份子(也是一个位置,带有选择)
'myvar'是
subtitle
parser的位置参数,它具有两个选择。:
语法既适用于主解析器和一个地下阶程。
示例调用
最高级别的帮助:
子命令帮助:
带有正确选择的子命令:
错误选择的子命令:
编辑
我不明白您的:
升高:
add_parser
是由subparser创建的动作的一种方法= parser.add_subparsers
。它不是解析器
的方法。这实际上是您测试的内容,还是您在没有测试的情况下为问题写的东西?With:
'title' is a positional argument, required
'cmd' is the subparser (also a positional, with choices)
'myvar' is a positional argument of the
subtitle
parser, which has two choices.The:
syntax applies to both a main parser and a subparser.
sample calls
top level help:
subcommand help:
subcommand with correct choice:
subcommand with wrong choice:
edit
I don't understand your:
That raises:
add_parser
is a method of an Action created bysubparser = parser.add_subparsers
. It is not a method of aparser
. Is this actually what you tested, or is it something you wrote for the question without testing?