Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Python command line parser argparse

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Python command line parser argparse how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python command line parser argparse how to use" it!

Chapter 1 introduction to argparse 1.1 Analysis

Argparse module is a built-in Python module for command item options and parameter parsing, and the argparse module makes it easy to write a user-friendly command line interface.

Argparse can help programmers define parameters for the model and parse command-line parameters through sys.argv. The module will also automatically generate help and user manuals and send error messages when the user passes invalid parameters to the program.

1.2 three steps for argparse definition

(1) create a command line parser object-- create an ArgumentParser () object

(2) add command line parameters to the parser-- call the add_argument () method to add parameters

(3) parse the parameters on the command line-- use parse_args () to parse the added parameters

1.3 Code example # Import library import argparse # 1. Define the command line parser object parser = argparse.ArgumentParser (description='test') # 2. Add command line parameter parser.add_argument ('--sparse', action='store_true', default=False, help='GAT with sparse version or not.') parser.add_argument ('--seed', type=int, default=72, help='Random seed.') parser.add_argument ('--epochs', type=int, default=10000, help='Number of epochs to train.') # 3. From the command line parsing parameter args = parser.parse_args () print (args.sparse) print (args.seed) print (args.epochs) Chapter 2 Parameter detailed explanation 2.1 create a command line parser object: ArgumentParser ()

The first step in using argparse is to create an ArgumentParser object:

Parser = argparse.ArgumentParser (description='test')

The ArgumentParser instantiated object will contain all the information needed to parse the command line into a Python data type

(1) describe description

Most calls to the ArgumentParser constructor use the description= keyword argument.

This parameter briefly describes what to do and how to do it to this extent.

In the help message, this description is displayed between the command line usage string and the help messages for various parameters.

2.2 add parameters to the command line: add_argument () method

(1) add a case of command line parameters

Adding program parameter information to an ArgumentParser is done by calling the add_argument () method.

Typically, these calls specify how ArgumentParser takes a command-line string and converts it to an object.

This information is stored in the ArgumentParser instantiated object when parse_args () is called for later use.

For example:

Parser.add_argument ('--sparse', action='store_true', default=False, help='GAT with sparse version or not.') parser.add_argument ('--seed', type=int, default=72, help='Random seed.') parser.add_argument ('--epochs', type=int, default=10000, help='Number of epochs to train.')

The add_argument () method defines how command-line arguments are parsed.

(2) the add_argument () method defines how to parse command line arguments.

ArgumentParser.add_argument (name or flags... [, action] [, nargs] [, const] [, default] [, type] [

Choices] [, required] [, help] [, metavar] [, dest])

Each parameter is explained as follows:

Name or flags-the name or label of a normal parameter or flag parameter option parameter, such as foo or-f,-- foo. The Flag parameter does not need to specify a parameter value, just with the parameter name.

Action-the action when the command line encounters the flags parameter. There are two common actions, store_true: set the flag parameter to true;store_false and set the flag parameter to False.

Nargs-the number of command line arguments that should be read, can it be a specific number, or is it? Sign, use default for Positional argument when no value is specified, const; or * sign for Optional argument for 0 or more parameters, or + sign for 1 or more parameters.

Default-the default value of the parameter when no parameter is specified.

Type-the data type to which the command line arguments should be converted.

Required-whether it is a required or optional parameter.

Help-help information for the parameter. When specified as argparse.SUPPRESS, the help information for the parameter is not displayed.

Metavar-the parameter name in the usage description. The default is the parameter name for required parameters and all uppercase for optional parameters.

Dest-the parsed parameter name. By default, the longest name for the optional parameter is converted from underscore to underscore.

Choices-A container of values that are allowed for the parameter.

Constant values required by const-action and nargs.

Store_const, which indicates that the assignment is const

Append, which stores the values encountered as a list, that is, if the parameters are duplicated, multiple values will be saved

Append_const, which saves a value defined in the parameter specification to a list

Count, which stores the number of encounters; in addition, it can also inherit argparse.Action custom parameter resolution

2.3Parse command line parameters: parse_args ()

The ArgumentParser object parses the parameters of the command line through the parse_args () method.

It examines each parameter on the command line, converts it to the appropriate data type, invokes the appropriate operation, and structures the parameter into the object args.

Args = parser.parse_args ()

In scripts, parse_args () is usually called without arguments, while ArgumentParser automatically determines command-line arguments from sys.argv.

2.4 input of command line parameters

Xxx-sparse-seed 0-epochs 1000

Where-- seed and-- epochs are common parameters and you need to specify specific data.

-- sparse is a flag parameter, and you do not need to specify a specific value. The specific value after the parameter name depends on the definition of its action.

If action = store_true,-- sparse indicates that the parameter value is set to true

If action = store_false,-- sparse indicates that the parameter value is set to false

Print (args.sparse) print (args.seed) print (args.epochs) Thank you for reading, this is the content of "how to use the Python command line parser argparse". After the study of this article, I believe you have a deeper understanding of how to use the Python command line parser argparse, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report