1 OptionParser模塊 2 3 -------------- 代碼說明 ----------------------------------------- 4 簡單流程 5 1,首先,必須 import OptionParser 類,創建一個 OptionParser 對象: 6 7 from optparse import OptionParser 8 parser = OptionParser() 9 10 11 2,add_option() 方法來, 來定義命令行參數: 12 parser.add_option("-f", "--file", ...) 13 說明: 14 2.1 -f 或者 –file 分別是長短參數名: 15 2.2 Actions 參數: 16 它指示 optparse 當解析到一個命令行參數時該如何處理。 17 18 例如: 19 parser.add_option("-f", "--file", action="store", type="string", dest="filename") 20 ## 執行腳本時 -f "1.txt" 21 則會將 "1.txt" 字符串保存在變量filename中, 22 ## 執行本時,如果沒有-f 23 則filename的值為None 或者默認值 24 25 26 #action 參數取值:“store” (默認), “tore_true” 和 “store_false” 27 ##(store_true 和 store_false ,用於處理帶命令行參數后面不 帶值的情況。) 28 ## 即后面帶不帶參數是根據此項來配置的, 29 30 例如: 31 parser.add_option("-v", action="store_true", dest="verbose") 32 parser.add_option("-q", action="store_false", dest="verbose") 33 34 #當解析到 ‘-v’,options.verbose 將被賦予 True 值,反之,解析到 ‘-q’,會被賦予 False 值, 35 ##如果沒有使用選項“-v”或者'-q' 則存儲變量的值為none 36 如果執行腳本時帶了選項,則verbose變量值為,為“tore_true” 和 “store_false” 中的Ture或False 否則 就為默認值 或者 None 37 38 39 例如: 40 41 parser.add_option( 42 "-f", "--file", # 操作指令 43 action="store", ## 表明選項后面是要有值的 44 dest="filename", # 存儲的變量 45 type="string", # 變量類型(一般默認的也是此類型的) 46 default='AAAA', 47 help="write report to FILE", # 顯示的幫助信息 48 metavar="FILE" # 存儲變量的值 49 ) 50 51 52 (options, args) = parser.parse_args() 53 54 55 if options.filename is True: 56 print 'filename is true' 57 if options.filename is False: 58 print 'filename is False' 59 60 61 62 63 64 2.3 type參數 65 type 參數取值:“string” (默認), "int" "float"... 66 67 68 2.4 dest參數 69 dest 參數取值,即選項 取值將要保存在哪個變量中,(即變量名) #如果沒有指定 dest 參數,將用命令行的參數名來對 options 對象的值進行存取。 70 71 72 2.5 default 參數 73 即 選項的的默認取值 74 功能:用於設置默認值。 75 例如: 76 parser.add_option("-f","--file", action="store", dest="filename", default="foo.txt") 77 parser.add_option("-v", action="store_true", dest="verbose", default=True) 78 79 80 81 3,調用 parse_args() 來解析程序的命令行: 82 (options, args) = parser.parse_args() 83 84 說明; 85 parse_args() 返回的兩個值: 86 options,它是一個對象(optpars.Values),保存有命令行參數值。只要知道命令行參數名,如 file,就可以訪問其對應的值: options.file 。 87 args,它是一個由 positional arguments 組成的列表。 88 89 90 4, add_option() 方法中的-h 選項 91 功能: 92 自動生成程序的幫助信息 ## 當 optparse 解析到 -h 或者 –help 命令行參數時,會調用 parser.print_help() 打印程序的幫助信息: 93 94 例如: 95 usage = "usage: %prog [options] arg1 arg2" 96 parser = OptionParser(usage=usage) 97 parser.add_option("-v", "--verbose", 98 action="store_true", dest="verbose", default=True, 99 help="make lots of noise [default]") 100 parser.add_option("-q", "--quiet", 101 action="store_false", dest="verbose", 102 help="be vewwy quiet (I'm hunting wabbits)") 103 parser.add_option("-f", "--filename", 104 metavar="FILE", help="write output to FILE"), 105 parser.add_option("-m", "--mode", 106 default="intermediate", 107 help="interaction mode: novice, intermediate, " 108 "or expert [default: %default]") 109 110 111 ############################################################################### 112 113 114 115 ############################################### 116 實戰用例 117 118 ##導入模塊 119 from optparse import OptionParser 120 ##創建實例對象 121 parser = OptionParser() 122 123 124 ##添加選項 125 parser.add_option( 126 "-p", "--pdbk", ## -f 或者 –file 分別是長短參數名 127 action="store", # 指示 optparse 當解析到一個命令行參數時該如何處理 128 dest="pdcl", # 存儲的變量 129 default=False, 130 help="write pdbk data to oracle db" 131 ) 132 133 ##添加選項 134 parser.add_option( 135 "-z", "--zdbk", 136 action="store_true", 137 dest="zdcl", # 存儲的變量 138 default=False, 139 help="write zdbk data to oracle db" 140 ) 141 142 ##添加選項 143 parser.add_option( 144 "-f", "--file", # 操作指令 145 action="store", 146 dest="filename", # 存儲的變量 147 type="string", # 變量類型 默認地,type 為'string' 148 help="write report to FILE", # 顯示的幫助信息 149 metavar="FILE" # 存儲變量的值 150 ) 151 152 #添加選項 153 parser.add_option( 154 "-q", "--quiet", 155 action="store_false", 156 dest="verbose", 157 default=True, 158 help="don't print status messages to stdout" 159 ) 160 161 ##解析選項 162 (options, args) = parser.parse_args() 163 164 ##使用選項變量 165 if options.pdcl is True: 166 print 'pdcl is true' 167 print("pdcl={0}".format(options.pdcl)) 168 if options.zdcl is True: 169 print 'zdcl is true' 170 if options.filename is not None: 171 print("filename={0}".format(options.filename)) 172 173 print(args) 174 175 通過對上面三種參數解析策略的說明,可以看到這里使用OptionParser模塊進行解析是最佳方式。 176 177 ########## 另種方法 ##################################################################### 178 getopt模塊 179 getopt模塊是專門處理命令行參數的模塊,用於獲取命令行選項和參數,也就是sys.argv。 180 命令行選項使得程序的參數更加靈活。支持短選項模式(-)和長選項模式(--)。 181 182 183 該模塊提供了兩個方法及一個異常處理來解析命令行參數。 184 getopt.getopt 185 方法用於解析命令行參數列表, 186 語法格式如下: 187 getopt.getopt(args, options[, long_options]) 188 189 方法參數說明: 190 args: 要解析的命令行參數列表。 191 options: 以列表的格式定義,options后的冒號(:)表示該選項必須有附加的參數,不帶冒號表示該選項不附加參數。 192 long_options: 以字符串的格式定義,long_options 后的等號(=)表示如果設置該選項,必須有附加的參數,否則就不附加參數。 193 該方法返回值由兩個元素組成: 第一個是 (option, value) 元組的列表。 第二個是參數列表,包含那些沒有'-'或'--'的參數。
