#-*-coding:utf-8-*-ImportArgparseargs="- F hello.txt-n 1 2 3-x 100-y b-z a-q Hello @args. txt i_am_bar-h". Split ()#using @args.txt requires fromfile_prefix_chars= "@"#args.txt file should be a single parameter, want to change the behavior of Reference Convert_arg_line_to_args ()#simple description of the Argumentparser parameter## Description-Start text for command line help, in most cases we will only use this parameter#Epilog-end text for command line help#prog-(default:sys.argv[0]) program name, generally do not need to modify, in addition, if you need to use in Help to the program's name, you can use% (Prog) s#prefix_chars-Prefix of the command, default is-, for example-f/--file. Some programs may want to support options such as/F and can use prefix_chars= "/"#Fromfile_prefix_chars-(Default:none) If you want the command line arguments to be read from the file, you may use it. For example, if fromfile_prefix_chars= ' @ ', there is a "@args. txt" in the command-line arguments, the contents of Args.txt are used as command-line arguments#Add_help-whether to add-h/-help option (default:true), general help information is required, so do not set it. ## Parents-type is list, if some of the options for this parser are the same as some other parser options, you can use parents to implement inheritance, such as Parents=[parent_parser]## Formatter_class-format of custom Help information (description and Epilog). By default, long help messages are < wrap and eliminate multiple contiguous blanks >. #three allowable values:#class Argparse. Rawdescriptionhelpformatter Direct output of the original form of description and Epilog (no line wrapping and whitespace removal)#class Argparse. Rawtexthelpformatter Direct output of the original form of the Help string in description and Epilog as well as in add_argument (no line wrapping and whitespace elimination)## class Argparse. Argumentdefaultshelpformatter output their corresponding default values after the Help information for each option, if one is set. This is the most used! #Argument_default-(default:none) sets the default value for a global option, which is typically set separately for each option, so this parameter is used sparingly, not to elaborate#Usage-(default:generated) If you need to modify the usage information (Usage:prog [-h] [--foo [foo]] bar [bar ...] ), then you can modify this, generally do not modify. #Conflict_handler-it is not recommended. This is only used in extreme cases, mainly to define how the names of the options added in the two add_argument are to be handled when conflicts occur, and the default handling is to throw exceptions. #note A line with # #表示这几个参数比较常用Parser = Argparse. Argumentparser (description="This is a description of% (Prog) s", epilog="This is a epilog of% (prog) s", prefix_chars="-+", fromfile_prefix_chars="@", formatter_class=Argparse. Argumentdefaultshelpformatter)#argumentparser.add_argument (name or flags ...) [, Action] [, Nargs] [, Const] [, Default] [, type] [, Choices] [, Required] [, help] [, Metavar] [, dest])#the parameters of the add_argument are more complex ... #name or flags-Specify the form of the parameter, want to write a few write a few, but we generally write two, a short parameter, a long parameter, see the following example "-F", "--file"#Optional options, position is not fixed, want to write how to write, default is optionalParser.add_argument ("- F","--file", help="Test test test")#fixed-position options, such as "Prog I_am_bar", in this case, I_am_bar is the value of the bar option, which is required by defaultParser.add_argument ("Bar", help="Test test test")#Nargs-Specify how many value after this parameter, for example, we want to use-N 1 2 3 4 to set the value of N to [1, 2, 3, 4]Parser.add_argument ("- N","--num", nargs="+", type=int)#here nargs= "+" means that if you specify the-n option, then-n is followed by at least one parameter, + denotes at least one, or one or 0, * 0 or more ,#Default-If this option is not present on the command line, use defaults to specifyParser.add_argument ("+g","++gold", help="Test test test", default="Test_gold")#requires prefix_chars with "+"#Type -If the parameter you want to pass in is a specified type (for example, float, int or file, etc. that can be converted from a string), you can use theParser.add_argument ("- x", type=int)#choices-Sets the range of parameter values, if the type in choices is not a string, remember to specify the type OHParser.add_argument ("- y", choices=['a','b','D'])#Required-the option of usually-f is optional, but if required=true is necessaryParser.add_argument ("- Z", choices=['a','b','D'], required=True)#Metavar-The name of the parameter, which is used only when displaying help information.Parser.add_argument ("- o", metavar="oooooo")#Help-set this option for helpful information#dest-Sets the value of this option to which property to put after parsingParser.add_argument ("- Q", dest=" World") args= Parser.parse_args (args)#If you do not have the args parameter, then use SYS.ARGV, which is the command line argument. With this parameter, it's convenient for us to debug.#Args.world is the value of-Q.#action-the Basic type of action to being taken when this argument is encountered on the command line.#Const-a constant value required by some action and Nargs selections.#These two look at the help documents themselves, more complex#http://docs.python.org/library/argparse.htmlPrintArgs
Sample code::
usage:argparse_sample.py [-h] [-f FILE] [-n num [num ...]] [+g GOLD] [-x x] [-y {a,b,d}]-Z {a,b,d} [-O oooooo] [-Q World] Bar This isa description of argparse_sample.py positional arguments:bar test test test optional arguments: -H,--help show this help message andExit-F FILE,--file File test test test (Default:none)-N num [num ...],--num num [num ...] +g GOLD, + +Gold Gold Test test test (Default:test_gold)-x x-y {a,b,d}-z {a,b,d}-o oooooo-Q World isa epilog of argparse_sample.py
Python Learning _argsparse