python3 argparse总是使用-h和--help作为帮助参数。现在我想使用-h和--host作为hostname参数。如何阻止argparse使用-h寻求帮助?
我知道在创建ArgumentParse实例时我可以使用add_help = False。但后来我开始处理print_help我自己。像这样:
import os
import argparse
from inc import epilog
def ParseCommandLine():
parser = argparse.ArgumentParser(
description = "Network client program",
epilog = epilog,
add_help = False,
)
parser.add_argument(
"--help",
dest="help",
action="store_true",
help="show this help message and exit",
)
parser.add_argument(
"-h", "--host",
dest="host",
action="store",
help="target host",
)
parser.add_argument(
"-p", "--port",
dest="port",
action="store",
type=int,
help="target port",
)
return parser, parser.parse_args()
def Main():
parser, opt = ParseCommandLine()
if opt.help:
parser.print_help()
os.sys.exit(0)
有效。但是现在我想为主机和端口参数添加required = True。然后它是borken。因为当你执行python xxxx.py --help时,argparse看到你缺少所需的段主机和端口,它只是向你抱怨,并且不显示帮助屏幕。
有人知道如何为argparse的帮助参数更改默认的registry_name吗?
答案 0 :(得分:1)
使用conflict_handler ='解决'覆盖register_name。
import os
import argparse
from inc import epilog
def ParseCommandLine():
parser = argparse.ArgumentParser(
description = "Network client",
epilog = epilog,
conflict_handler='resolve'
)
parser.add_argument(
"-w", "--crlf",
dest="crlf",
action="store_true",
help="use CRLF at the end of line"
)
parser.add_argument(
"-l", "--line",
dest="line",
action="store_true",
help="send line by line",
)
parser.add_argument(
"-h", "--host",
dest="host",
action="store",
required=True,
help="target host",
)
parser.add_argument(
"-p", "--port",
dest="port",
action="store",
type=int,
required=True,
help="target port",
)
return parser, parser.parse_args()
def Main():
parser, opt = ParseCommandLine()
if __name__ == '__main__':
Main()
让我们看看它是如何运作的
D:\pytools>python nc.py
usage: nc.py [--help] [-w] [-l] -h HOST -p PORT
nc.py: error: the following arguments are required: -h/--host, -p/--port
是的,它按我想要的方式工作
D:\pytools>python nc.py --help
usage: nc.py [--help] [-w] [-l] -h HOST -p PORT
Network client
optional arguments:
--help show this help message and exit
-w, --crlf use CRLF at the end of line
-l, --line send line by line
-h HOST, --host HOST target host
-p PORT, --port PORT target port
Report nc.py bugs to http://www.truease.com/forum-66-1.html
是的,这也是我想要的。
答案 1 :(得分:0)
您可以继承argparse.ArgumentParser
并通过更改
error
方法
self.print_usage(_sys.stderr)
到
self.print_help(argparse._sys.stderr)
import argparse
class MyArgumentParser(argparse.ArgumentParser):
def error(self, message):
"""error(message: string)
Prints a usage message incorporating the message to stderr and
exits.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
self.print_help(argparse._sys.stderr)
self.exit(2, argparse._('%s: error: %s\n') % (self.prog, message))
def parse_command_line():
parser = MyArgumentParser(
description="Network client program",
# epilog = epilog,
add_help=False,)
parser.add_argument(
"-h", "--host",
dest="host",
action="store",
help="target host",
required=True)
parser.add_argument(
"-p", "--port",
dest="port",
action="store",
type=int,
help="target port",)
return parser, parser.parse_args()
parser, opt = parse_command_line()
顺便说一句,您不需要添加--help
参数,因为如果省略它并且用户键入--help
,error
方法将被调用。< / p>