Skip to content
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

Sub-commands manual not generated #14

Open
ivansenic opened this issue Sep 28, 2017 · 4 comments
Open

Sub-commands manual not generated #14

ivansenic opened this issue Sep 28, 2017 · 4 comments

Comments

@ivansenic
Copy link

Hi,

We use such setup:

from setuptools import setup, find_packages

setup(
    name='ap',
    version='1.0',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'Click'
    ],
    entry_points='''
        [console_scripts]
        ap=main:cli
    '''
)

with the dynamic sub-commands located in the [root]/scripts/commands and dynamic loading in the main.py:

COMMANDS_FOLDER = os.path.join(os.path.dirname(__file__), 'scripts', 'commands')


class ApCli(click.MultiCommand):

    def list_commands(self, ctx):
        """Provides names of all commands by searching the commands folder"""
        rv = []
        for filename in os.listdir(COMMANDS_FOLDER):
            # find py files, ignore the ones that start with __ (f.e. __init__.py)
            if filename.endswith('.py') and not filename.startswith('__'):
                rv.append(filename[:-3])
        rv.sort()
        return rv

    def get_command(self, ctx, name):
        """Returns compiled command for given name, expects that command has a function same as its name"""
        ns = {}
        fn = os.path.join(COMMANDS_FOLDER, name + '.py')
        if os.path.isfile(fn):
            with open(fn) as f:
                code = compile(f.read(), fn, 'exec')
                eval(code, ns, ns)
        else:
            return

        if ns.get(name) is not None:
            return ns[name]


@click.command(cls=ApCli)
@click.option('-v', '--verbose', is_flag=True, default=False, help='Enables verbose mode.')
@click.version_option()
@ap.pass_context
def cli(context, verbose):
    """Main entry point for the CLI"""
    context.logger.isVerbose = verbose

However manual get generated only for our main entry point and for nothing else.. Any idea how can we fix this?

@jhermann
Copy link

Support for command groups seems to be missing generally, as I got this on my first test…

AttributeError: 'Group' object has no attribute 'get_short_help_str'

@smarlowucf
Copy link
Contributor

The AttributeError I think is an issue with Click < 7.0. #33 should fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@jhermann @smarlowucf @ivansenic and others