Introduction to Python programming argparse, pythonargparse
This article focuses on the Python programming argparse content, the specific introduction is as follows.
# Aaa. py # version 3.5 import OS # This sentence is useless. I don't know why markdown does not add this sentence when editing the code, the Code cannot be highlighted [Khan] import argparseparser = argparse. argumentParser (description = 'process some integers... ') # initialize a analyzer # parser. add_argument (parameter in) #__ init _ (self, option_strings, dest, nargs = None, const = None, default = None, type = None, choices = None, required = False, help = None, metavar = None) parser. add_argument ('integers', metavar = 'n', type = int, nargs = '+', help = 'an integer for the accumulator ') # This is an add [Location Parameter] # The first parameter is the custom parameter name, which is used for calculation in the Code (parser. parse_args (). integers * 2) parser. add_argument ('-- Sum', dest = 'accumulate', action = 'store _ const', const = sum, default = max, help = 'sum the integers (default: find the max) ') # This is an optional parameter. # the first parameter is a custom parameter. [Use parser in code. parse_args (). sum] [use in the system command line:> python aaa. py -- sumargs = parser. parse_args () print (args) # Namespace (accumulate = <built-in function sum>, integers2 = [1, 2, 3, 4]) print (args. integers) # integers must correspond to the preceding print (args. accumulate (args. integers) # accumulate must correspond to the above
The following is the result of a parameter call in the system command line:
D: \ Program Files (x86) \ Python35> python aaa. py-h
Usage: aaa. py [-h] [-- sum] N [N...]
Process some integers...
Positional arguments:
N an integer for the accumulator
Optional arguments:
-H, -- help show this help message and exit
-- Sum the integers (default: find the max)
D: \ Program Files (x86) \ Python35> python aaa. py 1 2 3 4 -- sum
Namespace (accumulate = <built-in function sum>, integers2 = [1, 2, 3, 4])
[1, 2, 3, 4]
10
D: \ Program Files (x86) \ Python35> python aaa. py 1 2 3 4
Namespace (accumulate = <built-in function max>, integers2 = [1, 2, 4])
[1, 2, 3, 4]
4
The running result in python interactive mode is as follows:
Attachment
Keyword Arguments:
|
|-Option_strings -- A list of command-line option strings which
| Shocould be associated with this action.
|
|-Dest -- The name of the attribute to hold the created object (s)
|
|-Nargs -- The number of command-line arguments that shocould be
| Consumed. By default, one argument will be consumed and a single
| Value will be produced. Other values include:
|-N (an integer) consumes N arguments (and produces a list)
| -'? 'Sumes zero or one arguments
|-'*' Consumes zero or more arguments (and produces a list)
|-'+' Consumes one or more arguments (and produces a list)
| Note that the difference between the default and nargs = 1 is that
| With the default, a single value will be produced, while
| Nargs = 1, a list containing a single value will be produced.
|
|-Const -- The value to be produced if the option is specified and
| Option uses an action that takes no values.
|
|-Default -- The value to be produced if the option is not specified.
|
|-Type -- A callable that accepts a single string argument, and
| Returns the converted value. The standard Python types str, int,
| Float, and complex are useful examples of such callables. If None,
| Str is used.
|
|-Choices -- A container of values that shoshould be allowed. If not None,
| After a command-line argument has been converted to the appropriate
| Type, an exception will be raised if it is not a member of this
| Collection.
|
|-Required -- True if the action must always be specified at
| Command line. This is only meaningful for optional command-line
| Arguments.
|
|-Help -- The help string describing the argument.
|
|-Metavar -- The name to be used for the option's argument with
| Help string. If None, the 'dest' value will be used as the name.
Summary
The above is all about the introduction to Python programming argparse, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!