如何使用Python的argparse命令行参数h?

2024-04-24 18:47:22 发布

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

我有以下python:

import argparse
parser = argparse.ArgumentParser()
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-h', '--host_name', required=True, help="Host IP address")
args = parser.parse_args()

这将产生以下错误:

^{pr2}$

除了-h,每个字母都能正常工作。好像是为了——帮助。我怎样才能使它不会-h不会自动保留?在


Tags: nameimportaddtruehostparsergrouprequired
1条回答
网友
1楼 · 发布于 2024-04-24 18:47:22

^{}接受可选参数add_help,您可以设置False。在

^{}的文档中:

Occasionally, it may be useful to disable the addition of this help option. This can be achieved by passing False as the add_help= argument to ArgumentParser:

>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)  
>>> parser.add_argument(' foo', help='foo help')
>>> parser.print_help()
usage: PROG [ foo FOO]

optional arguments:
  foo FOO  foo help

相关问题 更多 >