我想将 python-argparse 与参数和位置参数一起使用。假设我在命令行上有我的脚本(这只是一个简单而愚蠢的例子),这是我目前的代码:
#!/usr/bin/env python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true')
subparsers = parser.add_subparsers(help='command', dest='command')
cmd1_parser = subparsers.add_parser('command1')
cmd1_parser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
print args
Run Code Online (Sandbox Code Playgroud)
现在我这样称呼这个脚本:
~ $ myscript --verbose command1 --verbose
Namespace(command='command1', verbose=True)
~ $ myscript command1 --verbose
Namespace(command='command1', verbose=True)
~ $ myscript --verbose command1
Namespace(command='command1', verbose=True)
Run Code Online (Sandbox Code Playgroud)
现在你可以看到我总是得到相同的命名空间对象,并且无法区分详细命令是常规参数还是子解析器参数。但我需要分别处理这些参数。什么是一种简单的方法(以最少的代码工作)来做到这一点?
编辑:
我在 Python stdlib 问题跟踪器中提交了一个问题:http : //bugs.python.org/issue15327
例如我有选项:
parser.add_argument('-b', action="store_true")
parser.add_argument('-c', action="store_true")
parser.add_argument('-d', action="store_true")
Run Code Online (Sandbox Code Playgroud)
我想用同义词选项“-a”启用所有这些。是否可以?
编码
from argparse import ArgumentParser
p = ArgumentParser(description = 'foo')
p.add_argument('-b', '--bar', help = 'a description')
p.parse_args()
Run Code Online (Sandbox Code Playgroud)
...导致输出:
$ python argparsetest.py -h
usage: argparsetest.py [-h] [-b BAR]
foo
optional arguments:
-h, --help show this help message and exit
-b BAR, --bar BAR a description
Run Code Online (Sandbox Code Playgroud)
我想要的是:
$ python argparsetest.py -h
foo
optional arguments:
-h, --help show this help message and exit
-b BAR, --bar BAR a description
Run Code Online (Sandbox Code Playgroud)
例如,在寻求帮助时没有使用信息。有没有办法做到这一点?
我试图在Python中使用argparse的fromfile-prefix-chars功能从文件加载我的所有命令行参数,但它一直在抱怨我没有指定一些参数.
代码:
import argparse
def go():
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument("--option1")
parser.add_argument("--option2", type=int, required=True)
args = parser.parse_args()
if __name__ == "__main__":
go()
Run Code Online (Sandbox Code Playgroud)
参数文件:
--option1 foo
--option2 1234
Run Code Online (Sandbox Code Playgroud)
命令行和输出:
$ python testargparse.py @testargs
usage: testargparse.py [-h] [--option1 OPTION1] --option2 OPTION2
testargparse.py: error: argument --option2 is required
Run Code Online (Sandbox Code Playgroud)
你可以看到我在文件中提供了必需的参数,但是argparse没有看到它.
我正在做这样的事情:
parser = argparse.ArgumentParser()
group1 = parser.add_argument_group('group1')
group1.add_argument('--test1', help="test1")
group2 = parser.add_argument_group('group2')
group2.add_argument('--test2', help="test2")
group1_args = group1.parse_args()
group2_args = group2.parse_args()
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
Traceback (most recent call last):
File "test.py", line 19, in <module>
group1_args = group1.parse_args()
AttributeError: '_ArgumentGroup' object has no attribute 'parse_args'
Run Code Online (Sandbox Code Playgroud)
有没有办法只获取一个参数组的参数?
我正在尝试在我的python应用程序中使用argparse模块.我的应用程序应该使用单个强制参数运行,不带任何前缀.我无法想出办法.
我想parser.add_argument(...)在我的代码中使用常量定义构建一个to map参数.
假设我有以下内容
import argparse
# Both are the same type
CONST_A = <something>
CONST_B = <otherthing>
parser = argparse.ArgumentParser()
parser.add_argument(...)
# I'd like the following to be true:
parser.parse_args("--foo A".split()).foo == CONST_A
parser.parse_args("--foo B".split()).foo == CONST_B
Run Code Online (Sandbox Code Playgroud)
我能代替...什么呢?
我能做的最好的const是:
import argparse
# Both are the same type
CONST_A = 10
CONST_B = 20
parser = argparse.ArgumentParser()
status_group = parser.add_mutually_exclusive_group(required=True)
status_group.add_argument("-a", const=CONST_A, action='store_const')
status_group.add_argument("-b", const=CONST_B, action='store_const')
# I'd like the following to be true:
print …Run Code Online (Sandbox Code Playgroud) 有没有办法指定Python的ArgumentParser两个可选标志是冲突的?
arg_parser.add_argument('-c', '--clean', action='store_true')
arg_parser.add_argument('-d', '--dirty', action='store_true')
Run Code Online (Sandbox Code Playgroud)
我希望用户能够既不指定那些,也不能指定一个.
没有其他条件,它是否可以实现?
为什么我不能拥有一个argparse与titleor 相互排斥的组description,以便它在--help消息下显示为一个单独的类别?
我有一个名称和描述的选项组:
import argparse
parser = argparse.ArgumentParser()
group = parser.add_argument_group(
'foo options', 'various (mutually exclusive) ways to do foo')
group.add_argument('--option_a', action='store_true', help='option a')
group.add_argument('--option_b', action='store_true', help='option b')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
产量--help:
usage: foo.py [-h] [--option_a] [--option_b]
optional arguments:
-h, --help show this help message and exit
foo options:
various (mutually exclusive) ways to do foo
--option_a option a
--option_b option b
Run Code Online (Sandbox Code Playgroud)
但我想让小组互相排斥:
import argparse
parser = argparse.ArgumentParser()
group = …Run Code Online (Sandbox Code Playgroud) 我有一个Python程序,它维护一个联系人列表,我希望它通过命令行支持以下选项:
我需要的是:
prog [--show xyz | --list | --add xyz --number 123 --email abcd@xyz.com ]
Run Code Online (Sandbox Code Playgroud)
我尝试使用subparsers实现它,如下所示:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser1 = subparsers.add_parser('1')
subparser1.add_argument('--show', type=str, help="Shows the contact based on the given name provided as argument")
subparser1.add_argument('--list', action='store_true', help= "Prints all the contacts")
subparser2 = subparsers.add_parser('2')
subparser2.add_argument('--add', type=str, help="Adds a new contact by this name",required=True)
subparser2.add_argument('--number', type=int, help="The Phone Number for the new contact",required=True)
subparser2.add_argument('--email', type=str, help="Email address …Run Code Online (Sandbox Code Playgroud)