gpt4 book ai didi

python - argparse 如何存储所有添加的参数?有没有办法访问他们的名字和帮助字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:31 27 4
gpt4 key购买 nike

我正在尝试为一个大型程序生成一些外部文档,该程序有大量可选参数(经常扩展/更改)。我很好奇是否有一种方法可以从解析器对象访问参数,以便我可以看到传递给它的所有内容的所有名称、描述、帮助等。 parse_args() 函数删除所有附加信息并仅返回键/值对。

例如,如果我有以下代码:

import argparse

def main():
parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f','--foo', help='Foo help string')
parser.add_argument('-b','--bar', help='Bar help string')
parser.add_argument('-z','--zar', help='Zar help string')
args = parser.parse_args()

是否有某种方法可以获取解析器中所有参数的列表?类似于

[{'dest':'--f', 'help':'Foo help string'}, {'dest':'-b', 'help':etc...)]

如果我能得到类似的东西,那么标记一些漂亮的 html 文档就会变得轻而易举。

最佳答案

在内部,ArgumentParser 将这些内容存储在 _actions 属性中:

In [21]: parser._actions
Out[21]:
[_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
_StoreAction(option_strings=['-f', '--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help='Foo help string', metavar=None),
_StoreAction(option_strings=['-b', '--bar'], dest='bar', nargs=None, const=None, default=None, type=None, choices=None, help='Bar help string', metavar=None),
_StoreAction(option_strings=['-z', '--zar'], dest='zar', nargs=None, const=None, default=None, type=None, choices=None, help='Zar help string', metavar=None)]

您还可以通过命令行选项作为 _option_string_actions 属性中的键来查找它们:

In [14]: parser._option_string_actions
Out[14]:
{'--bar': _StoreAction(option_strings=['-b', '--bar'], dest='bar', nargs=None, const=None, default=None, type=None, choices=None, help='Bar help string', metavar=None),
'--foo': _StoreAction(option_strings=['-f', '--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help='Foo help string', metavar=None),
'--help': _HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
'--zar': _StoreAction(option_strings=['-z', '--zar'], dest='zar', nargs=None, const=None, default=None, type=None, choices=None, help='Zar help string', metavar=None),
'-b': _StoreAction(option_strings=['-b', '--bar'], dest='bar', nargs=None, const=None, default=None, type=None, choices=None, help='Bar help string', metavar=None),
'-f': _StoreAction(option_strings=['-f', '--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help='Foo help string', metavar=None),
'-h': _HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
'-z': _StoreAction(option_strings=['-z', '--zar'], dest='zar', nargs=None, const=None, default=None, type=None, choices=None, help='Zar help string', metavar=None)}

关于python - argparse 如何存储所有添加的参数?有没有办法访问他们的名字和帮助字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920452/

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