2014-02-08 23 views
0

我使用getopt從命令行獲取選項。我如何檢查是否設置了選項。我試圖設置一個值爲1並測試它,它的工作原理,但我想知道是否有另一種方法來做到這一點?做一個測試python選項強制性的

def main(argv): 
    inputfile = '' 
    outputfile = '' 
    i = 0 
    o = 0 
    d = 0 
    usage = """ 
Usage: rotate.py [OPTIONS] ... 
    rotate.py -i img1.jpg -o img2.py -d 10 
    rotate.py -i img1.jpg -d 10 

    -h --help    Display this usage message 
    -i --ifile <inputfile>   The file to rotate 
    -o --ofile <outputfile>   The file that will have the rotated image. 
    -d --degres <integer>   The number of degres to rotate 
""" 
    try: 
     opts, args = getopt.getopt(argv,"hi:o:d:",["ifile=","ofile=","degres=","help"]) 
    except getopt.GetoptError: 
     print usage 
     sys.exit(2) 
    for opt, arg in opts: 
     if opt in ("-h", "--help"): 
      print usage 
      sys.exit() 
     elif opt in ("-i", "--ifile"): 
      inputfile = arg 
      i = 1 
     elif opt in ("-o", "--ofile"): 
      outputfile = arg 
      o = 1 
     elif opt in ("-d", "--degres"): 
      degres = arg 
      d = 1 
    if (inputfile in globals()) & (outputfile in globals()) & (degres in globals()): 
     rotate_picture(inputfile, outputfile, degres) 
    elif (inputfile in globals()) & (degres in globals()): 
     rotate_picture(inputfile, inputfile, degres) 
    else: 
     print usage 

if __name__ == "__main__": 
    main(sys.argv[1:]) 
+4

是爲時已晚指出,['argparse'(HTTP: //(從2.7開始的Python的一部分)支持解析強制參數並且更易於使用? –

+0

來自'getopt'文檔:「對C++ getopt()函數不熟悉的用戶或者希望編寫更少代碼並獲得更好幫助和錯誤消息的用戶應該考慮使用argparse模塊,而不是*。」相信我,你會感謝你切換到'argparse'。 – SethMMorton

+0

我已經經歷了argparse,你是對的,它更好:)。謝謝你的提示。 – user2040597

回答

4

你最好將參數設置爲None事前,然後測試它們是否仍然None解析後:

inputfile = outputfile = degres = None 

for opt, arg in opts: 
    if opt in ("-h", "--help"): 
     print usage 
     sys.exit() 
    elif opt in ("-i", "--ifile"): 
     inputfile = arg 
     i = 1 
    elif opt in ("-o", "--ofile"): 
     outputfile = arg 
     o = 1 
    elif opt in ("-d", "--degres"): 
     degres = arg 
     d = 1 

if inputfile is None or degres is None: 
    print usage 
    sys.exit(1) 

outputfile = outputfile or inputfile 

rotate_picture(inputfile, outputfile, degres) 

請注意,您使用andor在Python做布爾測試, &是一個按位 AND運算符;它將整數位組合成新的整數。

的argparse版本是:

import argparse 

parser = argparse.ArgumentParser(description='Rotate an image') 
parser.add_argument('inputfile', type=argparse.FileType('r+b'), 
        help='The file to rotate') 
parser.add_argument('degrees', type=int, 
        help='The number of degrees to rotate') 
parser.add_argument('-o', '--output', type=argparse.FileType('wb'), 
        help='The file to write the result to. Defaults to the inputfile') 

args = parser.parse_args() 
outputfile = args.output or args.inputfile 
rotate_picture(args.inputfile, outputfile, args.degrees) 

和幫助是自動生成的你作爲:

usage: [-h] [-o OUTPUT] inputfile degrees 

Rotate an image 

positional arguments: 
    inputfile    The file to rotate 
    degrees    The number of degrees to rotate 

optional arguments: 
    -h, --help   show this help message and exit 
    -o OUTPUT, --output OUTPUT 
         The file to write the result to. Defaults to the 
         inputfile 
+0

非常感謝您的幫助... – user2040597

+0

argparse非常好,謝謝你... – user2040597