相关疑难解决方法(0)

Argparse:在'--help'中包含默认值的方法?

假设我有以下argparse片段:

diags.cmdln_parser.add_argument( '--scan-time',
                     action  = 'store',
                     nargs   = '?',
                     type    = int,
                     default = 5,
                     help    = "Wait SCAN-TIME seconds between status checks.")
Run Code Online (Sandbox Code Playgroud)

目前,--help退货:

usage: connection_check.py [-h]
                             [--version] [--scan-time [SCAN_TIME]]

          Test the reliability/uptime of a connection.



optional arguments:
-h, --help            show this help message and exit
--version             show program's version number and exit
--scan-time [SCAN_TIME]
                    Wait SCAN-TIME seconds between status checks.
Run Code Online (Sandbox Code Playgroud)

我更喜欢这样的东西:

--scan-time [SCAN_TIME]
                    Wait SCAN-TIME seconds between status checks.
                    (Default = 5)
Run Code Online (Sandbox Code Playgroud)

偷看帮助格式化程序代码显示有限的选项.是否有一种聪明的方式来argparse--scan-time类似的方式打印默认值,或者我应该只是 …

python argparse

265
推荐指数
4
解决办法
4万
查看次数

标签 统计

argparse ×1

python ×1