Official documents
Http://python.usyiyi.cn/translate/python_352/library/argparse.html
code example
ImportArgparse#1. Get parameter Resolution objectsParser = Argparse. Argumentparser (description="Help information before the description of some information, can not write Oh")#2. Add resolvable parameters#add_argument () function Common parameters#name or List of flags option string, such as Foo or-F,--foo#Nargs should read the number of command line arguments, the allowed values are#N (n is a number) means that there must be n arguments#The ' * ' represents any parameter that can be#'? ' delegate can have 0 or 1 parameters#' + ' represents at least one parameter#if Nargs is specified and the value is multiple, then it returns a list#Default parameter Defaults#type command line arguments should be converted to (by default read into a string)#The choices parameter allows a container of values that are valid parameters#whether the required parameter is required#Short description of the help parameterParser.add_argument (" -A","--app", Default=0, Type=int, Choices=range (1, ten), help="app ' s code") parser.add_argument ("First_arg", nargs=3)#Position ParametersParser.add_argument ("Second_arg")#Position ParametersParser.add_argument ("- S","--student", nargs=2, help="Sub Need", required=True) parser.add_argument ("- y", action='store_true')#options can be no arguments, have-y is true, none is false#3. Parsing Parameters#Parse_args () returns a very simple namespace object that is designed#when Parse_args () is called, the optional arguments are identified with a-prefix, and the remaining parameters are assumed to be positional parameters#Note: Positional parameters are requirednamespace =Parser.parse_args ()#4. Get the parameter values from the parsed namespace object#Note:#if Add_argument () is given a method similar to-A,--app this parameter#then you can use-A to pass the pass, but to resolve to the Namespace object is only an app attribute, that is, the use of NAMESPACE.A is not recognizedPrint(namespace)Print(Namespace.app)Print(NAMESPACE.Y)
Python3-argparse Module-parsing command-line arguments