【发布时间】:2021-07-02 07:16:24
【问题描述】:
这里是代码。
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="infomedia"
)
parser.add_argument("file", help="path to file")
parser.add_argument(
"-i",
"--info",
type=str,
default="False",
help="get information about",
)
cli_args = parser.parse_args()
worker = Worker(
cli_args.input,
cli_args.info,
)
worker._application()
当程序使用 -h / --help 运行时,它会显示默认值。
positional arguments:
file path to file
optional arguments:
-h, --help show this help message and exit
-i INFO, --info INFO get information about (default: False)
如何避免打印默认值?或者有没有办法以不同的方式定义这段代码的默认值?
【问题讨论】:
-
你为什么使用
formatter_class=argparse.ArgumentDefaultsHelpFormatter? -
@hpaulj 对不起。我认为它应该总是被定义。它在指南中,我将其添加到我的代码中。是这个问题吗?
-
是的,它指定了另一种格式化程序,一种显示默认值的格式化程序。这在官方 python 参考中有解释。
-
你想要默认的格式化程序:
argparse.ArgumentParser(description="infomedia")docs.python.org/3/library/argparse.html#formatter-class
标签: python-3.x command-line-arguments argparse