【问题标题】:Custom usage message for a single argument in argparseargparse 中单个参数的自定义使用消息
【发布时间】:2016-12-27 03:38:37
【问题描述】:

我有一个参数可以由用户指定一个或两个参数:

parser.add_argument("-s", "--start", required=True, metavar='START_TIME', 
  nargs='+', help="The start time either in milliseconds or as MM/DD/YYYY HH:MM:SS.")

帮助消息显示:

usage: foo
   -s START_TIME [START_TIME ...]

Foo

optional arguments:
  -s START_TIME [START_TIME ...], --start START_TIME [START_TIME ...]
                    The start time of the query window either in
                    milliseconds or as MM/DD/YYYY HH:MM:SS (24 hr).

这有点误导,因为 [START_TIME ...] 部分。有没有一种方法可以修改这个参数的用法消息,使其显示更像:

usage: foo
   -s START_TIME

Foo

optional arguments:
  -s START_TIME, --start START_TIME
                    The start time of the query window either in
                    milliseconds or as MM/DD/YYYY HH:MM:SS (24 hr).

我知道我可以用 argparse 替换整个用法消息,但我还有其他几个我不想搞砸的参数。我想做类似'nargs ='1 | 2'之类的事情,但恐怕这可能是一厢情愿......除了重组我的CLI之外,我可以做些什么来修复单个参数的使用消息?谢谢。

【问题讨论】:

    标签: python argparse


    【解决方案1】:

    我建议从您对add_argument 的呼叫中删除nargsnargs='+' 表示该参数可能有多个输入,但实际上您总是需要一个参数。 MM/DD/YYYY HH:MM:SS 的字符串在概念上是一个参数,可以用引号传入:

    python script.py -s 978370496000
    python script.py -s "01/01/2001 12:34:56"
    
    python temp3.py -h
    usage: temp3.py [-h] -s START_TIME
    
    optional arguments:
      -h, --help            show this help message and exit
      -s START_TIME, --start START_TIME
                            The start time either in milliseconds or as MM/DD/YYYY
                            HH:MM:SS.
    

    这将产生您想要的使用消息,我认为调用者不会感到困惑。

    【讨论】:

    • 感谢卡琳的回答。我试图让我的程序更加灵活,这样用户就不必将他们的时间参数用引号括起来,但我想我可能只是让它变得更加混乱。
    【解决方案2】:

    '+' 参数的默认显示是 S [S ...]。它应该传达这样一种想法,即您必须给出至少一个值,但可以有更多。 (看看* 产生了什么)。

    In [306]: parser=argparse.ArgumentParser()
    In [307]: a1=parser.add_argument('-s',nargs='+')
    In [308]: parser.print_help()
    usage: ipython3 [-h] [-s S [S ...]]
    
    optional arguments:
      -h, --help    show this help message and exit
      -s S [S ...]
    

    在您的情况下,metavar 字符串只是替换从dest 派生的默认字符串。

    你可以给出一个元组元变量,它显示为:

    In [309]: a1.metavar=('A','B')
    In [310]: parser.print_help()
    usage: ipython3 [-h] [-s A [B ...]]
    
    optional arguments:
      -h, --help    show this help message and exit
      -s A [B ...]
    

    空元变量:

    In [312]: parser.print_help()
    usage: ipython3 [-h] [-s  [...]]
    
    optional arguments:
      -h, --help  show this help message and exit
      -s  [ ...]
    

    覆盖此格式

    '%s [%s ...]' % get_metavar(2)
    

    您必须使用修改后的 _format_args 方法创建自定义 HelpFormatter 子类。

    ==================

    至于nargs='1|2',我已经探索过添加一个以regex 语法为模型的范围选项。加起来不难,但要熟悉argparse.py代码。该主题存在 Python 错误/问题。

    【讨论】:

    • 感谢您的深入解答!不过,我想我会采用更简单的方法。
    【解决方案3】:

    如果您想要零个、一个或两个参数而不是更多,您可以有两个选项-s1-s2

    用户可以更明显或更清楚地了解您的应用程序的使用情况。

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 2019-05-15
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2014-04-02
      相关资源
      最近更新 更多