Skip to content

Commit

Permalink
split vcs and vcs-help commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Jan 8, 2013
1 parent f28e605 commit d652957
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion scripts/vcs
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions scripts/vcs-help
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

import sys
from vcstool.commands.help import main

sys.exit(main() or 0)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
37 changes: 21 additions & 16 deletions vcstool/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 <command>', 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)
Expand Down
32 changes: 32 additions & 0 deletions vcstool/commands/vcs.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit d652957

Please sign in to comment.