truease.com
truease.com

Reputation: 1301

how can i change registry_name of argparse's help argument

python3 argparse always use -h and --help for the help argument. Now i want use -h and --host for hostname parameter. How can I stop argparse to use -h for help?

I know I can use add_help=False when I creating an instance of ArgumentParse. But then I gets to deal with print_help my self. like this:

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)

It works. But now I want to add required=True for both host and port arguments. Then it's borken. Because when you do python xxxx.py --help, argparse see you are missing the required argment host and port, it just complain to you, and do not show the help screen.

Anyone kown how to change the default registry_name for argparse's help argument?

Upvotes: 3

Views: 650

Answers (2)

truease.com
truease.com

Reputation: 1301

use conflict_handler='resolve' to override the 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()

let's see how it works

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

yes it works as i want

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

Yes, it is also I want.

Upvotes: 1

unutbu
unutbu

Reputation: 879551

You could subclass argparse.ArgumentParser and override the error method by changing

    self.print_usage(_sys.stderr)

to

    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()

By the way, you do not need to add a --help argument since if you omit it and a user types --help, the error method will get called just the same.

Upvotes: 0

Related Questions