-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Use function both as API and click function? #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's no longer a plain function, it's a command. You can run a command by calling it, which invokes its # will invoke Click pipeline
nasa_date_to_iso(['2017-090'], standalone_mode=False)
# equivalent
nasa_date_to_iso.main(['2017-090'], standalone_mode=False) Assuming you know the given command is a direct wrapper for a function you wrote (and not a group or other type of command), you can get at the function with # won't invoke Click pipeline
nasa_date_to_iso.callback('2017-090') |
Hm, that's of course not optimal, because now I have to decide between adding a boilerplate function with a different name just for |
def nasa_date_to_iso(value):
...
@click.command('nasa_date_to_iso')
def nasa_date_to_iso_command(value):
click.echo(nasa_date_to_iso(value)) This would be pretty easy to write a decorator for to automate. |
Thank you, that looks like the smallest extra code possible. I was just hoping that that wouldn't be necessary. Thanks anyway! |
I would still call this a bad experience as a user. Why do we have to write the boilerplate? You have 5 args, you need to duplicate 5 args. If you go kwargs then you lose the signature. |
A command is no longer a regular function. It may assume all sorts of other behaviors now, such as argument validation, the availability of the Click context, or that the function is being run interactively. Extracting a behavior into a common function that's used in multiple situations is a common and perfectly acceptable refactor. |
I ran into this today. Obvious in retrospect, but probably worth an addition to docs faq. |
I am trying to reduce boilerplate code and it was my idea to just decorate an existing function with click decorators to make it available via click. It's unclear to me from the documentation if this use case is supported. I also search the issues for the below error message and couldn't find anything related.
When I decorate a function with click, I am not able to use it as a function within Python:
Here's the function code:
Simple enough, right?
I install it via setup.py like so:
but when I call it within ipython I get this:
So, is it not possible to have the same function both as a click-cli function and an python-internal API?
System: Py3.6 via conda on macOS 10.12.6, click version 6.7
The text was updated successfully, but these errors were encountered: