Skip to content
This repository
Browse code

Merge pull request #74 from wiseman/develop

Consolidate command definitions and show short help text when listing commands.
  • Loading branch information...
commit 7981146af99727278b8bb973075bf445c3560658 2 parents c7b4ee6 + c8d7135
Kenneth Reitz authored

Showing 1 changed file with 148 additions and 65 deletions. Show diff stats Hide diff stats

  1. +148 65 legit/cli.py
213 legit/cli.py
@@ -36,14 +36,12 @@ def black(s):
36 36 def main():
37 37 """Primary Legit command dispatch."""
38 38
39   - if (args.get(0) in cmd_map) or (args.get(0) in short_map):
  39 + command = Command.lookup(args.get(0))
  40 + if command:
40 41 arg = args.get(0)
41 42 args.remove(arg)
42 43
43   - if arg in short_map:
44   - arg = short_map.get(arg)
45   -
46   - cmd_map.get(arg).__call__(args)
  44 + command.__call__(args)
47 45 sys.exit()
48 46
49 47 elif args.contains(('-h', '--help')):
@@ -66,10 +64,16 @@ def main():
66 64 sys.exit(call(' '.join(git_args), shell=True))
67 65
68 66 else:
  67 + show_error(colored.red('Unknown command {0}'.format(args.get(0))))
69 68 display_info()
70 69 sys.exit(1)
71 70
72 71
  72 +def show_error(msg):
  73 + sys.stdout.flush()
  74 + sys.stderr.write(msg + '\n')
  75 +
  76 +
73 77 # -------
74 78 # Helpers
75 79 # -------
@@ -419,34 +423,12 @@ def help(command):
419 423 if command == None:
420 424 command = 'help'
421 425
422   - help_info = dict()
423   - help_info['branches'] = 'branches\n\nGet a nice pretty list of ' \
424   - 'branches.'
425   - help_info['graft'] = 'graft <branch> <into-branch>\n\nMerges ' \
426   - 'specified branch into the second branch,' \
427   - ' and removes it. You can only graft unpublished ' \
428   - 'branches.'
429   - help_info['harvest'] = None
430   - help_info['help'] = 'help <command>\n\n' \
431   - 'Display help for legit command.'
432   - help_info['publish'] = 'publish <branch>\n\n' \
433   - 'Publishes specified branch to the remote.'
434   - help_info['unpublish'] = 'unpublish <branch>' \
435   - 'Removes specified branch from the remote.'
436   - help_info['settings'] = None
437   - help_info['sprout'] = 'sprout [<branch>] <new-branch>\n\n' \
438   - 'Creates a new branch off of the specified branch.' \
439   - 'Defaults to current branch. Switches to it immediately.'
440   - help_info['switch'] = 'switch <branch>\n\n' \
441   - 'Switches to specified branch. Automatically stashes and unstashes any changes.'
442   -
443   - help_info['sync'] = 'sync [<branch>]\n\n' \
444   - 'Syncronizes the given branch.' \
445   - 'Defaults to current branch.' \
446   - 'Stash, Fetch, Auto-Merge/Rebase, Push, and Unstash.'
447   - help_info['unpublish'] = 'unpublish <branch>\n\n' \
448   - 'Removes specified branch from the remote.'
449   - print help_info[command]
  426 + cmd = Command.lookup(command)
  427 + usage = cmd.usage or ''
  428 + help = cmd.help or ''
  429 + help_text = '%s\n\n%s' % (usage, help)
  430 + print help_text
  431 +
450 432
451 433 def display_available_branches():
452 434 """Displays available branches."""
@@ -482,11 +464,20 @@ def display_info():
482 464 ))
483 465
484 466 puts('Usage: {0}'.format(colored.blue('legit <command>')))
485   - puts('Commands: {0}.\n'.format(
486   - eng_join(
487   - [str(colored.green(c)) for c in sorted(cmd_map.keys())]
488   - )
489   - ))
  467 + puts('Commands:\n')
  468 + for command in Command.all_commands():
  469 + usage = command.usage or command.name
  470 + help = command.help or ''
  471 + puts('{0:40} {1}'.format(
  472 + colored.green(usage),
  473 + first_sentence(help)))
  474 +
  475 +
  476 +def first_sentence(s):
  477 + pos = s.find('. ')
  478 + if pos < 0:
  479 + pos = len(s) - 1
  480 + return s[:pos + 1]
490 481
491 482
492 483 def display_help():
@@ -515,30 +506,122 @@ def handle_abort(aborted):
515 506 settings.abort_handler = handle_abort
516 507
517 508
518   -cmd_map = dict(
519   - switch=cmd_switch,
520   - sync=cmd_sync,
521   - sprout=cmd_sprout,
522   - graft=cmd_graft,
523   - harvest=cmd_harvest,
524   - publish=cmd_publish,
525   - unpublish=cmd_unpublish,
526   - branches=cmd_branches,
527   - settings=cmd_settings,
528   - help=cmd_help,
529   - install=cmd_install
530   -)
531   -
532   -short_map = dict(
533   - sw='switch',
534   - sy='sync',
535   - sp='sprout',
536   - gr='graft',
537   - pub='publish',
538   - unp='unpublish',
539   - br='branches',
540   - ha='harvest',
541   - hv='harvest',
542   - har='harvest',
543   - h='help'
544   -)
  509 +class Command(object):
  510 + COMMANDS = {}
  511 + SHORT_MAP = {}
  512 +
  513 + @classmethod
  514 + def register(klass, command):
  515 + klass.COMMANDS[command.name] = command
  516 + if command.short:
  517 + for short in command.short:
  518 + klass.SHORT_MAP[short] = command
  519 +
  520 + @classmethod
  521 + def lookup(klass, name):
  522 + if name in klass.SHORT_MAP:
  523 + return klass.SHORT_MAP[name]
  524 + if name in klass.COMMANDS:
  525 + return klass.COMMANDS[name]
  526 + else:
  527 + return None
  528 +
  529 + @classmethod
  530 + def all_commands(klass):
  531 + return sorted(klass.COMMANDS.values(),
  532 + key=lambda cmd: cmd.name)
  533 +
  534 + def __init__(self, name=None, short=None, fn=None, usage=None, help=None):
  535 + self.name = name
  536 + self.short = short
  537 + self.fn = fn
  538 + self.usage = usage
  539 + self.help = help
  540 +
  541 + def __call__(self, *args, **kw_args):
  542 + return self.fn(*args, **kw_args)
  543 +
  544 +
  545 +def def_cmd(name=None, short=None, fn=None, usage=None, help=None):
  546 + command = Command(name=name, short=short, fn=fn, usage=usage, help=help)
  547 + Command.register(command)
  548 +
  549 +
  550 +def_cmd(
  551 + name='branches',
  552 + fn=cmd_branches,
  553 + usage='branches',
  554 + help='Get a nice pretty list of branches.')
  555 +
  556 +def_cmd(
  557 + name='graft',
  558 + short=['gr'],
  559 + fn=cmd_graft,
  560 + usage='graft <branch> <into-branch>',
  561 + help=('Merges specified branch into the second branch, and removes it. '
  562 + 'You can only graft unpublished branches.'))
  563 +
  564 +def_cmd(
  565 + name='harvest',
  566 + short=['ha', 'hv', 'har'],
  567 + usage='harvest [<branch>] <into-branch>',
  568 + help=('Auto-Merge/Rebase of specified branch changes into the second '
  569 + 'branch.'),
  570 + fn=cmd_harvest)
  571 +
  572 +def_cmd(
  573 + name='help',
  574 + short=['h'],
  575 + fn=cmd_help,
  576 + usage='help <command>',
  577 + help='Display help for legit command.')
  578 +
  579 +def_cmd(
  580 + name='install',
  581 + fn=cmd_install,
  582 + usage='install',
  583 + help='Installs legit git aliases.')
  584 +
  585 +def_cmd(
  586 + name='publish',
  587 + short=['pub'],
  588 + fn=cmd_publish,
  589 + usage='publish <branch>',
  590 + help='Publishes specified branch to the remote.')
  591 +
  592 +def_cmd(
  593 + name='settings',
  594 + fn=cmd_settings,
  595 + usage='settings',
  596 + help='Opens legit settings in a text editor.')
  597 +
  598 +def_cmd(
  599 + name='sprout',
  600 + short=['sp'],
  601 + fn=cmd_sprout,
  602 + usage='sprout [<branch>] <new-branch>',
  603 + help=('Creates a new branch off of the specified branch. Defaults to '
  604 + 'current branch. Switches to it immediately.'))
  605 +
  606 +def_cmd(
  607 + name='switch',
  608 + short=['sw'],
  609 + fn=cmd_switch,
  610 + usage='switch <branch>',
  611 + help=('Switches to specified branch. Automatically stashes and unstashes '
  612 + 'any changes.'))
  613 +
  614 +def_cmd(
  615 + name='sync',
  616 + short=['sy'],
  617 + fn=cmd_sync,
  618 + usage='sync <branch>',
  619 + help=('Syncronizes the given branch. Defaults to current branch. Stash, '
  620 + 'Fetch, Auto-Merge/Rebase, Push, and Unstash.'))
  621 +
  622 +def_cmd(
  623 + name='unpublish',
  624 + short=['unp'],
  625 + fn=cmd_unpublish,
  626 + usage='unpublish <branch>',
  627 + help='Removes specified branch from the remote.')

0 comments on commit 7981146

Please sign in to comment.
Something went wrong with that request. Please try again.