diff options
author | chusband <chusband@gmail.com> | 2019-05-31 16:16:33 +0000 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2019-08-01 12:42:20 -0700 |
commit | 4bb34ee93fbd342156ed9b2eb856067d92d71ada (patch) | |
tree | cb099da729139138135901879bdbcb29e62f1803 /examples | |
parent | 8e4d4d253b7eee6f090829a5833780436218bc9e (diff) | |
download | click-4bb34ee93fbd342156ed9b2eb856067d92d71ada.tar.gz |
Added method and command to update and write aliases.ini file with new aliases
Diffstat (limited to 'examples')
-rw-r--r-- | examples/aliases/aliases.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/aliases/aliases.py b/examples/aliases/aliases.py index 38ef72c..299d3a5 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)) |