optparse TypeError

Michele Simionato michele.simionato at gmail.com
Mon Jun 28 11:44:22 EDT 2010


On Jun 28, 3:30 pm, dirknbr <dirk... at gmail.com> wrote:
> I get an int object is not callable TypeError when I execute this. But
> I don't understand why.
>
>     parser = optparse.OptionParser("usage: %lines [options] arg1")
>     parser.add_option("-l", "--lines", dest="lines",
>                       default=10, type="int",
>                       help="number of lines")
>     parser.add_option("-t", "--topbottom", dest="topbottom",
>                       default="T", type="str",
>                       help="T(op) or B(ottom)")
>
>     (options, args) = parser.parse_args()
>     if len(args) != 1:
>         parser.error("incorrect number of arguments")
>     lines=options.lines
>     tb=options.topbottom
>
> Dirk
>     lines(args[0],topbottom=tb,maxi=lines)

optparse is so old-fashioned. Use plac!

$ cat x.py
import plac

@plac.annotations(
    lines=('number of lines', 'option', 'l', int),
    topbottom=('T(op) or B(ottom)', 'option', 't', str, 'TB'))
def main(arg, lines=10, topbottom='T'):
    print arg, lines, topbottom

if __name__ == '__main__':
    plac.call(main)

$ python x.py -h
usage: x.py [-h] [-l 10] [-t T] arg

positional arguments:
  arg

optional arguments:
  -h, --help           show this help message and exit
  -l 10, --lines 10    number of lines
  -t T, --topbottom T  T(op) or B(ottom)


(see http://pypi.python.org/pypi/plac)



More information about the Python-list mailing list