python argparse:如何覆盖所有其他参数 - 就像大多数程序中的 --help 一样?
python argparse : how to override all other parameters - like --help does in most programs?
我们有一堆使用标准参数集的脚本。这些在所有脚本使用的 class 中定义。每个脚本都可以根据需要添加额外的参数。
我正在尝试添加一个新参数,该参数应该像 --help 一样工作,因为如果存在,所有其他参数都将被忽略。当给出新参数 (--doc_string) 时,我希望忽略所有其他参数,我将打印一个特定的字符串并退出。有没有办法用 argparse
来做到这一点?
我研究过这样使用群组:
parser = argparse.ArgumentParser(prog='PROG')
group1 = parser.add_argument_group('docs', 'doc printing')
group1.add_argument('--doc_string', action = 'store_true', help='print the doc string and exit')
group2 = parser.add_argument_group('group2', 'group2 description')
group2.add_argument('--bar', required=True, help='bar help')
group2.add_argument('--blah',required=True, help='url used to test.')
parser.parse_args()
但是当 运行 仅 --doc_string 时,我仍然收到消息 --bar is required
。排外团体也不对。
我也看过 subclassing argparse.RawDescriptionHelpFormatter
或 argparse.RawTextHelpFormatter
或 argparse.ArgumentDefaultsHelpFormatter
但无法掌握如何使用它们来完成什么我正在努力。
向解析器注册您自己的操作。然后你可以用 --help
、--version
等相同的方式来做,即通过指定操作。
import argparse
class DocstringAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print('see ya later alligator')
parser.exit()
parser = argparse.ArgumentParser(prog='PROG')
parser.register('action', 'docstring', DocstringAction)
group1 = parser.add_argument_group('docs', 'doc printing')
group1.add_argument('--doc_string', nargs=0, action='docstring', help='print the doc string and exit')
group2 = parser.add_argument_group('group2', 'group2 description')
group2.add_argument('--bar', required=True, help='bar help')
group2.add_argument('--blah',required=True, help='url used to test.')
parser.parse_args()
如果您不需要文档字符串操作的任何参数,那么您可以(应该)将 nargs=0
推入 DocstringAction.__init__
,这样就没有必要指定它了。
我们有一堆使用标准参数集的脚本。这些在所有脚本使用的 class 中定义。每个脚本都可以根据需要添加额外的参数。
我正在尝试添加一个新参数,该参数应该像 --help 一样工作,因为如果存在,所有其他参数都将被忽略。当给出新参数 (--doc_string) 时,我希望忽略所有其他参数,我将打印一个特定的字符串并退出。有没有办法用 argparse
来做到这一点?
我研究过这样使用群组:
parser = argparse.ArgumentParser(prog='PROG')
group1 = parser.add_argument_group('docs', 'doc printing')
group1.add_argument('--doc_string', action = 'store_true', help='print the doc string and exit')
group2 = parser.add_argument_group('group2', 'group2 description')
group2.add_argument('--bar', required=True, help='bar help')
group2.add_argument('--blah',required=True, help='url used to test.')
parser.parse_args()
但是当 运行 仅 --doc_string 时,我仍然收到消息 --bar is required
。排外团体也不对。
我也看过 subclassing argparse.RawDescriptionHelpFormatter
或 argparse.RawTextHelpFormatter
或 argparse.ArgumentDefaultsHelpFormatter
但无法掌握如何使用它们来完成什么我正在努力。
向解析器注册您自己的操作。然后你可以用 --help
、--version
等相同的方式来做,即通过指定操作。
import argparse
class DocstringAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print('see ya later alligator')
parser.exit()
parser = argparse.ArgumentParser(prog='PROG')
parser.register('action', 'docstring', DocstringAction)
group1 = parser.add_argument_group('docs', 'doc printing')
group1.add_argument('--doc_string', nargs=0, action='docstring', help='print the doc string and exit')
group2 = parser.add_argument_group('group2', 'group2 description')
group2.add_argument('--bar', required=True, help='bar help')
group2.add_argument('--blah',required=True, help='url used to test.')
parser.parse_args()
如果您不需要文档字符串操作的任何参数,那么您可以(应该)将 nargs=0
推入 DocstringAction.__init__
,这样就没有必要指定它了。