0

I do have following script to capture the command line arguments. Third option for batch size is not being set any idea ? FYI : Only input & ouput file options are required parameter. Batch is optional.

import sys, getopt
import os
try:
    opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])
except getopt.GetoptError:
    print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
    sys.exit(2)
for opt, arg in opts:
    print opt
    if opt == '-h':
        print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
        sys.exit()
    elif opt in ("-i", "--ifile"):
        inputFile = arg
    elif opt in ("-o", "--ofile"):
        outputFile = arg
    elif opt in ("-b", "--bsize"):
        print "Another option %s" % opt
        batchsize = arg 
4

2 回答 2

2

这个脚本有几个问题,包括缩进错误和未能首先初始化设置......但我们假设你只是错误地复制/粘贴了它。

答案是您在 getopt 参数中的“b”之后缺少一个“:”。

这一行:

opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])

实际上应该是:

opts, args = getopt.getopt(argv,"hi:o:b:",["ifile=","ofile=","bsize="])
于 2013-05-24T18:46:12.573 回答
1

如果您不介意,我会推荐argparse。它更好用,而且不像 getopts 那样笨重。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--ifile", required=True)
parser.add_argument("-o", "--ofile", required=True)
parser.add_argument("-b", "--bsize", type=int, default=20000000)
parser.parse_args()
input_file = parser.input

等等

于 2013-05-24T18:50:05.910 回答