From 7e5fd397653f197eae6c8fbb71e083974ff7805b Mon Sep 17 00:00:00 2001 From: Haikel Guemar Date: Sep 09 2016 17:11:06 +0000 Subject: Allow pag to passthrough unrecognized commands to git This feature is to delegate execution of unrecognized commands to git. This allows using pag as an alias of git. e.g: 'pag who -n' redirects to 'git who -n' --- diff --git a/pag/app.py b/pag/app.py index b6c2ab9..529bc60 100644 --- a/pag/app.py +++ b/pag/app.py @@ -2,10 +2,32 @@ import click -@click.group() + +class PassthroughGroup(click.Group): + """This subclass adds two features + - allow resolving command names given to the command-line to the closest + existing command (e.g clo -> clone) + - if it doesn't exist, redirect to the default handlers and pass arguments + to the git binary. + """ + def get_command(self, ctx, cmd_name): + rv = click.Group.get_command(self, ctx, cmd_name) + if rv is not None: + return rv + matches = [x for x in self.list_commands(ctx) + if x.startswith(cmd_name)] + if not matches: + return click.Group.get_command(self, ctx, 'default') + elif len(matches) == 1: + return click.Group.get_command(self, ctx, matches[0]) + ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) + + +@click.group(cls=PassthroughGroup) def app(): pass + __all__ = [ 'app', ] @@ -15,7 +37,7 @@ from .commands import clone from .commands import fork from .commands import remote from .commands import pullrequest - +from .commands import gitaliasing if __name__ == '__main__': app() diff --git a/pag/commands/gitaliasing.py b/pag/commands/gitaliasing.py new file mode 100644 index 0000000..1c6e493 --- /dev/null +++ b/pag/commands/gitaliasing.py @@ -0,0 +1,18 @@ +import click + +from pag.app import app +from pag.utils import run + +# FIXME: click master has added a hidden parameter that would allow +# us to hide this fallback command from generated help. +# hidden paramter was added in commit 8f4c34f69554d3397627ef16adb1b1f9b83c381e +# https://github.com/pallets/click/commit/8f4c34f69554d3397627ef16adb1b1f9b83c381e +@app.command(context_settings=dict(ignore_unknown_options=True,)) +@click.argument('cli_args', nargs=-1, type=click.UNPROCESSED) +@click.pass_context +def default(ctx, cli_args): + """""" + git_cmd = ctx.info_name + myargs = ['git', git_cmd] + myargs.extend(cli_args) + run(myargs)