Learn python argparse module download install and use

Source: Internet
Author: User

Learn python argparse module download install and use

The Argparse module in Python replaces the previous Optparse module and is often used to parse command-line arguments and options.

Position parameters:

Start with one of the most basic programs (it does not implement any realistic functionality):

Import Argparse
Parser = Argparse. Argumentparser ()
Parser.parse_args ()

Here is the result after the run:

$ python prog.py
$ python prog.py--help
usage:prog.py [-h]optional arguments:
-H,--help show this help message and exit$ python prog.py--verbose
usage:prog.py [-h]prog.py:error:unrecognized arguments:--verbose
$ Python prog.py foo
usage:prog.py [-h]prog.py:error:unrecognized Arguments:foo

Null when no arguments are added

"--help" was added to the command line on the second execution, and a friendly help message was obtained.

The--HELP option can also be abbreviated to-H, which is the only parameter that can be used without our setting, and any other parameter will be an error.



Example:

Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument ("echo")
args = Parser.parse_args ()
Print Args.echo

Operation Result:

$ python prog.py
usage:prog.py [-H] Echo
Prog.py:error:the following arguments is Required:echo
$ python prog.py--help
usage:prog.py [-H] Echo

Positional Arguments:echo

Optional arguments:
-H,--help show this help message and exit
$ Python prog.py foo
Foo

We added the Add_argument () method, which specifies which command-line arguments the program accepts. In this example, we define it as echo.

Now we need to specify an option when invoking the program.

The Parse_args () method actually returns some data from our command-line arguments, which in the example above is the Echo

Although the auto-generated help information is beautifully presented, it is still not possible to know exactly what it is doing based on the echo parameter. So we're going to make it a little more practical.

Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument ("echo", help= "echo the string,")
args = Parser.parse_args ()
Print Args.echo

Run again:

$ python prog.py-h
usage:prog.py [-H] Echo
Positional arguments:
echo echo the string

Optional arguments:
-H,--help show this help message and exit

Now add some more:

Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument ("Square", help= "display a square of a given number")
args = Parser.parse_args ()
Print args.square**2

Results:

$ python prog.py 4
Traceback (most recent):
File "prog.py", line 5, <module>
Print args.square**2
typeerror:unsupported operand type (s) for * * or POW (): ' str ' and ' int '

Error after operation. This is because by default Argparse takes the arguments we pass as string, so in this case we need to tell it that we are going to pass an integer-type argument.

Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument ("Square", help= "display a square of a given number", Type=int)
args = Parser.parse_args ()
Print args.square**2

Operation Result:

$ python prog.py 4
16
$ python prog.py Four
usage:prog.py [-h] Square
prog.py:error:argument square:invalid int value: ' Four '

Now, the program runs smoothly and can handle some of the wrong inputs.

Manuscripts: Diligent Learning qxkue.net

Extended reading:

Learn python argparse module download Install and use (1)

Learn python argparse module download Install and use (2)

Learn python argparse module download Install and use (3)

Learn python argparse module download Install and use (4)

Learn python argparse module download install and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.