Home > Backend Development > Python Tutorial > How to use the argparse module to parse command line parameters in Python 2.x

How to use the argparse module to parse command line parameters in Python 2.x

PHPz
Release: 2023-07-30 21:30:34
Original
888 people have browsed it

How to use the argparse module for command line parameter parsing in Python 2.x

Overview:
In Python programming, command line parameters can be used to easily interact with users, and the argparse module is A powerful command line parameter parsing module in the Python standard library. It provides a simple, easy-to-use and flexible way to parse command line parameters, helping us build better command line tools. This article will introduce how to use the argparse module to parse command line parameters in Python 2.x, and attach some code examples.

Install the argparse module:
The argparse module is the standard library of Python 2.7 version, so there is no need to install additional packages. If your Python version is earlier, you can use the following command in the terminal to install the argparse module:

$ pip install argparse
Copy after login

Step 1: Import the argparse module
First, we need to import the argparse module to use the functions and kind. Use the following code to import the argparse module in your Python program:

import argparse
Copy after login

Step 2: Create a parser object
Next, we need to create a parser object. The parser object is used to add the definition of command line parameters and parse the command line parameters. Use the following code to create a parser object in a Python program:

parser = argparse.ArgumentParser(description='命令行工具描述')
Copy after login

When creating a parser object, we can add description information to the command line tool by setting the description parameter.

Step 3: Define command line parameters
Then, we need to define the name, type, default value and other attributes of each command line parameter. We can add the definition of command line parameters by calling the add_argument() method of the parser object. The following are some commonly used parameter types and examples of their usage:

  • Positional parameters

    parser.add_argument('positional_arg', help='这是一个位置参数')
    Copy after login
  • Optional parameters

    parser.add_argument('-o', '--optional_arg', help='这是一个可选参数')
    Copy after login
  • Parameter default value

    parser.add_argument('-d', '--default_arg', default=0, type=int, help='这是一个带有默认值的参数')
    Copy after login
  • Parameter type

    parser.add_argument('-t', '--type_arg', type=float, help='这是一个指定类型的参数')
    Copy after login

Among them, -o and --optional_arg is the short option and long option of the optional parameter, -d and --default_arg in default represents the default value of the parameter, type represents the type of the parameter.

Step 4: Parse the command line parameters
Finally, we need to parse the command line parameters and process them accordingly. We can use the parse_args() method of the parser object to parse the command line parameters. This method will return a namespace object, and we can obtain the value of each parameter through the properties of the object. Use the following code to parse command line arguments in a Python program:

args = parser.parse_args()
Copy after login

After parsing the command line arguments, the args object will contain the value of each argument.

Full example:
The following is a complete example that demonstrates how to use the argparse module to parse command line arguments:

import argparse

# 创建解析器对象
parser = argparse.ArgumentParser(description='这是一个命令行工具的描述')

# 添加解析器的命令行参数
parser.add_argument('positional_arg', help='这是一个位置参数')
parser.add_argument('-o', '--optional_arg', help='这是一个可选参数')
parser.add_argument('-d', '--default_arg', default=0, type=int, help='这是一个带有默认值的参数')
parser.add_argument('-t', '--type_arg', type=float, help='这是一个指定类型的参数')

# 解析命令行参数
args = parser.parse_args()

# 输出命令行参数的值
print('Positional Argument: %s' % args.positional_arg)
print('Optional Argument: %s' % args.optional_arg)
print('Default Argument: %d' % args.default_arg)
print('Type Argument: %f' % args.type_arg)
Copy after login

Assume the above code is saved as args_example.py, we can enter the following command in the terminal to run the script:

$ python args_example.py positional_value -o optional_value -d 10.5 -t 3.14
Copy after login

The output results are as follows:

Positional Argument: positional_value
Optional Argument: optional_value
Default Argument: 10
Type Argument: 3.140000
Copy after login

Summary:
argparseThe module is Python 2. The parsing of command line parameters in x provides a very convenient method. By importing the argparse module, creating a parser object, adding the definition of command line parameters, and processing the command line parameters after parsing them, we can easily write powerful command line tool scripts. I hope the usage examples shown in this article will be helpful to you in understanding and using the argparse module.

The above is the detailed content of How to use the argparse module to parse command line parameters in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template