optparse TypeError

rantingrick rantingrick at gmail.com
Mon Jun 28 17:47:44 EDT 2010


On Jun 28, 10:44 am, Michele Simionato <michele.simion... at gmail.com>
wrote:

> optparse is so old-fashioned. Use plac!

Hog wash! Both are archaic and asinine. Both clog your scripts with
wasted lines and your memory with complex interfaces far worse than
colon clogging junk food can hold a candle to. Give your *script* an
enema, and do *yourself* a favor by harnessing the simplistic elegance
of the "optphart" module instead!


#-- Start Script --#
def optphart(args):
    d = {'args':[]}
    for arg in args:
        if arg.startswith('-'):
            key, value = arg.split('=', 1)
            options = value.split(',')
            if len(options) > 1:
                d[key[1:]] = options
            else:
                d[key[1:]] = [value]
        else:
            d['args'].append(arg)
    return d


if __name__ == '__main__':
    args = [
        'nakedArgumentOne',
        'nakedArgumentTwo',
        '-file=aSingleFile.txt',
        '-files=aFileOne.txt,aFileTwo.txt',
        '-options=Op1,Op2,Op3,Op4',
        '-help=this_is_some_unhelpful_help',
        ]
    opts = optphart(args)
    #print opts.keys()
    for k,v in opts.items():
        print '%s -> %s' %(k, v)
#-- End Script --#




More information about the Python-list mailing list