diff --git a/scripts/vcs b/scripts/vcs index 04a306ed..c0f906fa 100755 --- a/scripts/vcs +++ b/scripts/vcs @@ -1,6 +1,6 @@ #!/usr/bin/env python import sys -from vcstool.commands.help import main +from vcstool.commands.vcs import main sys.exit(main() or 0) diff --git a/scripts/vcs-help b/scripts/vcs-help new file mode 100755 index 00000000..04a306ed --- /dev/null +++ b/scripts/vcs-help @@ -0,0 +1,6 @@ +#!/usr/bin/env python + +import sys +from vcstool.commands.help import main + +sys.exit(main() or 0) diff --git a/setup.py b/setup.py index ddcca663..ac254403 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ license='BSD', entry_points={ 'console_scripts': [ - 'vcs = vcstool.commands.help:main', + 'vcs = vcstool.commands.vcs:main', 'vcs-branch = vcstool.commands.branch:main', 'vcs-diff = vcstool.commands.diff:main', 'vcs-export = vcstool.commands.export:main', diff --git a/vcstool/commands/help.py b/vcstool/commands/help.py index facc308d..fd1bdb52 100644 --- a/vcstool/commands/help.py +++ b/vcstool/commands/help.py @@ -13,23 +13,13 @@ def main(args=None): parser = get_parser(add_help=False) ns, _ = parser.parse_known_args(args) - if ns.command and ns.command != 'help': - # accept command with same prefix if unique - commands = [cmd.command for cmd in vcstool_commands] - commands = [cmd for cmd in commands if cmd.startswith(ns.command)] - if len(commands) != 1: - print("vcs: '%s' is not a vcs command. See 'vcs help'." % ns.command) - if commands: - print('\n\ -Did you mean one of these?\n\ - %s' % '\n '.join(commands), file=sys.stderr) + # help for a specific command + if ns.command: + # relay help request foe specific command + entrypoint = get_entrypoint(ns.command) + if not entrypoint: return 1 - - entrypoint = load_entry_point('vcstool', 'console_scripts', 'vcs-%s' % commands[0]) - if args is None: - args = sys.argv[1:] - args.remove(ns.command) - return entrypoint(args) + return entrypoint(['--help']) # regular parsing validating options and arguments parser = get_parser() @@ -57,6 +47,21 @@ def get_parser(add_help=True): return parser +def get_entrypoint(command): + # accept command with same prefix if unique + commands = [cmd.command for cmd in vcstool_commands] + commands = [cmd for cmd in commands if cmd.startswith(command)] + if len(commands) != 1: + print("vcs: '%s' is not a vcs command. See 'vcs help'." % command, file=sys.stderr) + if commands: + print('\n\ +Did you mean one of these?\n\ + %s' % '\n '.join(commands), file=sys.stderr) + return None + + return load_entry_point('vcstool', 'console_scripts', 'vcs-%s' % commands[0]) + + def get_parser_with_command_only(): parser = argparse.ArgumentParser(prog='vcs', usage='%(prog)s ', formatter_class=argparse.RawDescriptionHelpFormatter, description='%s\n\n%s' % (_get_description(), '\n'.join(_get_command_help(vcstool_commands))), epilog=_get_epilog(), add_help=False) parser.add_argument('command', help=argparse.SUPPRESS) diff --git a/vcstool/commands/vcs.py b/vcstool/commands/vcs.py new file mode 100644 index 00000000..983646f4 --- /dev/null +++ b/vcstool/commands/vcs.py @@ -0,0 +1,32 @@ +from __future__ import print_function + +import sys + +from vcstool.commands.help import get_entrypoint, get_parser +from vcstool.commands.help import main as help_main + + +def main(args=None): + # no help to extract command first (which might be followed by --help) + parser = get_parser(add_help=False) + ns, _ = parser.parse_known_args(args) + args = args if args is not None else sys.argv[1:] + + # relay to specific command + if ns.command and ns.command != 'help': + entrypoint = get_entrypoint(ns.command) + if not entrypoint: + return 1 + + args.remove(ns.command) + return entrypoint(args) + + # remove help command if specified + if ns.command: + args.remove(ns.command) + + return help_main(args) + + +if __name__ == '__main__': + sys.exit(main())