Python argparse中的MetavarTypeHelpFormatter是什么意思?

2024-10-02 08:25:35 发布

您现在位置:Python中文网/ 问答频道 /正文

parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.MetavarTypeHelpFormatter)
parser.add_argument('--foo', type=int)
parser.add_argument('bar', type=float)
parser.print_help()

什么是metavar?你知道吗

MetavarTypeHelpFormatter做什么?你知道吗

在什么情况下我们使用metavar?你知道吗


Tags: addparserfooformattertypeargparsebarargument
1条回答
网友
1楼 · 发布于 2024-10-02 08:25:35

您忘记显示的帮助:

usage: PROG [-h] [ foo int] float

positional arguments:
  float

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

看到intfloat单词了吗?它们匹配type参数。你知道吗

没有特殊格式化程序类的帮助:

usage: PROG [-h] [ foo FOO] bar

positional arguments:
  bar

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

FOObar。你知道吗

使用显式metavar

parser.add_argument(' foo', type=int, metavar='anInt')
parser.add_argument('bar', type=float, metavar='aFloat')

usage: PROG [-h] [ foo anInt] aFloat

positional arguments:
  aFloat

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

相关问题 更多 >

    热门问题