Python Learning argparse Module

Source: Internet
Author: User

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: Import the module first, then create a parse object, and 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 follow, and the last call to the Parse_args () method to parse it; , the following is a brief explanation of steps 2 and 3.

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

1 ImportArgparse2 3 defParse_args ():4Description = Usage:%prog [options] poetry-file5 6This isthe Slow Poetry Server, blocking edition.7 Run It like this:8 9 python slowpoetry.pyTen  OneIf you areinchThe base directory of the twisted-Intro Package, A You could run it as this: -  -Python blocking-server/slowpoetry.py poetry/Ecstasy.txt the  -To serve up John donne's Ecstasy, which I know you want to do. -  -  +Parser = Argparse. Argumentparser (Description =description) -      +Help =The addresses to connect. AParser.add_argument ('addresses', Nargs ='*', help =Help ) at  -Help = the filename to operate on. Default ispoetry/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) in  -Help = The interface-listen on. Default islocalhost. toParser.add_argument ('--iface', Help=help, default='localhost') +  -Help =The number of seconds between sending bytes. theParser.add_argument ('--delay', Type=float, Help=help, default=.7) *  $Help =The number of bytes to send at a time.Panax NotoginsengParser.add_argument ('--bytes', Type=int, Help=help, default=10) -  theargs =Parser.parse_args (); +     returnargs A  the if __name__=='__main__': +args =Parse_args () -      $      forAddressinchargs.addresses: $         Print 'The address is:%s.'%Address -      -     Print 'The filename is:%s.'%Args.filename the     Print 'The port is:%d.'%Args.port -     Print 'The interface is:%s.'%Args.ifaceWuyi     Print 'The number of seconds between sending bytes:%f'%Args.delay the     Print '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

Related Article

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.