在内部,这些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)}