3

I am trying to register command-line options in Click. Everything is working fine until I add the params argument to the constructor.

class InitCommand(click.Command):
 def __init__(self):
 super().__init__(
 name='init',
 short_help='Initialize the needed scaffolding.',
 help='something helpful, but longer',
 # params=[] ### <-- Works fine
 params=[click.Option('--force', default=False)]
 )

I get the following error:

Traceback (most recent call last):
 File "/usr/local/bin/aquapy", line 11, in <module>
 load_entry_point('aquapy-cli', 'console_scripts', 'aquapy')()
 File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point
 return get_distribution(dist).load_entry_point(group, name)
 File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
 return ep.load()
 File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
 return self.resolve()
 File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
 module = __import__(self.module_name, fromlist=['__name__'], level=0)
 File "/Users/******/__main__.py", line 3, in <module>
 from .bootstrap import main
 File "/Users/******/bootstrap.py", line 17, in <module>
 main.add_command(InitCommand())
 File "/Users/******/commands/init_command.py", line 10, in __init__
 params=[click.Option('--force', default=False)]
 File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1460, in __init__
 Parameter.__init__(self, param_decls, type=type, **attrs)
 File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1266, in __init__
 self._parse_decls(param_decls or (), expose_value)
 File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1533, in _parse_decls
 raise TypeError('Name defined twice')
TypeError: Name defined twice
Stephen Rauch
50.1k32 gold badges119 silver badges143 bronze badges
asked Jun 27, 2017 at 9:09
4
  • You forgot a parenthesis in your __init__(). Also, please provide the complete Traceback. Thanks :). Commented Jun 27, 2017 at 9:12
  • No, I didn't...?? Commented Jun 27, 2017 at 9:13
  • 1
    How about an MCVE and an explanation of the use case? The pattern you are using here to init this class looks nothing like the way click classes are generally used. Commented Jun 27, 2017 at 13:49
  • @VaibhavBajaj The closing ) is at the bottom. The whole part under super() is the arguments to the constructor. Commented Jun 27, 2017 at 15:26

1 Answer 1

4

The issue is that click.Option() expects the first argument to a list and is not like the decorator version @click.option().

params=[click.Option(['--force'], default=False)]

Wrapping the command option in a list solved the problem.

answered Jun 27, 2017 at 16:37
Sign up to request clarification or add additional context in comments.

1 Comment

I figured it would behave like the decorator, too, and didn't even think to check the method signature in the Click documentation. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.