# -*- coding:utf-8 -*- from optparse import OptionParser parser = OptionParser() parser.add_option("-i", "--input", dest="inputFilename", help="Input file name", metavar="[INPUT]") parser.add_option("-o", "--output", dest="outputFilename", help="Output file name", metavar="[OUTPUT]") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Make lots of noise") (options, args) = parser.parse_args() print "Input file name : %s" % (options.inputFilename) print "Output file name: %s" % (options.outputFilename) print "Verbose : %s" % (options.verbose) # オプションがない場合は help を表示する if options.inputFilename == None: parser.print_help() exit() if options.outputFilename == None: parser.print_help() exit()
実行結果
- オプションなしの場合
$ python test00.py Input file name : None Output file name: None Verbose : False Usage: test00.py [options] Options: -h, --help show this help message and exit -i [INPUT], --input=[INPUT] Input file name -o [OUTPUT], --output=[OUTPUT] Output file name -v, --verbose Make lots of noise
- help オプションを付けた場合
$ python test00.py -h Usage: test00.py [options] Options: -h, --help show this help message and exit -i [INPUT], --input=[INPUT] Input file name -o [OUTPUT], --output=[OUTPUT] Output file name -v, --verbose Make lots of noise
- input, output オプションを付けた場合
$ python test00.py -i in.txt -o out.txt Input file name : in.txt Output file name: out.txt Verbose : False
- verbose オプションも付けた場合
$ python test00.py -i in.txt -o out.txt -v Input file name : in.txt Output file name: out.txt Verbose : True
0 件のコメント:
コメントを投稿