gpt4 book ai didi

python - 互斥参数提示 Action 索引

转载 作者:行者123 更新时间:2023-11-28 21:15:16 25 4
gpt4 key购买 nike

我正在尝试对参数进行分组,以便用户可以执行以下任一操作:

python sample.py scan -a 1 -b 2
or
python sample.pt save -d /tmp -n something

这是我的代码:

import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='this is the description'
)
parser.add_argument('op', choices=['scan','save'], help='operation', default='scan')
root_group = parser.add_mutually_exclusive_group()

group1 = root_group.add_argument_group('g1', 'scan')
group1.add_argument('-a', help='dir1')
group1.add_argument('-b', help='dir2')

group2 = root_group.add_argument_group('g2', 'save')
group2.add_argument('-d', help='dir')
group2.add_argument('-n', help='name')

args = parser.parse_args()
print args

当我运行 python sample.py --help

我遇到了一个错误。有人可以告诉我如何解决吗?

Traceback (most recent call last):
File "sample.py", line 18, in <module>
args = parser.parse_args()
File "C:\Python27\lib\argparse.py", line 1688, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "C:\Python27\lib\argparse.py", line 1720, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "C:\Python27\lib\argparse.py", line 1926, in _parse_known_args
start_index = consume_optional(start_index)
File "C:\Python27\lib\argparse.py", line 1866, in consume_optional
take_action(action, args, option_string)
File "C:\Python27\lib\argparse.py", line 1794, in take_action
action(self, namespace, argument_values, option_string)
File "C:\Python27\lib\argparse.py", line 994, in __call__
parser.print_help()
File "C:\Python27\lib\argparse.py", line 2313, in print_help
self._print_message(self.format_help(), file)
File "C:\Python27\lib\argparse.py", line 2287, in format_help
return formatter.format_help()
File "C:\Python27\lib\argparse.py", line 279, in format_help
help = self._root_section.format_help()
File "C:\Python27\lib\argparse.py", line 209, in format_help
func(*args)
File "C:\Python27\lib\argparse.py", line 317, in _format_usage
action_usage = format(optionals + positionals, groups)
File "C:\Python27\lib\argparse.py", line 388, in _format_actions_usage
start = actions.index(group._group_actions[0])
IndexError: list index out of range

如果我添加 action='store_const',错误就会消失,并出现一个新的错误,要求 4 个输入。

最佳答案

Argparse 似乎并不完全支持将一个组添加到另一个组中。发生此错误是因为 Argparse 要求 root_group 执行某种操作。解决方法是向组中添加一个参数:

import argparse

if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='this is the description'
)

# This is now redundant. We can remove it
# parser.add_argument('op', choices=['scan','save'], help='operation', default='scan')
root_group = parser.add_mutually_exclusive_group()

# Workaround
root_group.add_argument('--scan', help='scan', action='store_true')
root_group.add_argument('--save', help='save', action='store_true')

group1 = root_group.add_argument_group('g1', 'scan')
group2 = root_group.add_argument_group('g2', 'save')

group1.add_argument('-a', help='dir1')
group1.add_argument('-b', help='dir2')

group2.add_argument('-d', help='dir', default='')
group2.add_argument('-n', help='name')

args = parser.parse_args()
print args

请注意,我们正在使用 --scan--save。要避免使用 -- 前缀,您可能需要 Sub-commands 的帮助。详情可查here .

关于python - 互斥参数提示 Action 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30499648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com