Examples of functions:
Import argparse # create a parser (using argparse first step is to create a ArgumentParser Objects) Parser = argparse.ArgumentParser (the Description = ' Process some integers ' ) # add the parameter # to add a ArgumentParser program parameter information by calling add_argument ( ) Method completed. # Usually, these calls ArgumentParser specify how to obtain command-line string and convert it to an object. This information is stored and used when parse_args () is called. parser.add_argument ( ' integers ' , metavar = ' N ' , type = int, nargs = ' + ' , help = ' an integer for the accumulator ' ) parser.add_argument ( ' --sum' , Dest = ' the accumulate ' , Action = ' store_const ' , const = SUM, default = max, Help = ' SUM The integers (default: Find The max) ' ) # resolution parameters # ArgumentParser by parse_args () method of resolution parameters. It will check the command line, convert each parameter to the appropriate type and then call the corresponding operation. # In most cases, this means a simple Namespace object parsed from the command line parameter attributes construct: args = parser.parse_args () Print (args.accumulate (args.integers))
operation result:
(base) D:\PycharmProjects1>python.exe argparse_test.py -h usage: argparse_test.py [-h] [--sum] N [N ...] Process some integers positional arguments: N an integer for the accumulator optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max) (base) D:\PycharmProjects1>python.exe argparse_test.py 12 33 44 44 (base) D:\PycharmProjects1>