diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/aliases/aliases.py | 24 | ||||
-rw-r--r-- | examples/complex/complex/cli.py | 6 | ||||
-rw-r--r-- | examples/complex/complex/commands/cmd_init.py | 4 | ||||
-rw-r--r-- | examples/complex/complex/commands/cmd_status.py | 4 |
4 files changed, 31 insertions, 7 deletions
diff --git a/examples/aliases/aliases.py b/examples/aliases/aliases.py index eb5bad4..4125c03 100644 --- a/examples/aliases/aliases.py +++ b/examples/aliases/aliases.py @@ -14,6 +14,9 @@ class Config(object): self.path = os.getcwd() self.aliases = {} + def add_alias(self, alias, cmd): + self.aliases.update({alias: cmd}) + def read_config(self, filename): parser = configparser.RawConfigParser() parser.read([filename]) @@ -22,6 +25,14 @@ class Config(object): except configparser.NoSectionError: pass + def write_config(self, filename): + parser = configparser.RawConfigParser() + parser.add_section('aliases') + for key, value in self.aliases.items(): + parser.set('aliases', key, value) + with open(filename, 'wb') as file: + parser.write(file) + pass_config = click.make_pass_decorator(Config, ensure=True) @@ -109,3 +120,16 @@ def commit(): def status(config): """Shows the status.""" click.echo('Status for %s' % config.path) + + +@cli.command() +@pass_config +@click.argument("alias_", metavar='ALIAS', type=click.STRING) +@click.argument("cmd", type=click.STRING) +@click.option('--config_file', type=click.Path(exists=True, dir_okay=False), + default="aliases.ini") +def alias(config, alias_, cmd, config_file): + """Adds an alias to the specified configuration file.""" + config.add_alias(alias_, cmd) + config.write_config(config_file) + click.echo('Added %s as alias for %s' % (alias_, cmd)) diff --git a/examples/complex/complex/cli.py b/examples/complex/complex/cli.py index bcfd14a..0d10c62 100644 --- a/examples/complex/complex/cli.py +++ b/examples/complex/complex/cli.py @@ -6,7 +6,7 @@ import click CONTEXT_SETTINGS = dict(auto_envvar_prefix='COMPLEX') -class Context(object): +class Environment(object): def __init__(self): self.verbose = False @@ -24,7 +24,7 @@ class Context(object): self.log(msg, *args) -pass_context = click.make_pass_decorator(Context, ensure=True) +pass_environment = click.make_pass_decorator(Environment, ensure=True) cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), 'commands')) @@ -57,7 +57,7 @@ class ComplexCLI(click.MultiCommand): help='Changes the folder to operate on.') @click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode.') -@pass_context +@pass_environment def cli(ctx, verbose, home): """A complex command line interface.""" ctx.verbose = verbose diff --git a/examples/complex/complex/commands/cmd_init.py b/examples/complex/complex/commands/cmd_init.py index 8c30186..a77116b 100644 --- a/examples/complex/complex/commands/cmd_init.py +++ b/examples/complex/complex/commands/cmd_init.py @@ -1,10 +1,10 @@ import click -from complex.cli import pass_context +from complex.cli import pass_environment @click.command('init', short_help='Initializes a repo.') @click.argument('path', required=False, type=click.Path(resolve_path=True)) -@pass_context +@pass_environment def cli(ctx, path): """Initializes a repository.""" if path is None: diff --git a/examples/complex/complex/commands/cmd_status.py b/examples/complex/complex/commands/cmd_status.py index 99e736e..12229c4 100644 --- a/examples/complex/complex/commands/cmd_status.py +++ b/examples/complex/complex/commands/cmd_status.py @@ -1,9 +1,9 @@ import click -from complex.cli import pass_context +from complex.cli import pass_environment @click.command('status', short_help='Shows file changes.') -@pass_context +@pass_environment def cli(ctx): """Shows file changes in the current working directory.""" ctx.log('Changed files: none') |