Python command-line arguments

Source: Internet
Author: User

First, getopt

Opts,args = getopt.getopt ([command line argument list], short option, [long list of options]), the third argument can be omitted
A colon after the short option name (:) indicates that the option must have additional parameters. If not:-ABC is equivalent to-a-b-C; If a has:,-ABC is equivalent to-a BC.
An equal sign (=) after the long option name indicates that the option must have additional parameters.
Returns opts and args.
OPTs is a parameter option and its value tuple [('-O ', ' value '), ('--option ', '),...],opts can have duplicates
Args is a command-line input that removes useful arguments [' A1 ', ' A2 ',...]

1 Importgetopt, sys2 3 defMain ():4     Try:5opts, args = Getopt.getopt (sys.argv[1:],"ho:v", [" Help","output="])6     exceptgetopt. Getopterror as err:7         #Print help information and exit:8         PrintSTR (ERR)#Would print something like "option-a not recognized"9 usage ()TenSys.exit (2) OneOutput =None AVERBOSE =False -      forO, ainchopts: -         ifo = ="- v": theVERBOSE =True -         elifOinch("- H","--help"): - usage () - sys.exit () +         elifOinch("- o","--output"): -Output =a +         Else: A             assertFalse,"unhandled option" at     # ... -  - if __name__=="__main__": -Main ()

Second, Argparse

Third, Optparse

2.7 Start scrapping

Iv. docopt

Non-standard library

Python command-line arguments

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.