Skip to content
Myr

Added a complex example for plugin loading.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 31043c3 · Browse files · 7 files+113 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+113 added−0 removed

  • other:+113 added−0 removed

7 files+113 added−0 removed

added:examples/complex/complex/commands/__init__.py
added:examples/complex/complex/commands/cmd_init.py+13 added
1import click2from complex.cli import pass_context345@click.command('init', short_help='Initializes a repo.')6@click.argument('path', required=False, type=click.Path(resolve_path=True))7@pass_context8def cli(ctx, path):9 """Initializes a repository."""10 if path is None:11 path = ctx.home12 ctx.log('Initialized the repository in %s',13 click.format_filename(path))
added:examples/complex/complex/commands/cmd_status.py+10 added
1import click2from complex.cli import pass_context345@click.command('init', short_help='Shows file changes.')6@pass_context7def cli(ctx):8 """Shows file changes in the current working directory."""9 ctx.log('Changed files: none')10 ctx.vlog('bla bla bla, debug info')
added:examples/complex/complex/__init__.py
added:examples/complex/complex/cli.py+65 added
1import os2import sys3import click456class Context(object):78 def __init__(self):9 self.verbose = False10 self.home = os.getcwd()1112 def log(self, msg, *args):13 """Logs a message to stderr."""14 if args:15 msg %= args16 click.echo(msg, file=sys.stderr)1718 def vlog(self, msg, *args):19 """Logs a message to stderr only if verbose is enabled."""20 if self.verbose:21 self.log(msg, *args)222324pass_context = click.make_pass_decorator(Context, ensure=True)25cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),26 'commands'))272829class ComplexCLI(click.MultiCommand):3031 def list_commands(self, ctx):32 rv = []33 for filename in os.listdir(cmd_folder):34 if filename.endswith('.py') and \35 filename.startswith('cmd_'):36 rv.append(filename[4:-3])37 rv.sort()38 return rv3940 def get_command(self, ctx, name):41 try:42 mod = __import__('complex.commands.cmd_' +43 name.encode('ascii', 'replace'),44 None, None, ['cli'])45 except ImportError:46 return47 return mod.cli484950@click.command(cls=ComplexCLI)51@click.option('--home', type=click.Path(exists=True, file_okay=False,52 resolve_path=True),53 help='Changes the folder to operate on.')54@click.option('-v', '--verbose', is_flag=True,55 help='Enables verbose mode.')56@pass_context57def cli(ctx, verbose, home):58 """A complex command line interface."""59 ctx.verbose = verbose60 if home is not None:61 ctx.home = home626364def main():65 cli(auto_envvar_prefix='COMPLEX')
added:examples/complex/README+10 added
1$ complex_23 complex is an example of building very complex cli4 applications that load subcommands dynamically from5 a plugin folder and other things.67Usage:89 $ pip install --editable .10 $ complex --help
added:examples/complex/setup.py+15 added
1from setuptools import setup23setup(4 name='complex',5 version='1.0',6 packages=['complex', 'complex.commands'],7 include_package_data=True,8 install_requires=[9 'Click',10 ],11 entry_points='''12 [console_scripts]13 complex=complex.cli:main14 ''',15)