diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2014-05-22 01:21:41 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2014-05-22 01:21:41 +0200 |
commit | db4b4b467118fec0ba87c07b548a6f23eced4dee (patch) | |
tree | 26b3906a18eeab544bc35e0cd9673840c9c6573c /examples | |
parent | 31043c3dc7b6b355831a2c49c14a1aecda2af3b5 (diff) | |
download | click-db4b4b467118fec0ba87c07b548a6f23eced4dee.tar.gz |
Added a complex example for plugin loading.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/complex/README | 10 | ||||
-rw-r--r-- | examples/complex/complex/__init__.py | 0 | ||||
-rw-r--r-- | examples/complex/complex/cli.py | 65 | ||||
-rw-r--r-- | examples/complex/complex/commands/__init__.py | 0 | ||||
-rw-r--r-- | examples/complex/complex/commands/cmd_init.py | 13 | ||||
-rw-r--r-- | examples/complex/complex/commands/cmd_status.py | 10 | ||||
-rw-r--r-- | examples/complex/setup.py | 15 |
7 files changed, 113 insertions, 0 deletions
diff --git a/examples/complex/README b/examples/complex/README new file mode 100644 index 0000000..599c33c --- /dev/null +++ b/examples/complex/README @@ -0,0 +1,10 @@ +$ complex_ + + complex is an example of building very complex cli + applications that load subcommands dynamically from + a plugin folder and other things. + +Usage: + + $ pip install --editable . + $ complex --help diff --git a/examples/complex/complex/__init__.py b/examples/complex/complex/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/complex/complex/__init__.py diff --git a/examples/complex/complex/cli.py b/examples/complex/complex/cli.py new file mode 100644 index 0000000..bd70ca3 --- /dev/null +++ b/examples/complex/complex/cli.py @@ -0,0 +1,65 @@ +import os +import sys +import click + + +class Context(object): + + def __init__(self): + self.verbose = False + self.home = os.getcwd() + + def log(self, msg, *args): + """Logs a message to stderr.""" + if args: + msg %= args + click.echo(msg, file=sys.stderr) + + def vlog(self, msg, *args): + """Logs a message to stderr only if verbose is enabled.""" + if self.verbose: + self.log(msg, *args) + + +pass_context = click.make_pass_decorator(Context, ensure=True) +cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), + 'commands')) + + +class ComplexCLI(click.MultiCommand): + + def list_commands(self, ctx): + rv = [] + for filename in os.listdir(cmd_folder): + if filename.endswith('.py') and \ + filename.startswith('cmd_'): + rv.append(filename[4:-3]) + rv.sort() + return rv + + def get_command(self, ctx, name): + try: + mod = __import__('complex.commands.cmd_' + + name.encode('ascii', 'replace'), + None, None, ['cli']) + except ImportError: + return + return mod.cli + + +@click.command(cls=ComplexCLI) +@click.option('--home', type=click.Path(exists=True, file_okay=False, + resolve_path=True), + help='Changes the folder to operate on.') +@click.option('-v', '--verbose', is_flag=True, + help='Enables verbose mode.') +@pass_context +def cli(ctx, verbose, home): + """A complex command line interface.""" + ctx.verbose = verbose + if home is not None: + ctx.home = home + + +def main(): + cli(auto_envvar_prefix='COMPLEX') diff --git a/examples/complex/complex/commands/__init__.py b/examples/complex/complex/commands/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/complex/complex/commands/__init__.py diff --git a/examples/complex/complex/commands/cmd_init.py b/examples/complex/complex/commands/cmd_init.py new file mode 100644 index 0000000..8c30186 --- /dev/null +++ b/examples/complex/complex/commands/cmd_init.py @@ -0,0 +1,13 @@ +import click +from complex.cli import pass_context + + +@click.command('init', short_help='Initializes a repo.') +@click.argument('path', required=False, type=click.Path(resolve_path=True)) +@pass_context +def cli(ctx, path): + """Initializes a repository.""" + if path is None: + path = ctx.home + ctx.log('Initialized the repository in %s', + click.format_filename(path)) diff --git a/examples/complex/complex/commands/cmd_status.py b/examples/complex/complex/commands/cmd_status.py new file mode 100644 index 0000000..abf78b6 --- /dev/null +++ b/examples/complex/complex/commands/cmd_status.py @@ -0,0 +1,10 @@ +import click +from complex.cli import pass_context + + +@click.command('init', short_help='Shows file changes.') +@pass_context +def cli(ctx): + """Shows file changes in the current working directory.""" + ctx.log('Changed files: none') + ctx.vlog('bla bla bla, debug info') diff --git a/examples/complex/setup.py b/examples/complex/setup.py new file mode 100644 index 0000000..1d107e5 --- /dev/null +++ b/examples/complex/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +setup( + name='complex', + version='1.0', + packages=['complex', 'complex.commands'], + include_package_data=True, + install_requires=[ + 'Click', + ], + entry_points=''' + [console_scripts] + complex=complex.cli:main + ''', +) |