我想使用argparse构建一个带子命令的工具.可能的语法可能是
/tool.py下载 - 从1234 - 间隔60
/tool.py下载 - build 1432
/tool.py clean - 数字10
所以我想用argparse来实现:
但我没有找到一种方法将' - from'和' - internal'与一个组配对,然后使该组与'--build'互斥.
下面是我当前的代码,它只使' - from'和'--build'是互斥的.既不确保' - from'和'--interval'汇集在一起,也不确保' - interval'和'--build'是互斥的.
parser = argparse.ArgumentParser(description='A Tool')
subparsers = parser.add_subparsers(help='sub-command help')
#create the parser for the 'download' command
download_parser = subparsers.add_parser('download', help='download help')
download_parser.add_argument('--interval', dest='interval', type=int,help='interval help')
group = download_parser.add_mutually_exclusive_group()
group.add_argument('--from',type=int, help='from help')
group.add_argument('--build', type=int, help='interval help')
Run Code Online (Sandbox Code Playgroud)
例如,
/tool.py下载 - 从1234
不应该被允许,因为' - from'必须使用'--interval'.但我的代码默默接受它.
和
/tool.py下载--interval 1234 --build …
为什么argparse不会解析这些参数?
--foo 1 2 3 bar
Run Code Online (Sandbox Code Playgroud)
运用
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs='+')
parser.add_argument('bar')
Run Code Online (Sandbox Code Playgroud)
这给出了以下错误:
错误:参数太少
如果我先通过bar参数,它可以工作:
bar --foo 1 2 3
Run Code Online (Sandbox Code Playgroud)
现在,这本身并不算太糟糕.我可以忍受首先使用位置参数,这只是因为这种行为与argparse为我们创建的帮助不一致,它指出bar应该是最后一个:
用法:argparsetest.py [-h] [ - foo FOO [FOO ...]]栏
那么如何使用一致的帮助文本来完成这项工作呢?
这是一个完整的测试程序.
我有一个有很多可用选项的程序.例如,用于更改设置的配置选项.
./app config -h
Run Code Online (Sandbox Code Playgroud)
使用普通的argparse子命令给我帮助
现在我想在config子命令中添加另一个子命令,名为list以列出配置值
./app config list
Run Code Online (Sandbox Code Playgroud)
此外,该命令应该接受另一个选项,以便我可以说
./app config list CATEGORY
Run Code Online (Sandbox Code Playgroud)
仅列出一个类别的配置
我现在的代码基本上就是这个命令
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title='subcommands',
... description='valid subcommands',
... help='additional help')
>>> subparsers.add_parser('foo')
>>> subparsers.add_parser('bar')
>>> parser.parse_args(['-h'])
usage: [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
valid subcommands
{foo,bar} additional help
Run Code Online (Sandbox Code Playgroud)
到目前为止,我找不到在子命令中使用子命令的任何方法.如果这是可能的,怎么样?如果没有,还有其他方法可以实现这一目标吗?
提前致谢
我正在尝试使用以下代码向我的脚本添加命令行选项:
import argparse
parser = argparse.ArgumentParser('My program')
parser.add_argument('-x', '--one')
parser.add_argument('-y', '--two')
parser.add_argument('-z', '--three')
args = vars(parser.parse_args())
foo = args['one']
bar = args['two']
cheese = args['three']
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
另外,我如何从IDLE shell运行它?我使用命令'python myprogram.py -x foo -y bar -z cheese',它给我一个语法错误
我有一个参数是一个1-100的整数的程序,我只是不喜欢它在使用argparse时显示在-h帮助消息中的方式(它字面上列出0,1,2,3,4 ,5,......等)
有没有办法改变这个或用另一种方式代表它?
谢谢
编辑:
以下是那些询问的人的代码:
norse = parser.add_argument_group('Norse')
norse.add_argument('-n', '--norse', required=False, help='Run the Norse IPViking scan.', action='store_true')
norse.add_argument('--threshold', required=False, type=int, choices=range(0,101), help='Threshold (0-100) denoting at what threat level to provide additional data on an IP \
address. Default is 49.', default=49)
Run Code Online (Sandbox Code Playgroud) 我试图以call_command一种非常类似于这个问题的方式使用Django 而没有答案.
我称之为的方式是:
args = []
kwargs = {
'solr_url': 'http://127.0.0.1:8983/solr/collection1',
'type': 'opinions',
'update': True,
'everything': True,
'do_commit': True,
'traceback': True,
}
call_command('cl_update_index', **kwargs)
Run Code Online (Sandbox Code Playgroud)
理论上,根据文档,这应该有效.但它不起作用,它只是不起作用.
这是add_arguments我的Command类的方法:
def add_arguments(self, parser):
parser.add_argument(
'--type',
type=valid_obj_type,
required=True,
help='Because the Solr indexes are loosely bound to the database, '
'commands require that the correct model is provided in this '
'argument. Current choices are "audio" or "opinions".'
)
parser.add_argument(
'--solr-url',
required=True,
type=str,
help='When swapping cores, it …Run Code Online (Sandbox Code Playgroud) 是否有可能访问传递给参数的选择元组?如果是这样,我该怎么做呢
例如,如果我有
parser = argparse.ArgumentParser(description='choose location')
parser.add_argument(
"--location",
choices=('here', 'there', 'anywhere')
)
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
我可以访问元组('here', 'there', 'anywhere')吗?
我目前正在构建一个cli工具,并使用该模块遇到了一些令人头疼的功能argparse(至少对我来说令人头疼)。我进行了快速google搜索,但找不到任何答案——尽管我的搜索术语很可能是不正确的。
以下屏幕截图说明了该行为:
情况是这样的:
.parse_args()当传递into的结果时vars(),位置参数中的破折号会被保留(如预期),但对于可选参数来说,情况似乎并非如此,即,对于可选参数,破折号会转换为下划线。
我在这里缺少什么?我希望 python 导出可以解释为什么存在这种行为..提前致谢。
m
忍者编辑:
提交这篇文章后,我想到这可能与--用于声明可选参数的前缀有关。但我仍然想知道实际的推理。
编辑:
有人向我指出,这与此无关,vars()但问题仍然存在:为什么会出现这种行为?
python command-line arguments command-line-interface argparse
如何使用 指定最小或最大浮点参数argprase?我希望能够在最小和最大浮点值之间提供命令行参数。
我能找到的最接近的是 中的choices选项add_argument(),但它只指定了参数的允许值。
parser.add_argument("L", type=float, choices=range(2))
Run Code Online (Sandbox Code Playgroud)
0.5L的命令行参数失败:
invalid choice: 0.5 (choose from 0, 1)
Run Code Online (Sandbox Code Playgroud) I need an ArgumentParser, with a conflict handling scheme, that resolves some registered set of duplicate arguments, but raises on all other arguments.
My initial approach (see also the code example at the bottom) was to subclass ArgumentParser, add a _handle_conflict_custom method, and then instantiate the subclass with ArgumentParser(conflict_handler='custom'), thinking that the _get_handler method would pick it up.
This raises an error, because the ArgumentParser inherits from _ActionsContainer, …