<分区>
我正在编写一个新脚本,并希望在不带任何参数调用脚本时默认调用 -h 或 --help 参数。因此,例如,如果有人调用 command_line_utility.py
,那么我希望它打印出使用 command_line_utility.py -h
获得的输出。
我仔细研究了文档并查看了一些示例,但所有示例都指定了默认参数值,实际上并没有让 arg parse 调用默认参数。
# Setting up Main Argument Parser
main_parser = argparse.ArgumentParser(description="A set of python web utility scripts")
main_parser.add_argument("-v",'--version', action='version', version='kuws V0.0.1')
# Setting up the main subparser
subparsers = main_parser.add_subparsers(help="Available commands found below, for more info on a command use: python command_line_utility.py <command> -h or kuws <command> -h")
"""Code below handles 'redirects' command in the main script
i.e. >python command_line_utility.py redirects or kuws redirects
"""
redirects_parser = subparsers.add_parser('redirects', argument_default='-u',
help='Allows you to trace redirects and get other information')
redirects_parser.add_argument('-u', "--url",
help='usage: python main.py redirects -u <url>; Lets you see the trace for a url', nargs='?', dest="trace_url")
就目前而言,当我运行该文件时,实际上没有任何内容打印到命令行。没有帮助文本或错误或任何东西。