pythonpython-click

Click error "takes no arguments" in main()


I am having issues which I believe are due to click. I am trying to run this code and I keep running into the same traceback TypeError stating an argument is being passed to main().

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import boto
import boto.ec2
import click

@click.command()
@click.option('--region', '-r', default='us-west-2', help='AWS Region')

def main():
    try:
        ec2 = boto.ec2.connect_to_region(region)
    except Exception, error:
        print("Boto Error: %s" ) % str(error)
    else:
        print("It's working!")
    finally:
        sys.exit(1)

if __name__ == "__main__":
    main()

This code seems pretty simple, so I take click is doing something here to prevent this for working correctly?

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    main()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-5.1-py2.7.egg/click/core.py", line 700, in __call__
    return self.main(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-5.1-py2.7.egg/click/core.py", line 680, in main
    rv = self.invoke(ctx)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-5.1-py2.7.egg/click/core.py", line 873, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-5.1-py2.7.egg/click/core.py", line 508, in invoke
    return callback(*args, **kwargs)
TypeError: main() takes no arguments (1 given)

Solution

  • I'm not familiar with click module but from what I can see from the docs, you should change def main(): to def main(region): since you want the function to accept the parameter you are passing from the command line.