仅在参数列表中而不是在其用法中更改argparse中的metavar值 [英] Changing the metavar value in argparse only in argument listing and not in its Usage

查看:79
本文介绍了仅在参数列表中而不是在其用法中更改argparse中的metavar值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于无需重复ALLCAPS的argparse帮助问题.

尽管我会简要解释一下这个问题是什么,我的问题是什么:
我想以默认的-h,--help的方式为我的选项显示argparse帮助,每个选项后都没有ALLCAPS文本,或者至少没有重复的CAPS.

Though i would explain in brief what that question was and what my question is:
I'd like to display argparse help for my options the same way the default -h,--help is, without the ALLCAPS text after each option, or at least without the duplicated CAPS.

例如,使用以下代码:

#filename=temp.py
import argparse
p = argparse.ArgumentParser()
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()

正在运行python temp.py -h:

usage: temp.py [-h] [-i INI]

optional arguments:
  -h, --help         show this help message and exit
  -i INI, --ini INI  use alternate ini file

现在我想要的是这样的东西:

Now what I want is something like:

usage: 123.py [-h] [-i INI]

optional arguments:
  -h, --help         show this help message and exit
  -i, --ini INI      use alternate ini file

OR

usage: 123.py [-h] [-i INI]

optional arguments:
  -h, --help         show this help message and exit
  -i, --ini          use alternate ini file

要获得第二个,方法是将p.add_argument行中的默认metavar更改为:

To get the second one, the way is to change the default metavar in the line p.add_argument as:

p.add_argument('-i', '--ini', help="use alternate ini file")

并更改argparse.ArgumentParser()中的默认用法声明.

and change the default usage statement in argparse.ArgumentParser().

但是当我的代码中可选参数的数量增加时,我发现很难通过根据代码中的修改来添加和删除参数来修改用法消息.

But when number of optional arguments increases in my code, I find it difficult to modify the usage message by adding and deleting the argument according to the modification in my code.

还有其他方法可以解决metavar问题而又不影响使用说明.

Is there any other way of solving the problem of metavar without affecting the usage statement.

如果我想显示我的帮助,如第一种情况所示,在-i, --ini之后只有INI次.

Also what if I want to have my help displayed as shown in the first case, where there is only one time INI after -i, --ini.

如果我在将帮助显示为-i, --ini INI-i, --ini而不是-i INI, --ini INI时出错,请以正确的原因纠正我. (错误是指我使用的约定会导致用户混淆或误解)

If I am getting wrong in showing help as -i, --ini INI or -i, --ini instead of -i INI, --ini INI please correct me with proper reason. (By getting wrong I mean, it the convention I am using will lead to confusion or misunderstanding in user)

推荐答案

对于快速解决方案,您只需将退格字符设置为metavar.

For a fast solution you can just set backspace character to a metavar.

p.add_argument('-i', '--ini', help="use alternate ini file", metavar='\b')

它将为您提供:

optional arguments:
  -h, --help         show this help message and exit

如果您想要这样:

  -i, --ini INI      use alternate ini file

您将必须修改帮助格式化程序.在此处回答 python argparse帮助消息,为短选项禁用metavar吗?/a>

You will have to modify help formatter. Answered here python argparse help message, disable metavar for short options?

这篇关于仅在参数列表中而不是在其用法中更改argparse中的metavar值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆