diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/python/Click/examples/aliases/aliases.py | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/python/Click/examples/aliases/aliases.py')
-rw-r--r-- | third_party/python/Click/examples/aliases/aliases.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/third_party/python/Click/examples/aliases/aliases.py b/third_party/python/Click/examples/aliases/aliases.py new file mode 100644 index 0000000000..38ef72c5c4 --- /dev/null +++ b/third_party/python/Click/examples/aliases/aliases.py @@ -0,0 +1,111 @@ +import os +import click + +try: + import ConfigParser as configparser +except ImportError: + import configparser + + +class Config(object): + """The config in this example only holds aliases.""" + + def __init__(self): + self.path = os.getcwd() + self.aliases = {} + + def read_config(self, filename): + parser = configparser.RawConfigParser() + parser.read([filename]) + try: + self.aliases.update(parser.items('aliases')) + except configparser.NoSectionError: + pass + + +pass_config = click.make_pass_decorator(Config, ensure=True) + + +class AliasedGroup(click.Group): + """This subclass of a group supports looking up aliases in a config + file and with a bit of magic. + """ + + def get_command(self, ctx, cmd_name): + # Step one: bulitin commands as normal + rv = click.Group.get_command(self, ctx, cmd_name) + if rv is not None: + return rv + + # Step two: find the config object and ensure it's there. This + # will create the config object is missing. + cfg = ctx.ensure_object(Config) + + # Step three: lookup an explicit command aliase in the config + if cmd_name in cfg.aliases: + actual_cmd = cfg.aliases[cmd_name] + return click.Group.get_command(self, ctx, actual_cmd) + + # Alternative option: if we did not find an explicit alias we + # allow automatic abbreviation of the command. "status" for + # instance will match "st". We only allow that however if + # there is only one command. + matches = [x for x in self.list_commands(ctx) + if x.lower().startswith(cmd_name.lower())] + if not matches: + return None + elif len(matches) == 1: + return click.Group.get_command(self, ctx, matches[0]) + ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) + + +def read_config(ctx, param, value): + """Callback that is used whenever --config is passed. We use this to + always load the correct config. This means that the config is loaded + even if the group itself never executes so our aliases stay always + available. + """ + cfg = ctx.ensure_object(Config) + if value is None: + value = os.path.join(os.path.dirname(__file__), 'aliases.ini') + cfg.read_config(value) + return value + + +@click.command(cls=AliasedGroup) +@click.option('--config', type=click.Path(exists=True, dir_okay=False), + callback=read_config, expose_value=False, + help='The config file to use instead of the default.') +def cli(): + """An example application that supports aliases.""" + + +@cli.command() +def push(): + """Pushes changes.""" + click.echo('Push') + + +@cli.command() +def pull(): + """Pulls changes.""" + click.echo('Pull') + + +@cli.command() +def clone(): + """Clones a repository.""" + click.echo('Clone') + + +@cli.command() +def commit(): + """Commits pending changes.""" + click.echo('Commit') + + +@cli.command() +@pass_config +def status(config): + """Shows the status.""" + click.echo('Status for %s' % config.path) |