import os
import click
try:
import ConfigParser as configparser
except ImportError:
import configparser
class Config(object):
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):
def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv
cfg = ctx.ensure_object(Config)
if cmd_name in cfg.aliases:
actual_cmd = cfg.aliases[cmd_name]
return click.Group.get_command(self, ctx, actual_cmd)
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):
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():
@cli.command()
def push():
click.echo('Push')
@cli.command()
def pull():
click.echo('Pull')
@cli.command()
def clone():
click.echo('Clone')
@cli.command()
def commit():
click.echo('Commit')
@cli.command()
@pass_config
def status(config):
click.echo('Status for %s' % config.path)