argparse function usage

argparse is Python's built-in library for parsing command-line parameters. It can help developers create user-friendly command-line interfaces, so that users can pass parameters to programs through the command line. Through argparse, you can define the type, number and help text of the command line parameters that the program can accept.

First, the basic usage of argparse:

#导入argparse模块:
import argparse 

#创建ArgumentParser对象:
parser = argparse.ArgumentParser(description='Description of your program')

#添加命令行参数:
parser.add_argument('arg_name', type=str, help='Help text for this argument')
 
#解析命令行参数:
args = parser.parse_args()

#使用解析得到的参数:
print(args.arg_name)

arg_name : The parameter name, you can access the value of the parameter through args.arg_name.
type : The type of the parameter, which can be int, float, str, etc.
help : The help text for the parameter, which will be displayed when the user requests help.

parser.add_argument is a method of the argparse.ArgumentParser class that is used to add command-line argument options to the parser.

parser.add_argument(name or flags, action, nargs, const, default, type, choices, required, help, metavar)

name or flags : The name or options of the parameter. Can be the name of a positional argument (no prefix) or an option name prefixed with - or - -. For example, "filename" is the name of a positional parameter, and "- -output" is the name of an option.

action : The action of the parameter. It specifies what action should be taken when the parameter appears on the command line. Commonly used actions are "store" (save value, default), "store_const" (save constant value), "store_true" (save True), "store_false" (save False), "append" (append value to the list), "append_const" (append a constant to a list), etc.

nargs : the number of arguments. It specifies the number of values ​​accepted by this parameter. Commonly used values ​​are None (default, accept a value), '?' (accept zero or one value), '*' (accept zero or more values), '+' (accept at least one value), etc.

const : A constant value. When the parameter uses the store_const action, the parameter specifies the constant value stored.

default : The default value. The default value used when this parameter is not provided on the command line.

type : The type of the value. Specifies the data type into which the parameter value should be parsed.

choices : Limits on values. Specifies the allowed parameter values, only the provided values ​​will be accepted.

required : Whether it is required. If set to True, this parameter is required and an error will be reported if it is not provided.

help : help text. Used to display the help information for this parameter, usually a brief description of the parameter.

metavar : used to display the name of the parameter in the help message.

Among the above parameters, name or flags, type and help are the most commonly used parameters, and other parameters are used as required. In actual use, you can flexibly choose appropriate parameter settings according to your command line parameter requirements.

Next, let us illustrate the usage of argparse through a simple example. Suppose we want to write a simple calculator program that can add, subtract, multiply, and divide two numbers.
Create a new pythonfile called calculator and enter the following code:

import argparse

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

parser = argparse.ArgumentParser(description='Simple calculator')
parser.add_argument('operation', type=str, help='Operation to perform: add, subtract, multiply, divide')
parser.add_argument('num1', type=float, help='First number')
parser.add_argument('num2', type=float, help='Second number')

args = parser.parse_args()

if args.operation == 'add':
    result = add(args.num1, args.num2)
elif args.operation == 'subtract':
    result = subtract(args.num1, args.num2)
elif args.operation == 'multiply':
    result = multiply(args.num1, args.num2)
elif args.operation == 'divide':
    result = divide(args.num1, args.num2)
else:
    print('Invalid operation')
    exit(1)

print('Result:', result)

By running the program, and passing parameters, you can perform different operations. For example, run the following command in the terminal:

python calculator.py add 10 5

Output result:

Result: 15.0

or:

python calculator.py divide 10 5

Output result:

Result: 2.0

Use python calculator.py -h to view the help documentation

python calculator.py -h

show:

usage: calculator.py [-h] operation num1 num2

Simple calculator

positional arguments:
  operation   Operation to perform: add, subtract, multiply, divide
  num1        First number
  num2        Second number

optional arguments:
  -h, --help  show this help message and exit

In this way, users can pass parameters through the command line and perform different calculation operations.

Two, parser.add_argument() function

Sometimes we come across parser.add_argument() code with both long and short names, as shown below:

# 添加一个可选参数"-s"或"--steps",用于指定MCTS步数,默认值为1000,同时提供帮助信息
    parser.add_argument("-s", "--steps", type=int, default=1000, help="Number of MCTS steps"

parser.add_argument() : This is a method in the argparse module for adding a command line argument. By calling this method, we define the rules and properties of the command line arguments we want the program to accept.

" -s " and " --steps ": These two parameters are the names of the command line parameters. "-s" is a short flag, and "--steps" is a long flag. The value of the parameter can be specified by these two flags.

type=int : This is the type specification of the parameter. Here, parameter values ​​will be interpreted as integer types.

default=1000 : This is the default value for the parameter. If no -s or --steps arguments are provided on the command line, the program uses a default value of 1000.

help="Number of MCTS steps ": This is a short description of the parameter that will be displayed when the user uses the --help flag to view the help. Here, the parameter is used to specify the number of MCTS (Monte Carlo Tree Search) steps.

To summarize, this code tells our program that the -s or --steps parameter can be used in the following ways:

-s 2000 or --steps 2000: Specify parameter names and values ​​on the command line.
If this parameter is not specified on the command line, 1000 is used as the parameter value by default.
When the user needs to view help, the description of the parameter will be displayed: "Number of MCTS steps".

In the argparse module, both a short flag (usually a single character, such as "-s") and a long flag (usually a word, such as "--steps") can be defined for a command-line argument. This approach is to facilitate users to specify the same parameter in different ways on the command line, making the command line more flexible and easy to use.

#使用"-s"标志的示例:
python my_script.py -s 2000
#使用"--steps"标志的示例:
python my_script.py --steps 2000

Both ways will set the parameter to 2000. Short flags are generally used for convenient command-line input, while long flags are generally used for more specific and readable option names.

There is no essential difference between defining two flags at the same time, they are just different manifestations in naming and usage. The program recognizes the parameter based on the flags provided by the user and sets its value to the user-specified or default value.

3. About - and - -

When using argparse to parse command-line arguments, argument names usually start with "- -". The purpose of this is to distinguish between command-line arguments and positional arguments.

In the original code, "- -" is not added before the parameter name because this code uses positional arguments (Positional Arguments). Positional parameters do not need the prefix "- -", they are parsed according to the position where the parameter appears in the command line. Create a new Python file called script.py .

#script.py
def parse_args():
    parser = argparse.ArgumentParser("TreEnhance Hyperparams")
    a = parser.add_argument
    a("base_dir", help="BASE DIRECTORY")
    a("expname",help="Name of the run")
    a("dropout", type=float, default=0.6, help="Dropout")
    a("num_images", type=int, default=100, help="number of Images")
    a("num_steps", type=int, default=10, help="number of steps")
    a("val_images", type=int, default=100, help="number of val images")
    a("lr", type=float, default=0.001, help="learning rate")
    a("size", type=int, default=256, help="image size")
    a("num_gen", type=float, default=256, help="number of generation")
    a("bs", type=int, default=16, help="batch size")
    a("lambd", type=int, default=20, help="lambda in the loss function")
    a("loops", type=int, default=5, help="number of optimization loops")
    return parser.parse_args()

For example, when you run this script, you need to provide the corresponding values ​​​​in the order of function parameters, you can run it like this:

python my_script.py ./path/to/base_dir my_expname 0.6 100 10 100 0.001 256 256 16 20 5

In the command line above, the corresponding values ​​are provided in the order of the arguments to the function parse_args(). These values ​​will be assigned to the corresponding parameters according to the order of the parameters. The assignment results are as follows:
base_dir: ./path/to/base_dir
expname: my_expname
dropout: 0.6
num_images: 100 and
so on
However, in order to improve readability and be more flexible To specify parameters, it is generally recommended to use long format command line parameters, that is, add "-" before the parameter name, as shown in the following code:

#script.py
def parse_args():
    parser = argparse.ArgumentParser("TreEnhance Hyperparams")
    a = parser.add_argument
    a("--basedir", nargs='?', default='./', help="BASE DIRECTORY")
    a("--expname", default="default_exp", help="Name of the run")
    a("--dropout", type=float, default=0.6, help="Dropout")
    a("--num_images", type=int, default=100, help="number of Images")
    a("--num_steps", type=int, default=100, help="number of steps")  # 每个epoch训练的迭代步数
    a("--val_images", type=int, default=100, help="number of val images")
    a("--lr", type=float, default=0.001, help="learning rate")
    a("--size", type=int, default=256, help="image size")
    a("--num_gen", type=float, default=50, help="number of generation")  # 相当于训练的epoch数,256
    a("--bs", type=int, default=8, help="batch size")
    a("--lambd", type=int, default=20, help="lambda in the loss function")
    a("--loops", type=int, default=5, help="number of optimization loops")
    return parser.parse_args()

Use the long form of command-line parameters to specify parameter values ​​on the command line without being in a particular order. This is more readable and allows optional arguments on the command line, you can run:

python my_script.py --basedir /path/to/base_dir --expname my_expname --dropout 0.6 --num_images 100 --num_steps 10 --val_images 100 --lr 0.001 --size 256 --num_gen 256 --bs 16 --lambd 20 --loops 5

To sum up, the purpose of providing both short flags and long flags for command line parameters is to increase the flexibility and readability of the command line, so that users can choose an appropriate way to specify parameter values ​​according to their preferences and needs.

4. The difference between args = parser.parse_args() and return parser.parse_args()

args = parser.parse_args() and return parser.parse_args() differ in their return values ​​and purposes.

args = parser.parse_args(): This line of code will call the parser.parse_args() method to parse the command line arguments and store the parsed result in the variable args. args is a Namespace object that contains the parsed command line arguments. You can access the values ​​of these parameters through attributes, such as args.expname, args.num_images, etc.

# 示例
args = parser.parse_args()
print(args.expname)      # 输出:命令行参数中的 expname 参数值
print(args.num_images)   # 输出:命令行参数中的 num_images 参数值

return parser.parse_args(): This line of code will return the result of the parser.parse_args() method in the function. That is, when you call this function, it will parse the command-line arguments, and use the parsed result as the return value of the function. In this way, you can directly get the parsed parameter value when calling the function without assigning it to a variable separately.

# 示例
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--expname", help="Name of the run")
    parser.add_argument("--num_images", type=int, default=100, help="number of Images")
    # 添加其他命令行参数...
    return parser.parse_args()

# 在其他地方调用函数
args = parse_args()
print(args.expname)      # 输出:命令行参数中的 expname 参数值
print(args.num_images)   # 输出:命令行参数中的 num_images 参数值

Summary: The parser.parse_args() method is used to parse command line arguments and store the parsed results in a namespace object. args = parser.parse_args() Assign this namespace object to the variable args, so that you can access the value of the command line parameters through attributes. return parser.parse_args() takes the parsed result as the return value of the function, so that you can directly get the parsed parameter value when calling the function without additional assignment.

Guess you like

Origin blog.csdn.net/thy0000/article/details/131928899