Skip to content

Use function both as API and click function? #1054

Description

@michaelaye

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:

@click.command()
@click.argument('datestr')
def nasa_date_to_iso(datestr):
    """Convert the day-number based NASA format to ISO.

    Parameters
    ----------
    datestr : str
        Date string in the form Y-j

    Returns
    -------
    Datestring in ISO standard yyyy-mm-ddTHH:MM:SS.MMMMMM
    """
    date = dt.datetime.strptime(datestr, nasa_date_format)
    click.echo(date.isoformat())
    return date.isoformat()

Simple enough, right?
I install it via setup.py like so:

    entry_points={
        "console_scripts": [
            'nasa_date_to_iso = planetpy.utils:nasa_date_to_iso',
        ]
    },

but when I call it within ipython I get this:

In [1]: from planetpy.utils import nasa_date_to_iso

In [2]: nasa_date_to_iso('2017-090')
Usage: ipython [OPTIONS] DATESTR

Error: Got unexpected extra arguments (0 1 7 - 0 9 0)
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/Users/klay6683/miniconda3/envs/stable/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2971: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

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

Metadata

Metadata

Assignees

Labels

docsUpdates to documentation, readme, docstrings, typos

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions