Python optparse nargs选项不适用于整数

时间:2015-11-25 11:12:01

标签: python parsing optparse

我对nargs的{​​{1}}选项有疑问。 这是我的代码:

optparse

我需要能够将代码用作:

def main():
    #variable1 = 'Teste'
    timestr = time.strftime("%Y%m%d%H%M%S")
    if len(sys.argv) == 1:
        prog = os.path.basename(sys.argv[0])
        print("Tool for calculating radiance and reflectance")
        print("for landsat 4,5,7 and 8 dataset.\n")
        print("Nais srl")
        print("Author: Lorenzo Bernardi\n")
        print("usage: %s\n"%sys.argv[0])
        print("-d, --directory  Mandatory. Directory of landsat dataset. Eg. C:\mydir\LT51900311984170XXX05")
        print("-p, --process    Mandatory. Options: [rad, ref or radref] to process radiance, reflectance or both. Default: rad")
        print("-l, --series     Mandatory. Options: [4,5,7 or 8] to specify the Landsat series")
        print("-s, --sensor     Mandatory. Options: [LC, LO, LE, LT] to specify the sensor. Eg. LE=Landsat ETM+")
        print("-b, --bands      Optional. Bands to process")
        print("-o, --output     Optional. Directory where will be saved the radiance and/or reflectance layers")
        print("-v,              Optional. Leave out the end-user control on bands under processing")

        sys.exit(option_error())
    else:
        usage = "usage: %prog [options] "
        parser = optparse.OptionParser(usage=usage)

        parser.add_option("-d", "--directory", dest="directory", action="store", type="string", \
                          help="complete path of landsat product folder: mydir/filename/")

        parser.add_option("-p", "--process", dest="operation", action="store", type="choice", \
                          help="process requested: radiance, reflectance, both", choices=['rad', 'ref', 'radref'], default='rad')

        parser.add_option("-l", "--series", dest="satellite", action="store", type="choice", \
                          help="Landsat series:4, 5, 7, 8", choices=['4', '5', '7', '8'])

        parser.add_option("-s", "--sensor", dest="sensor", action="store", type="choice", \
                          help="sensor acronymous, for example LO for Landsat OLI, or LE for Landsat ETM+, etc..", choices=['LC', 'LO', 'LE', 'LT'], default=None)

        parser.add_option("-o", "--output",   dest="output", type="string", \
                help="Directory of output raster. \n \
                    Unless specified, output directory will be workDirectory/results/datafolder/. \n \
                    If specified, the output directory wil be mydirectory/results/filename/rad (and/or ref)/", default=None)

        parser.add_option("-x", action="store_true", dest="bool", help="activate iterative radiance and/or reflectance computation for all the bands", default=False)

        parser.add_option("-b", "--bands", dest="bands", action="store", type=int, \
                          help="bands to process", nargs='*', default=None)

        (options, args) = parser.parse_args()

bla bla bla -b 1 2 3

或没有-b标志(默认情况下)。

错误是:

bla bla bla -b 1

有人可以建议一些解决方案吗?

1 个答案:

答案 0 :(得分:0)

optparse模块不支持将'*'指定为nargs参数的有效值。必须指定一个整数,如official documentation所暗示的那样:

  

Option.nargs(默认值:1)

     

当看到此选项时,应使用多少type类型的参数。如果> 1,optparse会将值元组存储到dest

然而,the argparse module does support it

  

'*'。存在的所有命令行参数都被收集到一个列表中。

此外,since version 2.7 the optparse module is deprecated in favor of the argparse module

  

自版本2.7以来不推荐使用:不推荐使用optparse模块,不会进一步开发;开发将继续使用argparse模块。

argparse模块还支持指定custom help and usage messages,从而减​​轻了自己打印的需要。