the python argparse module (rpm)

. 3  # - * - Coding: UTF-. 8 - * -   
. 4    
. 5  Import argparse  
 . 6    
. 7 args = " -f -n hello.txt the -X-100. 3. 1 2 -Yb -za args.txt i_am_bar -H @ -q Hello " .split ()   
 . 8  # use = @ args.txt claim fromfile_prefix_chars "@"   
. 9  # args.txt file should be a line parameter, you want to change the behavior of the reference convert_arg_line_to_args ()   
10    
. 11    
12 is  # simple ArgumentParser parameter Description   
13 is  # # Description - command line help text begins, in most cases, we will only use this parameter   
14  # Epilog - command line help text at the end of   
15  #prog - (default: sys.argv [0 ]) in the name of the program, generally do not need modification, addition, if you need help in the name of the program can be used% (PROG) S   
16  # prefix_chars - prefix command, default - such as -f / - file. Some programs may wish to support such an option / f, = you can use prefix_chars "/"   
17  # fromfile_prefix_chars - (default: None) If you want the command-line parameters can be read from a file, it may be used. For example, if there are parameters fromfile_prefix_chars = '@', a command line for the "@ args.txt", content will args.txt as command line arguments   
18  # add_help - whether to increase -h / -help option (default: True) general help information is required, so you do not set it.  
19  # # parents - type list, some options if the parser's parser, like some other options, parents can use to implement inheritance, for example = parents [parent_parser]   
20  # # formatter_class - custom help message format (description and epilog). By default, the help information will be long <wrap and eliminating a plurality of consecutive blank>.  
21  # three allowed values:   
22  #direct output class argparse.RawDescriptionHelpFormatter description and epilog original form (without wrap operation and eliminating blank)   
23 is  # class argparse.RawTextHelpFormatter description and direct output and epilog add_argument original form of help strings (not wrap and eliminate blank operation)   
24-  # # class argparse.ArgumentDefaultsHelpFormatter default output value in helping their corresponding information behind each option, if set, then. The most commonly used now!  
25  # argument_default - (default: None) Set a global default option, usually set each option separately, so this parameter is used less, not elaborate   
26  # usage - (default: Generated) If you need to modify the usage information (usage: PROG [-h] [ --foo [FOO]] bar [bar ...]), you can modify this, generally do not modify.  
27  # conflict_handler - not recommended. This will be used in extreme cases, it is the definition of how to deal with when names of two add_argument added option of conflict, the default handler is throw an exception.  
28  # comment line ## indicates there are several commonly used parameters   
29 parser = argparse.ArgumentParser(description="This is a description of %(prog)s", epilog="This is a epilog of %(prog)s", prefix_chars="-+", fromfile_prefix_chars="@", formatter_class=argparse.ArgumentDefaultsHelpFormatter)  
30   
31 # ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])  
32 # add_argument的参数是比较复杂的。。。  
33   
34 #name or flags - in the form specified parameters, to write several write a few, but we usually write two, a short argument, a long argument, look at the following example "-f", "--file"   
35  # can choose option position is not fixed, like how to write on how to write, the default is optional   
36 parser.add_argument ( " -f " , " --file " , Help = " the Test the Test the Test " )  
 37  # fixed position options such as "prog i_am_bar", this way, then, i_am_bar is the value of friends bar option, the default must be some   
38 parser.add_argument ( " bar " , Help = " the Test the Test the Test " )  
 39    
40  # nargs - specified behind this parameter the number of the value, e.g., we want to use -n 1 2 3 4, n has a value set to [1, 2, 3,  4]  
41parser.add_argument ( " -n " , " --num " , = nargs " + " , of the type = int)  
 42  # Here nargs = "+" that if you specify the -n option, then back at least to keep -n a parameter, + indicates at least one, or represent a 0, * 0 or more,?   
43    
44  # default - if the command line does not have this option appears, then use the default value specified default   
45 parser.add_argument ( " + G " , " ++ Gold " , = Help " Test Test Test " , default = " test_gold " ) # required prefix_chars with" + "  
46   
47 #type - If desired parameter is passed in the specified type (e.g., float, int or file, etc. may be converted into one type from a string), use   
48 parser.add_argument ( " the -X- " , type = int)  
 49    
50  # choices - set the parameter value range, if the choices are not a string type, designated type I remember oh   
51 is parser.add_argument ( " -Y " , choices = [ ' a ' , ' B ' , ' D ' ])  
 52 is    
53 is  # required - usually -f this option is optional, but if required = True then that is a must   
54 parser.add_argument ( " the -z " , choices = [ 'A ' , ' B ' , ' D ' ], required = True)  
 55    
56 is  # metavar - name of the parameter, is used only when displaying help information.   
57 is parser.add_argument ( " -o " , metavar = " OOOOOO " )  
 58    
59  # help - setting this option help   
60  # dest - set the value of this option is that which attributes parsed out into   
61 parser.add_argument ( " -q " , dest = " world " )   
 62    
63 args = Parser .parse_args(args) # If you do not have args parameter, then use sys.argv, which is command-line parameter friends. Have this parameter, you help us debug ah   
64-  # args.world it is -q value   
65    
66  # Action - Action at The Basic of the type of BE taken to the when the this argument at The IS AT encountered the Command Line.   
67  # const - A Constant value Action and by some nargs required Selections.   
68  # The two look at their documentation to help you, more complicated   
69  # http://docs.python.org/library/argparse.html   
70    
71  Print args  
 72   
73  
74 

 

This complex code, help with the final output is (as long as there is -h command-line options will help and exit output oh)

 

usage: argparse_sample.py [-h] [-f FILE] [-n NUM [NUM ...]] [+g GOLD] [-x X]  
                          [-y {a,b,d}] -z {a,b,d} [-o OOOOOO] [-q WORLD]  
                          bar  
   
This is a description of argparse_sample.py  
   
positional arguments:  
  bar                   test test test  
   
optional arguments:  
  -h, --help            show this help message and exit  
  -f FILE, --file FILE  test test test (default: None)  
  -n NUM [NUM ...], --num NUM [NUM ...]  
  +g GOLD, ++gold GOLD  test test test (default: test_gold)  
  -x X  
  -y {a,b,d}  
  -z {a,b,d}  
  -o OOOOOO  
  -q WORLD  
   
This is a epilog of argparse_sample.py  

 

Transfer from http://blog.iamzsx.me/show.html?id=100001

 

Guess you like

Origin www.cnblogs.com/zjf-on/p/12020657.html