MetaStack
MetaStack

Reputation: 3696

how to integrate a whole click cli module with setuptools entry_points

Under spartans/bin/ I have a click cli module named spartans.py that looks like this:

import click

@click.group()
def main():
    ...

@main.command()
def hub_push():
    ...

@main.command()
@click.argument('username', type=str, required=False)
@click.argument('password', type=str, required=False)
def hub_clean(username='', password=''):
    ...

@main.command()
@click.argument('force', type=bool, required=False)
def hub_delete(force=False):
    ...

if __name__ == '__main__':  # necessary?
    main()

I am trying to install the this spartans package using setuptools, so that I can run from the command line these commands:

$ spartans hub_push
$ spartans hub_clean --username ...
$ spartans hub_delete --force

Technically I'm testing on windows, but it should be able to run like that on anything. However, I can't figure out how to get this pattern using setuptools.

If I simply change directory to the folder holding spartans.py, the click module lets me use it like this:

$ spartans.py hub_push
$ spartans.py hub_clean --username ...
$ spartans.py hub_delete --force

but if I want to use entry_points with setuptools I have to do it like this:

...
entry_points={
    "console_scripts": [
        "spartans = spartans.bin.spartans:hub_clean",
        # "spartans = spartans.bin.spartans",  # module not callable!
    ]
},

Since the module itself isn't callable I don't know how to create the pattern I want above. it seems I'll have to settle for something like:

$ spartans-hub_push
$ spartans-hub_clean --username ...
$ spartans-hub_delete --force

Where I concatenate the specific command to the module. But this cannot be right. I looked at the documentation but it still only calls a specific function: http://click.palletsprojects.com/en/5.x/setuptools/

How do I get setuptools to call a click cli package?

Upvotes: 0

Views: 481

Answers (1)

buran
buran

Reputation: 14233

It should be "spartans = spartans.bin.spartans:main", i.e. use the click.group

Upvotes: 1

Related Questions