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
Provenance
- Commit
db4b4b4- Tree
26b3906- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
31043c3
7 files changed+113 added−0 removed
added:examples/complex/complex/commands/__init__.py
added:examples/complex/complex/commands/cmd_init.py+13 added
1+import click2+from complex.cli import pass_context3+4+5+@click.command('init', short_help='Initializes a repo.')6+@click.argument('path', required=False, type=click.Path(resolve_path=True))7+@pass_context8+def 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
1+import click2+from complex.cli import pass_context3+4+5+@click.command('init', short_help='Shows file changes.')6+@pass_context7+def 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
1+import os2+import sys3+import click4+5+6+class Context(object):7+8+ def __init__(self):9+ self.verbose = False10+ self.home = os.getcwd()11+12+ def log(self, msg, *args):13+ """Logs a message to stderr."""14+ if args:15+ msg %= args16+ click.echo(msg, file=sys.stderr)17+18+ 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)22+23+24+pass_context = click.make_pass_decorator(Context, ensure=True)25+cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),26+ 'commands'))27+28+29+class ComplexCLI(click.MultiCommand):30+31+ 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 rv39+40+ 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.cli48+49+50+@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_context57+def cli(ctx, verbose, home):58+ """A complex command line interface."""59+ ctx.verbose = verbose60+ if home is not None:61+ ctx.home = home62+63+64+def main():65+ cli(auto_envvar_prefix='COMPLEX')added:examples/complex/README+10 added
1+$ complex_2+3+ complex is an example of building very complex cli4+ applications that load subcommands dynamically from5+ a plugin folder and other things.6+7+Usage:8+9+ $ pip install --editable .10+ $ complex --helpadded:examples/complex/setup.py+15 added
1+from setuptools import setup2+3+setup(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+)