Skip to content
Myr

Added method and command to update and write aliases.ini file with new aliases

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

authored by chusband · committed by David Lord · parent 8e4d4d2 · Browse files · 2 files+29 added−3 removed
This commit

State

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

Diff stats

2 files+29 added−3 removed

  • other:+29 added−3 removed

2 files+29 added−3 removed

modified:docs/advanced.rst+5 added-3 removed
10 unmodified lines
1112Command Aliases13---------------1415Many tools support aliases for commands. For instance, you can configure16``git`` to accept ``git ci`` as alias for ``git commit``. Other tools17also support auto-discovery for aliases by automatically shortening them.15Many tools support aliases for commands (see `Command alias example16<https://github.com/pallets/click/tree/master/examples/aliases>`_).17For instance, you can configure ``git`` to accept ``git ci`` as alias for18``git commit``. Other tools also support auto-discovery for aliases by19automatically shortening them.2021Click does not support this out of the box, but it's very easy to customize22the :class:`Group` or any other :class:`MultiCommand` to provide this23functionality.
358 unmodified lines
modified:examples/aliases/aliases.py+24 added
12 unmodified lines
13 def __init__(self):14 self.path = os.getcwd()15 self.aliases = {}1617 def add_alias(self, alias, cmd):18 self.aliases.update({alias: cmd})1920 def read_config(self, filename):21 parser = configparser.RawConfigParser()22 parser.read([filename])23 try:24 self.aliases.update(parser.items('aliases'))25 except configparser.NoSectionError:26 pass2728 def write_config(self, filename):29 parser = configparser.RawConfigParser()30 parser.add_section('aliases')31 for key, value in self.aliases.items():32 parser.set('aliases', key, value)33 with open(filename, 'wb') as file:34 parser.write(file)353637pass_config = click.make_pass_decorator(Config, ensure=True)383940class AliasedGroup(click.Group):
78 unmodified lines
119@pass_config120def status(config):121 """Shows the status."""122 click.echo('Status for %s' % config.path)123124125@cli.command()126@pass_config127@click.argument("alias_", metavar='ALIAS', type=click.STRING)128@click.argument("cmd", type=click.STRING)129@click.option('--config_file', type=click.Path(exists=True, dir_okay=False),130 default="aliases.ini")131def alias(config, alias_, cmd, config_file):132 """Adds an alias to the specified configuration file."""133 config.add_alias(alias_, cmd)134 config.write_config(config_file)135 click.echo('Added %s as alias for %s' % (alias_, cmd))