我正在使用
ArgumentParser
在 python 中,让用户可以选择将自己的函数添加到我的脚本中。为此,我给他们一个函数来生成在 argparse 中使用的数据
add_argument()
功能。我注意到 add_argument 的签名是
add_argument(self, *args, **kwargs)
但是当查看 vscode 中的文档时,我可以看到它可以获得的所有不同参数(action、nargs、const、default 等)。
Python是如何做到的?据我所知,当您向用户提供 **kwargs 时,它会显示为 **kwargs,仅此而已。
我浏览了这个网站和整个互联网,但我找不到它使用的语法。
看起来更好,这就是定义
class ArgumentParser(_AttributeHolder, _ActionsContainer):
"""Object for parsing command line strings into Python objects.
Keyword Arguments:
- prog -- The name of the program (default:
``os.path.basename(sys.argv[0])``)
- usage -- A usage message (default: auto-generated from arguments)
- description -- A description of what the program does
- epilog -- Text following the argument descriptions
- parents -- Parsers whose arguments should be copied into this one
- formatter_class -- HelpFormatter class for printing help messages
- prefix_chars -- Characters that prefix optional arguments
- fromfile_prefix_chars -- Characters that prefix files containing
additional arguments
- argument_default -- The default value for all arguments
- conflict_handler -- String indicating how to handle conflicts
- add_help -- Add a -h/-help option
- allow_abbrev -- Allow long options to be abbreviated unambiguously
- exit_on_error -- Determines whether or not ArgumentParser exits with
error info when an error occurs
"""
def __init__(self,
prog=None,
usage=None,
description=None,
epilog=None,
parents=[],
formatter_class=HelpFormatter,
prefix_chars='-',
fromfile_prefix_chars=None,
argument_default=None,
conflict_handler='error',
add_help=True,
allow_abbrev=True,
exit_on_error=True):
在argparse.py:1720 参数类型已明确声明