ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è§£æ
from optparse import OptionParser, OptionValueError import os # ã¹ã¯ãªããã®ä½¿ç¨æ¹æ³ã表ãæåå # ããã©ã«ãå¤ã¯"Usage: %prog [options]" # "usage: "ã§å§ã¾ããªãã¨èªåçã«"usage :"ã追å ããã # %progã¯ã¹ã¯ãªããåã§ç½®æ usage = "usage: %prog [options] keyword" # OptionPraserã®ã¤ã³ã¹ã¿ã³ã¹ãçæ parser = OptionParser(usage) # ãªãã·ã§ã³ã®è¿½å # action ãªãã·ã§ã³ãè¦ã¤ãã£ãå ´åã«è¡ãå¦ç # type ãªãã·ã§ã³ã®å # dest å¼æ°ã®ä¿åå # çç¥æã¯é·ããªãã·ã§ã³åãä½¿ç¨ # ãããçç¥ãªãçããªãã·ã§ã³å # default ãªãã·ã§ã³ã®ããã©ã«ãå¤ # çç¥ããå ´åã®ããã©ã«ãå¤ã¯None # metavar ãã«ã表示ã§ä½¿ãããä»®å¼æ° # çç¥æã¯ä¿åå ã®ååã大æåã«ãã¦ä½¿ç¨ parser.add_option( "-f", "--file", # ã©ã¡ããä¸ã¤ã¯å¿ ãå¿ è¦ action="store", # å¼æ°ãä¿åï¼ããã©ã«ãå¤ï¼ type="string", # å¼æ°ããã®ã¾ã¾æååã¨ãã¦ä¿åï¼ããã©ã«ãå¤ï¼ dest="log_file", help="log file" ) parser.add_option( "-l", "--line", type="int", # å¼æ°ãint( )ã§å¤æãã¦ä¿å metavar="N", default=10, help="display search result up to N" ) parser.add_option( "-s", type="choice", # å¼æ°ãchoicesã«å«ã¾ãã¦ããªããã°ã¨ã©ã¼ choices=["Yahoo", "Google", "Wikipedia"], default="Yahoo", metavar="SEARCH ENGINE", help="choose search engine form Yahoo, Google, Wikipedia" ) parser.add_option( "-x", action="append_const", # constã§æå®ããå®æ°ã追å const="X", dest="hoge", default=[], # ãã®å ´åã¯ãªã¹ãã§ãªããã°ã¨ã©ã¼ # ããã©ã«ãå¤ã«[]ãæå®ããªãã¦ãããªãã·ã§ã³ã # è¦ã¤ãã£ãæç¹ã§ä¿åå ã¯[]ã§åæåããã help="meaningless option" ) parser.add_option( "-y", action="append_const", const="Y", dest="hoge", help="meaningless option" ) parser.add_option( "-d", "--debug", action="store_true", # Trueãä¿å # store_falseãªãFalseãä¿å default=False, help="debug" ) def check_directory(option, opt_str, value, parser): if os.path.isdir(value): parser.values.dir = value # ãªãã·ã§ã³ã®å¼æ°ãä¿å else: raise OptionValueError("option -dir: %s is not directory" % value) parser.add_option( "--dir", action="callback", # callbackã§æå®ããé¢æ°ãå¼ã³åºã callback=check_directory, type="string", # å¼æ°ãåããªãå¿ é default="./", dest="dir", help="direcotry to hogehoge" ) # ã³ãã³ãã©ã¤ã³ã®è§£æ # options å ¨ã¦ã®ãªãã·ã§ã³ã®å¤ãå ¥ã£ããªãã¸ã§ã¯ã # args ã³ãã³ãã©ã¤ã³è§£æå¾ã«æ®ã£ãå¼æ° (options, args) = parser.parse_args() # ã¨ã©ã¼å¦ç if not args: # ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããããã°ã©ã ãçµäº parser.error("requires keyword") #parser.print_help() # ãã«ãã¡ãã»ã¼ã¸ã表示 #exit() # ããã°ã©ã ãçµäº # ãªãã·ã§ã³ãåç § # dest="file"ãªãoptions.fileã§åç § # destãçç¥ããã¦ããã°é·ããªãã·ã§ã³åã§åç § # ãããçç¥ããã¦ããã°çããªãã·ã§ã³åã§åç § print "options.log_file =", options.log_file print "options.line =", options.line print "options.s =", options.s print "options.hoge =", options.hoge print "options.debug =", options.debug print "options.dir =", options.dir print "keyword =", args[0]
$ test.py -h Usage: test.py [options] keyword Options: -h, --help show this help message and exit -f LOG_FILE, --file=LOG_FILE log file -l N, --line=N display search result up to N -s SEARCH ENGINE choose search engine form Yahoo, Google, Wikipedia -x meaningless option -y meaningless option -d, --debug debug --dir=DIR direcotry to hogehoge $ test.py python options.log_file = None options.line = 10 options.s = Yahoo options.hoge = [] options.debug = False options.dir = ./ keyword = python $ test.py python -f hoge.txt -l 20 -s Google -x -y -d --dir hoge options.log_file = hoge.txt options.line = 20 options.s = Google options.hoge = ['X', 'Y'] options.debug = True options.dir = hoge keyword = python $ test.py Usage: test.py [options] keyword test.py: error: requires keyword $ test.py python -f Usage: test.py [options] keyword test.py: error: -f option requires an argument $ test.py python -l 20x Usage: test.py [options] keyword test.py: error: option -l: invalid integer value: '20x' $ test.py python -s Goo Usage: test.py [options] keyword test.py: error: option -s: invalid choice: 'Goo' (choose from 'Yahoo', 'Google', 'Wikipedia') $ test.py python --dir hogehoge Usage: test.py [options] keyword test.py: error: option -dir: hogehoge is not directory
詳ããã¯optparserのマニュアルãåç §ãã¦ãã ããã