0

当我使用 argparse 时,例如:

from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-i", "--input", action="store")

那么帮助菜单是这样的:

usage: test.py [-h] [-i INPUT]

options:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        input file

如果我不想要--input INPUT这里,我该怎么办?像这样:

usage: test.py [-h] [-i INPUT]

options:
  -h, --help            show this help message and exit
  -i INPUT              input file
4

3 回答 3

3

您可以有两个具有相同目标属性的选项,并用于argparse.SUPPRESS隐藏其中一个的帮助文本。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="input", action="store")
parser.add_argument("--input", dest="input", action="store", help=argparse.SUPPRESS)
于 2021-10-05T16:34:18.750 回答
0

删除--inputadd_argument删除该选项并添加dest='input'以保留INPUT在帮助文本中

parser.add_argument("-i", dest='input', action="store")
于 2021-10-05T16:28:12.697 回答
-1

设置dest用于存储值的名称:

from argparse import ArgumentParser                                             
parser = ArgumentParser()                                                       
parser.add_argument("-i", dest="input", help="input file", action="store")      
parser.parse_args()
于 2021-10-05T16:36:25.693 回答