python - how can i change registry_name of argparse's help argument -
python3 argparse use -h , --help argument. want use -h , --host hostname parameter. how can stop argparse use -h help?
i know can use add_help=false when creating instance of argumentparse. gets deal print_help self. this:
import os import argparse 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 message , 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. want add required=true both host , port arguments. it's borken. because when python xxxx.py --help, argparse see missing required argment host , port, complain you, , not show screen.
anyone kown how change default registry_name argparse's argument?
use conflict_handler='resolve' override register_name.
import os import argparse 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 @ end of line" ) parser.add_argument( "-l", "--line", dest="line", action="store_true", help="send line 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 works
d:\pytools>python nc.py usage: nc.py [--help] [-w] [-l] -h host -p port nc.py: error: following arguments required: -h/--host, -p/--port
yes works want
d:\pytools>python nc.py --help usage: nc.py [--help] [-w] [-l] -h host -p port network client optional arguments: --help show message , exit -w, --crlf use crlf @ end of line -l, --line send line line -h host, --host host target host -p port, --port port target port report nc.py bugs http://www.truease.com/forum-66-1.html
yes, want.
Comments
Post a Comment