This article mainly introduces the usage of the argparse module in python, and analyzes in detail the usage tips of the argparse module in the form of an instance, for more information, see the example in this article to describe how to use the argparse module in python. Share it with you for your reference. The specific analysis is as follows:
Parameters are often included when writing a command line tool, so it is implemented using argparse in python.
#-*-Coding: UTF-8-*-import argparse args = "-f hello.txt-n 1 2 3-x 100-y B-z a-q hello @args.txt I _am_bar-h ". split () # Use @ args.txt to require a parameter in the fromfile_prefix_chars = "@" # args.txt file. To change the behavior, see convert_arg_line_to_args () # Brief description of the ArgumentParser parameter # description-start Text of the command line help, in most cases, we only use this parameter # epilog-end Text of the command line help # prog-(default: sys. argv [0]) name of the program, which generally does not need to be modified. In addition, if you need to use the program name in help, you can use % (prog) s # pre Fix_chars-command prefix. The default value is-, for example,-f/-- file. Some programs may want to support options such as/f. You can use prefix_chars = "/" # fromfile_prefix_chars-(default: None). If you want the command line parameters to be read from the file, it may be used. For example, if fromfile_prefix_chars = '@' and the command line parameter contains "@args.txt", the contents of args.txt will be used as the command line parameter # add_help-whether to add the-h/-help option (default: true). Generally, the help information is required, so you do not need to set it. # Parents-the type is list. If some parser options are the same as those of some other parser options, parents can be used for inheritance, for example, parents = [parent_parser] # formatter_class-custom help information format (description and epilog ). By default, long help information is
<自动换行和消除多个连续空白>
. # Three allowed values: # class argparse. rawDescriptionHelpFormatter directly outputs the original format of description and epilog (no line breaks or blank elimination operations) # class argparse. rawTextHelpFormatter directly outputs the original form of the help string in description, epilog, and add_argument (do not perform automatic line breaks or empty spaces) # class argparse. argumentdefashelshelpformatter outputs the corresponding default values after the help information of each option, if any. This is the most common one! # Argument_default-(default: None) sets the default value of a global option. Generally, each option is set separately, so this parameter is rarely used. Do not elaborate # usage-(default: generated) if you need to modify the usage information (usage: PROG [-h] [-- foo [FOO] bar [bar...]), this can be modified. Do not modify it. # Conflict_handler-not recommended. This is used in extreme cases. It is mainly used to define how the options added in the two add_argument statements are handled in case of a conflict of names. By default, an exception is thrown. # Comment on a row ## it indicates that these parameters are commonly used as parser = argparse. argumentParser (description = "This is a description of % (prog) s", epilog = "This is a epilog of % (prog) s", prefix_chars = "-+ ", fromfile_prefix_chars = "@", formatter_class = argparse. argumentdefashelshelpformatter) # ArgumentParser. add_argument (name or flags... [, action] [, nargs] [, const] [, default] [, type] [, choices] [, required] [, help] [, metavar] [, dest]) # add_argume Nt parameters are complex... # Name or flags-specify the parameter format and write a few values. However, we generally write two values: one short parameter and the other long parameter. For more information, see the following example: "-f ", "-- file" # optional option. The location is not fixed. You can write the file if you want to write it. The default option is parser. add_argument ("-f", "-- file", help = "test") # fixed location options, such as "prog I _am_bar, I _am_bar is the value of the bar option. It is a required parser by default. add_argument ("bar", help = "test") # nargs-specify the number of values after this parameter. For example, we want to use-n 1 2 3 4, to set the value of n to [1, 2, 3, 4] parser. add_argument ("-n", "-- num", nargs = "+", type = in T) # Here, nargs = "+" indicates that if you specify the-n option,-n must be followed by at least one parameter, + indicates at least one ,? It indicates one or zero, * zero or more. # default-if this option is not displayed in the command line, use the default parser specified by default. add_argument ("+ g", "++ gold", help = "test", default = "test_gold ") # prefix_chars is required to include "+" # type-if the parameter to be passed in is of the specified type (for example, float, int or file, and Other types that can be converted from strings ), you can use parser. add_argument ("-x", type = int) # choices-set the parameter value range. If the type in choices is not a string, specify the type parser. add_argument ("-y", choices = ['A', 'B', 'D']) # required-normally-f is optional, but if requi If red = True, parser is required. add_argument ("-z", choices = ['A', 'B', 'D'], required = True) # metavar-Parameter Name, it is used only when the help information is displayed. parser. add_argument ("-o", metavar = "OOOOOO") # help-set the help information for this option # dest-set the value of this option to the parser attribute to which it is parsed. add_argument ("-q", dest = "world") args = parser. parse_args (args) # If you do not have the args parameter, use sys. argv, that is, the command line parameter. With this parameter, we can easily debug it # args. world is The value of-q # action-the basic type of action to be taken when this argument is encountered at The command line. # const-A constant value required by some action and nargs selections. # These two help documents are complex # http://docs.python.org/library/argparse.html Print args
I hope this article will help you with Python programming.