13

I'm using setuptools for a Python script I wrote

After installing, I do:

$ megazord -i input -d database -v xx-xx -w yy-yy

Like I would if I was running it ./like_this

However, I get:

Traceback (most recent call last):
  File "/usr/local/bin/megazord", line 9, in <module>
    load_entry_point('megazord==1.0.0', 'console_scripts', 'megazord')()
TypeError: main() takes exactly 1 argument (0 given)

Which looks like setuptools is not sending my arguments to main() to be parsed (by optparse)

Here's my setuptools config for entry_points:

entry_points = {
    'console_scripts': [ 
        'megazord = megazord.megazord:main',
        'megazord-benchmark = megazord.benchmark:main',
        'megazord-hash = megazord.mzhash:main',
        'megazord-mutate = megazord.mutator:main',
        ]
}

Any ideas?

Austin Richardson
  • 8,078
  • 13
  • 43
  • 49

2 Answers2

20

Just to give a full picture of what megazord.py would look like, using @Jeffrey Harris suggestion to use a nice library for parsing the inputs.

import argparse

def main():
    ''' Example of taking inputs for megazord bin'''
    parser = argparse.ArgumentParser(prog='my_megazord_program')
    parser.add_argument('-i', nargs='?', help='help for -i blah')
    parser.add_argument('-d', nargs='?', help='help for -d blah')
    parser.add_argument('-v', nargs='?', help='help for -v blah')
    parser.add_argument('-w', nargs='?', help='help for -w blah')

    args = parser.parse_args()

    collected_inputs = {'i': args.i,
                    'd': args.d,
                    'v': args.v,
                    'w': args.w}
    print 'got input: ', collected_inputs 

And with using it like in the above, one would get

$ megazord -i input -d database -v xx-xx -w yy-yy
got input: {'i': 'input', 'd': 'database', 'w': 'yy-yy', 'v': 'xx-xx'}

And since they are all optional arguments,

$ megazord
got input: {'i': None, 'd': None, 'w': None, 'v': None}
HeyWatchThis
  • 21,241
  • 6
  • 33
  • 41
14

The setuptools console_scripts entry point wants a function of no arguments.

Happily, optparse (Parser for command line options) doesn't need to be passed any arguments, it will read in sys.argv[1:] and use that as it's input.

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
Jeffrey Harris
  • 3,480
  • 25
  • 30
  • 7
    Side note that as of Python 2.7, optparse is [depracated](https://docs.python.org/2/library/optparse.html) in favor of [argparse](https://docs.python.org/2/library/argparse.html) – HeyWatchThis May 20 '14 at 14:14