Python Learning argparse Module

Source: Internet
Author: User

Here are just a few of the common uses, specific principles and detailed explanations of the website's documentation:


First, Introduction:

Argparse is a standard module used by Python to parse command-line arguments and options in place of obsolete optparse modules. The Argparse module is used to parse command-line arguments, such as Python parsetest.py input.txt output.txt--user=name--port=8080.

Second, the use of the steps:

1:import Argparse

2:parser = Argparse. Argumentparser ()

3:parser.add_argument ()

4:Parser.parse_args ()

explanation: First import the module, then create a parse object, then add the command line arguments and options you want to follow to the object, each Add_argument method corresponds to a parameter or option that you want to focus on, and finally call Parse_args ( ) method, which can be used after successful parsing, briefly describe steps 2 and 3 below.

Third, Method Argumentparser (Prog=none, Usage=none,description=none, Epilog=none, Parents=[],formatter_class=argparse. Helpformatter, prefix_chars= '-', Fromfile_prefix_chars=none, argument_default=none,conflict_handler= ' error ', add_ Help=true)

These parameters all have default values, which are printed when parser.print_help () is called or when the program is run because the parameter is incorrect (the Python interpreter actually calls the Pring_help () method). It is generally only necessary to pass the description parameter, as above.

Iv. Method Add_argument (Name or flags ...) [, Action] [, Nargs] [, Const] [, Default] [, type] [, Choices] [, Required] [, help] [, Metavar] [, Dest])

which

Name or Flags: command-line parameter name or option, such as address above or-p,--port. If the command line argument is not given and no defualt is set, an error occurs. However, if it is an option, it is set to none

Nargs: The number of command-line arguments, generally using wildcard notation, where, '? ' means only one, ' * ' means 0 to more, ' + ' means at least one

Default: Defaults

Type: The types of parameters, which are string by default, type float, int, and so on

Help: Similar to the parameters in the Argumentparser method, and the same situations arise


These are the most commonly used places, and others can be found in the official documentation. An example is given below, which basically includes a common scenario:

Import Argparsedef Parse_args (): Description = "" "Usage:%prog [options] Poetry-filethis is the Slow poetry Server, Blo cking edition. Run it like This:python slowpoetry.py <path-to-poetry-file>if is in the base directory of the Twisted-intro p Ackage,you could run it like This:python blocking-server/slowpoetry.py Poetry/ecstasy.txtto serve up John donne ' s Ecstas    Y, which I know you want to do. "" Parser = Argparse.    Argumentparser (Description = description) help = "The addresses to connect." Parser.add_argument (' addresses ', Nargs = ' * ', help = help) help = "The filename to operate on. Default is Poetry/ecstasy.txt "parser.add_argument (' filename ', help=help) help =" The port to listen on.    Default to a random available port. " Parser.add_argument ('-P ',--Port ', Type=int, help=help) help = "The interface-listen on."    Default is localhost. " Parser.add_argument ('--iface ', help=help, default= ' localhost ') help = "The number of seconds between sendingBytes. "    Parser.add_argument ('--delay ', Type=float, Help=help, default=.7) help = "The number of bytes to send at a time."    Parser.add_argument ('--bytes ', Type=int, Help=help, default=10) args = Parser.parse_args (); return argsif __name__ = = ' __main__ ': args = Parse_args () for address in Args.addresses:print ' The address is:%s. '% addr Essprint ' The filename is:%s. '% Args.filenameprint ' The port was:%d. '% Args.portprint ' The interface is:%s. '% arg S.ifaceprint ' The number of seconds between sending bytes:%f '% Args.delayprint ' the number of bytes to send at a time: %d. '% args.bytes
Run the script: Python test.py--port 10000--delay 1.2 127.0.0.1 172.16.55.67 poetry/ecstasy.txt

The output is:

the address is:127.0.0.1.
The address is:172.16.55.67.
The filename is:poetry/ecstasy.txt.
The Port is:10000.
the interface is:localhost.
The number of seconds between sending bytes:1.200000
the number of bytes to send at a time:10.


Python Learning argparse Module

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.