ARGPARSE添加选择参数并保存到变量(dest)

发布于 2025-01-20 21:09:46 字数 1218 浏览 2 评论 0原文

我正在尝试向我的代码添加参数,我可以使以下代码工作:


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2025-01-27 21:09:46

用:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('title', help='Top level arg')
sp = parser.add_subparsers(dest='cmd')
sp1 = sp.add_parser('subtitle')
sp1.add_argument('myvar', choices=['foo', 'bar'])
args = parser.parse_args()
print(args)

“标题”是一个位置参数,所需的

“ cmd”是子份子(也是一个位置,带有选择)

'myvar'是subtitle parser的位置参数,它具有两个选择。

sp1.add_argument('myvar', choices=['foo', 'bar'])

语法既适用于主解析器和一个地下阶程。

示例调用

最高级别的帮助:

2126:~/mypy$ python3 stack71851214.py -h
usage: stack71851214.py [-h] title {subtitle} ...

positional arguments:
  title       Top level arg
  {subtitle}

optional arguments:
  -h, --help  show this help message and exit

子命令帮助:

2126:~/mypy$ python3 stack71851214.py atitle subtitle -h
usage: stack71851214.py title subtitle [-h] {foo,bar}

positional arguments:
  {foo,bar}

optional arguments:
  -h, --help  show this help message and exit

带有正确选择的子命令:

2126:~/mypy$ python3 stack71851214.py atitle subtitle foo
Namespace(cmd='subtitle', myvar='foo', title='atitle')

错误选择的子命令:

2127:~/mypy$ python3 stack71851214.py atitle subtitle baz
usage: stack71851214.py title subtitle [-h] {foo,bar}
stack71851214.py title subtitle: error: argument myvar: invalid choice: 'baz' (choose from 'foo', 'bar')

编辑

我不明白您的:

parser.add_parser('title', help='Top level arg')

升高:

AttributeError: 'ArgumentParser' object has no attribute 'add_parser'

add_parser是由subparser创建的动作的一种方法= parser.add_subparsers。它不是解析器的方法。这实际上是您测试的内容,还是您在没有测试的情况下为问题写的东西?

With:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('title', help='Top level arg')
sp = parser.add_subparsers(dest='cmd')
sp1 = sp.add_parser('subtitle')
sp1.add_argument('myvar', choices=['foo', 'bar'])
args = parser.parse_args()
print(args)

'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:

sp1.add_argument('myvar', choices=['foo', 'bar'])

syntax applies to both a main parser and a subparser.

sample calls

top level help:

2126:~/mypy$ python3 stack71851214.py -h
usage: stack71851214.py [-h] title {subtitle} ...

positional arguments:
  title       Top level arg
  {subtitle}

optional arguments:
  -h, --help  show this help message and exit

subcommand help:

2126:~/mypy$ python3 stack71851214.py atitle subtitle -h
usage: stack71851214.py title subtitle [-h] {foo,bar}

positional arguments:
  {foo,bar}

optional arguments:
  -h, --help  show this help message and exit

subcommand with correct choice:

2126:~/mypy$ python3 stack71851214.py atitle subtitle foo
Namespace(cmd='subtitle', myvar='foo', title='atitle')

subcommand with wrong choice:

2127:~/mypy$ python3 stack71851214.py atitle subtitle baz
usage: stack71851214.py title subtitle [-h] {foo,bar}
stack71851214.py title subtitle: error: argument myvar: invalid choice: 'baz' (choose from 'foo', 'bar')

edit

I don't understand your:

parser.add_parser('title', help='Top level arg')

That raises:

AttributeError: 'ArgumentParser' object has no attribute 'add_parser'

add_parser is a method of an Action created by subparser = parser.add_subparsers. It is not a method of a parser. Is this actually what you tested, or is it something you wrote for the question without testing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文