繁体   English   中英

我怎样才能更改argparse帮助参数的registry_name

[英]how can i change registry_name of argparse's help argument

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吗?

使用conflict_handler ='resolve'覆盖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

是的,这也是我想要的。

您可以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方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM