本文介绍了访问和“隐式"参数帮助字符串中的metavar值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有显式动作的情况下在 argparse.ArgumentParser()上调用 add_argument 时,将获得"store" 动作.在自动生成的-help 输出中,除非设置了 metavar :

When you call add_argument on an argparse.ArgumentParser() without an explicit action, you get the "store" action. In the auto-generated --help output you get the uppercase of the long option, unless you set metavar:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--version', metavar='X.Y.Z')
parser.add_argument('--date'),
parser.parse_args(['--help'])

显示:

usage: try.py [-h] [--version X.Y.Z] [--date DATE]

optional arguments:
  -h, --help       show this help message and exit
  --version X.Y.Z
  --date DATE

在这种情况下,我将 X.Y.Z 称为显式metavar,将 DATE 称为隐式metavar.

In this I would call X.Y.Z an explicit metavar, and DATE an implicit metavar.

如果您想获得更多有用的帮助,可以执行以下操作:

If you want to have more useful help you can do:

parser.add_argument('--version', metavar='X.Y.Z', 
                    help = "set version to % (metavar)s")

给出(仅显示更改的行):

which gives (only changed lines shown):

  --version X.Y.Z  set version to X.Y.Z

并能够在帮助字符串中使用该%(metavar)s 很好,因为当您更改 metavar ='MAJOR.MINOR'时,帮助不会不需要更新(您一定会忘记).

and being able to use that %(metavar)s in the help string is nice because when you change metavar='MAJOR.MINOR', the help doesn't need to be updated (which you are bound to forget).

但是,如果您添加带有隐式metavar的-date 参数的帮助,则:

But if you add the help for the --date argument, with the implicit metavar:

parser.add_argument('--date', 
                    help="use %(metavar)s instead of today's date")

您得到:

  --date DATE      use None instead of today

None 不是我所期望的,也不是我想要的.

And that None is not what I expected, nor what I want.

当然,我总是可以在帮助中对'DATE'进行硬编码,或显式提供metavar(尤其是在帮助字符串中使用它时).但是当我这样做时,当我更改long选项的名称时,我一定会忘记更新metavar.

Of course I can always hard-code 'DATE' in the help, or explicitly provide the metavar (especially when it is used in the help string). But when I do that, I am bound to forget to update the metavar when I change the name of the long option.

是否有一种自动"方式来获取帮助字符串中的 DATE 而不是 None ?
还是我在使用%(metavar)s ?我应该在其他地方使用(如果是,则使用什么)?

Is there an "automatic" way to get DATE in the help string instead of None ?
Or am I using %(metavar)s where I should be using something else (and if so, what)?

推荐答案

在调用 parser.parse_args()之前,您可以做的一件事就是更新添加到 parser 中的那些动作.>的 metavar 属性为 None :

One thing you can do before calling parser.parse_args() is update those actions added to the parser that have a metavar attribute that is None:

for action in parser._actions:
    if not hasattr(action, 'metavar') or not hasattr(action, 'dest'):
        continue
    metavar = getattr(action, 'metavar')
    if metavar is None:
        action.metavar = action.dest.upper()

生成的输出如下:

  --version X.Y.Z  set version to X.Y.Z
  --date DATE      use DATE instead of today

但是正如我们在BDFL的母语中所说:"mooi是安德斯"¹

But as we say in the BDFL's native language: "mooi is anders" ¹

¹

这篇关于访问和“隐式"参数帮助字符串中的metavar值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:06