1. Definition: Argparse is a library in the Python standard library for handling command line parameters
2, the command line parameters are divided into positional parameters and option parameters: The position parameter is the program according to the location of the parameter to determine thesuch as:[Root@openstack_1 /]# lsroot/ #其中root/is positional parameterOption parameters are parameters that the application has already defined in advance and are not arbitrarily specified such as:[Root@openstack_1 /]# ls-L #-L is an option parameter in the LS command
3, the use of the steps:
(1) Import Argparse first imports the module (2) parser = Argparse. Argumentparser () Create a Parse object (3) parser.add_argument () Add the command line arguments and options you want to follow to the object (4) Analysis of Parser.parse_args ()
4,Argparse. Argumentparser () method parameters Notice: Generally we only choose to use description
Prog=none-Program name
Description=none,-start text when Help is displayedEpilog=none,-Show when helpThe ending textParents=[],-as with some of the other parameters, you can inheritFormatter_class=argparse. Helpformatter,-format of custom Help informationPrefix_chars= '-',-the prefix of the command, the default is '-'Fromfile_prefix_chars=none,-command line arguments are read from the fileArgument_default=none,-set a global option default value, typically set individually for each optionConflict_handler= ' ERROR ',-defines what to do when the name of the option added in two add_argument conflicts, the default handling is to throw an exceptionAdd_help=true-whether to increase the-H/--HELP option, which is True by default)
5,Add_argument () method parameters Notice:
Name or flags ...-required, specify the form of the parameter, generally write two, a short parameter, a long parameter
Argparse
Argparse. Argumentparser ()
Parser.add_argument (' echo ') # add_argument () specifies command-line options that the program can accept
Parser.parse_args () # Parse_args () returns some data from the specified options
Print (args)
Print (Args.echo)
Results:G:\flower\python\arg_parse>python demo1.py fooNamespace (echo= ' foo ')Foo
Action represents the way a value is given to a key, which is used in the bool type, and action means the behavior when the specified parameter is present in the read parameter
Help can write helpful information
Parser = Argparse. Argumentparser (Description= ' This is a description ')
Parser. Add_argument ('--ver ', '-V ', Action= ' Store_true ', Help= ' hahaha ')
# to label a variable-value in the dictionary form to depositargsDictionary
Args= Parser. Parse_args ()
ifArgs.ver:
Print("Ture")
Else:
Print("False")
Results:G:\flower\python\arg_parse>python demo1.py-v
TureG:\flower\python\arg_parse>python demo1.py-jusage:demo1.py [-h] [--ver]demo1.py:error:unrecognized arguments:-j
Required-required parameters, normally-f options are optional, but if required=true is required
Type-Specifies the parameter type
# Required label means --ver parameter is required, and the type is int , input other types will error
Parser.add_argument ('--ver '-v 'True int )
Results:
G:\flower\python\arg_parse>python demo1.py-v 1TureG:\flower\python\arg_parse>python demo1.py-v SSusage:demo1.py [-h]--ver verdemo1.py:error:argument--ver/-v:invalid int value: ' SS '
Choices-Sets the range of parameters, if the type in choice is not a string, specify the type
Indicates that the value of this parameter can only be derived from a certain number of value candidate values, in addition to the error, with the choice parameter can be
parser. add_argument (' file '[' test1'test2 ' ])
parser. Parse_args ()
Print (' read in%s '%(args.file))
Results:G:\flower\python\arg_parse>python demo1.py test1Read in Test1
Nargs-Specifies how many of the value following this parameter, which defaults to 1
# indicates that a script can read in two integers to giveNumkey (the value at this time is2An array of integers)
Parser.add_argument (' filename ', Nargs= 2, type= int)
Args= Parser.parse_args ()
Print(' read in%s '%(Args.filename))
Results:G:\flower\python\arg_parse>python demo1.py 1 2 3
usage:demo1.py [-h] filename filenamedemo1.py:error:unrecognized Arguments:3Analysis: Nargs can also be used to indicate that if there is a positional parameter input, then all input will be used as the value of the positional parameter; ' + ' means reading at least 1 of the positional parameters. ‘?‘ Indicates that the positional parameter is either not, or just one. (PS: Consistent with the symbolic use of regular expressions.) such as
parser. add_argument (' filename '+ ' int)
parser. Parse_args ()
Print (' read in%s '%(args.filename))
dest -Set the value of this option to which property to put in after parsing it
parser. Add_argument ('-file '[' test1 'test2 ' ]' World ')
parser. Parse_args ()
Print (' read in%s '%(args.world))
Results:
G:\flower\python\arg_parse>python demo1.py-file test1Read in Test1
Above
Python3 in Argparse module