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 argparse module

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to use the Python argparse module". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the Python argparse module" can help you solve the problem.

The argparse module makes it easy to write a user-friendly command line interface. The program defines the parameters it needs, and then argparse will figure out how to parse those parameters from sys.argv. The argparse module will also automatically generate help and user manuals and send error messages when the user passes invalid parameters to the program.

Example import argparseparser = argparse.ArgumentParser (description='test') parser.add_argument ('--sparse', action='store_true', default=False, help='GAT with sparse version or not.')''_ StoreTrueAction (option_strings= ['--sparse'], dest='sparse', nargs=0, const=True, default=False, type=None, choices=None, help='GAT with sparse version or not.', metavar=None)''parser.add_argument ('-seed', type=int, default=72) Help='Random seed.')''_ StoreAction (option_strings= ['--seed'], dest='seed', nargs=None, const=None, default=72, type=, choices=None, help='Random seed.', metavar=None)''parser.add_argument ('-- epochs', type=int, default=10000, help='Number of epochs to train.')''_ StoreAction (option_strings= [--epochs'], dest='epochs', nargs=None, const=None, default=10000, type=, choices=None, help='Number of epochs to train.' Metavar=None)''args = parser.parse_args () print (args.sparse) print (args.seed) print (args.epochs)' 'False7210000''' step 1. Instantiate ArgumentParser# instantiate # create an ArgumentParser object # ArgumentParser object contains all the information you need to parse the command line into a Python data type. Parser = argparse.ArgumentParser (description = 'test')

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.

two。 Add parameters using the add_argument function

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

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 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 list of the option string, such as foo or-f,-- foo.

The action of the action- command line when it encounters a parameter. The default value is store.

Store_const- indicates that the assignment is const.

Append- stores the values encountered as a list, that is, if the parameters are duplicated, multiple values are saved.

Append_const- saves a value defined in the parameter specification to a list

Count- stores the number of times encountered; in addition, it can also inherit argparse.Action custom parameter parsing

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

Constant values required by const-action and nargs.

The default value when default- does not specify a parameter.

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

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

Required-whether optional parameters can be omitted (for optional parameters only).

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. 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.

Parsing parameters using parse_args

ArgumentParser parses the parameters through the parse_args () method. It checks the command line, converts each parameter to the appropriate type, and then invokes the appropriate operation. In most cases, this means that a simple Namespace object is built from the properties parsed on the command line:

# parsing the parameter args = parser.parse_args () print (args.echo) about "how to use the Python argparse module" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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