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