The gods are silent - personal CSDN blog directory
VX "polarisrising war" can directly search and add author friend discussions.
argparse module (Python official document: argparse - command line options, parameters, and subcommand Parser - Python 3.10.2 documentation ), which can be used to pass parameters directly when running Python scripts from the command line.
This paper will first give the code format that can be applied directly, and then introduce more content on this basis.
At present, this article only briefly introduces the necessary functions of argparse package, and its deeper applications may continue to be updated in the future.
argparse is a module that comes with Python.
Import the package through import argparse.
A simple application example: use argparse to pass in the parameter section_id and GPU_ ID, and get two parameters (gpu_id is directly converted to torch.device 1)
parser = argparse.ArgumentParser() #Create parser #The ArgumentParser object contains all the information needed to parse the command line into Python data types. parser.add_argument("--section_id",default='1',type=str) parser.add_argument("--gpu_id",default='0',type=str) #Add parameters. Each participant will be introduced in detail later args = parser.parse_args() #Analytical parameters arg_dict=args.__dict__ #Set ARG_ Convert dict to dict format section_id=arg_dict['section_id'] device = torch.device("cuda:"+arg_dict['gpu_id'] if torch.cuda.is_available() else "cpu")
Run the following statement on the command line: Python example py --section_ id 2 --gpu_ ID 1 (example.py is a python script, and the parameter is followed by the value to be passed in)
Note that if anaconda is used as the environment manager, if you want to directly use the python command, you need to make terminal in the virtual environment; Otherwise, replace the python command with the path of the Python interpreter.
The following examples and overall content architecture refer to the official documents:
Example prog Py (get a list and calculate its sum or maximum):
import argparse parser = argparse.ArgumentParser(description='Process some integers.') #description is the text displayed before the parameter help document parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') #The integers property will be a list of one or more integers #The calculate attribute will be the sum() function when the -- sum parameter is specified on the command line, otherwise it will be the max() function. args = parser.parse_args() print(args.accumulate(args.integers))
Run Python prog. On the command line Py - H, get the prompt message:
usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers (default: find the max)
Example of calculating the maximum value: Python prog py 1 2 3 4
Example of summation: Python prog py 1 2 3 4 --sum
Example of passing in invalid parameters and reporting errors: Python prog py a b c
Error message:
usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'
add_argument() method (only some input parameters are listed below):
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
- name or flags - a name or a list of option strings, such as foo or - F, - foo.
The one preceded by - or - is the optional argument, otherwise it is the positive argument (which must be passed in from the command line). The first parameter passed in can be a series of flags or a single argument name.
Example of creating an optional argument: parser add_ argument('-f', '--foo')
Example of creating a positive argument: parser add_ argument('bar')
>>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument('-f', '--foo') >>> parser.add_argument('bar') >>> parser.parse_args(['BAR']) Namespace(bar='BAR', foo=None) >>> parser.parse_args(['BAR', '--foo', 'FOO']) Namespace(bar='BAR', foo='FOO') >>> parser.parse_args(['--foo', 'FOO']) usage: PROG [-h] [-f FOO] bar PROG: error: the following arguments are required: bar
- Action - the basic type of action used when the parameter appears on the command line.
- Default value 'store': store parameter values
- ‘store_const ': store the value specified by const keyword argument.
- nargs - a single action is associated with several parameters.
- '*': integrate all parameters into one list
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', nargs='*') >>> parser.add_argument('--bar', nargs='*') >>> parser.add_argument('baz', nargs='*') >>> parser.parse_args('a b --foo x y --bar 1 2'.split()) Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])
2. ‘+’: Similar to '*', but error will be reported if at least one parameter is not passed in.
>>> parser = argparse.ArgumentParser(prog='PROG') #prog input parameter is the program name, and the default value is OS path. basename(sys.argv[0]) >>> parser.add_argument('foo', nargs='+') >>> parser.parse_args(['a', 'b']) Namespace(foo=['a', 'b']) >>> parser.parse_args([]) usage: PROG [-h] foo [foo ...] PROG: error: the following arguments are required: foo
- const - the required constant selected by some actions and nargs. If the input parameter is action='store_const 'or' append '_ const 'will add the value to a parameter value, and the value must be assigned. The default value is None
- Default - default value, the value used when the parameter does not appear on the command line and does not exist in the namespace object.
optional argument is used when option name does not appear on the command line:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default=42) >>> parser.parse_args(['--foo', '2']) Namespace(foo='2') >>> parser.parse_args([]) Namespace(foo=42)
If the target namespace already has attribute set, it will not be used:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default=42) >>> parser.parse_args([], namespace=argparse.Namespace(foo=101)) Namespace(foo=101)
If the default value is a string, the parser will pass parameters like the command line, such as automatically converting according to the type value; Otherwise, use the original object:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--length', default='10', type=int) >>> parser.add_argument('--width', default=10.5, type=int) >>> parser.parse_args() Namespace(length=10, width=10.5)
- Type - the type to which the command line parameter should be converted.
It can be any function with callable and single str input parameter, such as float, int, ascii, ord, open and argparse FileType('w', encoding='latin-1'),pathlib.Path, or a user-defined function.
Only errortype, errortargumenttypeor errortargumenttypecan be returned.
bool is not recommended because it only sets empty strings to False and non empty strings to True.
Only simple treatment is recommended. Some problems are that the error information is too complex. The problem with FileType is that it does not automatically close files. - help - a brief description of what this parameter does.
View it on the command line with the - h or - help parameter. - metavar - the name of the parameter displayed in usage messages.
In the help information generated by ArgumentParser, various parameters need to be distinguished. The dest value is used as the object name by default. Positive argument actions directly uses the dest value, optional argument actions capitalizes the dest value, and metavar is an optional name. Metavar only changes the display name, and the attribute name is still the dest value. nargs with different values may make metavar used many times.
Simple example:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', metavar='YYY') >>> parser.add_argument('bar', metavar='XXX') >>> parser.parse_args('X --foo Y'.split()) Namespace(bar='X', foo='Y') >>> parser.print_help() usage: [-h] [--foo YYY] XXX positional arguments: XXX options: -h, --help show this help message and exit --foo YYY
Pass in multiple metavar instances with tuples:
>>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument('-x', nargs=2) >>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz')) >>> parser.print_help() usage: PROG [-h] [-x X X] [--foo bar baz] options: -h, --help show this help message and exit -x X X --foo bar baz
- dest - parse_args() returns the property name of the object.
Default:
Positive argument actions is add_ The first argument to argument().
Example:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('bar') >>> parser.parse_args(['XXX']) Namespace(bar='XXX')
The optional argument actions are inferred from the option strings: select an option string that can start with --, then remove --, and replace it with -. Then change the - in the middle of the option string to_
Example:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('-f', '--foo-bar', '--foo') >>> parser.add_argument('-x', '-y') >>> parser.parse_args('-f 1 -x 2'.split()) Namespace(foo_bar='1', x='2') >>> parser.parse_args('--foo 1 -y 2'.split()) Namespace(foo_bar='1', x='2')
Specify dest input parameter value:
Example:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', dest='bar') >>> parser.parse_args('--foo XXX'.split()) Namespace(bar='XXX')
Other references not mentioned in the text and endnotes:
Yes, torch For the explanation of device, please refer to my previous blog post PyTorch Python API details (continuous updat ing...)_ The silent blog of the gods - CSDN blog ↩︎