Improved the hm command a little more (made argument handling easier)

This commit is contained in:
Nick Bolton 2010-04-11 02:22:15 +00:00
parent 63d8668497
commit 10a3a5300c
2 changed files with 736 additions and 659 deletions

File diff suppressed because it is too large Load Diff

73
hm.py
View File

@ -17,29 +17,29 @@
import sys, os import sys, os
from build import commands from build import commands
# Valid commands. # list of valid commands as keys. the values are optarg strings, but most
cmd_list = [ # are None for now (this is mainly for extensibility)
'about', cmd_dict = {
'setup', 'about' : [None, []],
'configure', 'setup' : [None, []],
'build', 'configure' : [None, []],
'clean', 'build' : ['dr', []],
'update', 'clean' : ['dr', []],
'install', 'update' : [None, []],
'package', 'install' : [None, []],
'dist', 'package' : [None, []],
'open', 'destroy' : [None, []],
'destroy', 'kill' : [None, []],
'kill', 'usage' : [None, []],
'usage', 'revision' : [None, []],
'revision', 'hammer' : [None, []],
'hammer', 'reformat' : [None, []],
'reformat', 'open' : [None, []],
] }
# aliases to valid commands
cmd_alias_dict = { cmd_alias_dict = {
'info' : 'usage', 'info' : 'about',
'about' : 'usage',
'help' : 'usage', 'help' : 'usage',
'dist' : 'package', 'dist' : 'package',
'make' : 'build', 'make' : 'build',
@ -49,7 +49,7 @@ cmd_alias_dict = {
def complete_command(arg): def complete_command(arg):
completions = [] completions = []
for cmd in cmd_list: for cmd, optarg in cmd_dict.iteritems():
if cmd.startswith(arg): if cmd.startswith(arg):
completions.append(cmd) completions.append(cmd)
@ -60,6 +60,7 @@ def complete_command(arg):
return completions return completions
def start_cmd(argv): def start_cmd(argv):
cmd_arg = '' cmd_arg = ''
if len(argv) > 1: if len(argv) > 1:
cmd_arg = argv[1] cmd_arg = argv[1]
@ -70,18 +71,20 @@ def start_cmd(argv):
completions = complete_command(cmd_arg) completions = complete_command(cmd_arg)
if len(completions) > 0: if cmd_arg and len(completions) > 0:
if len(completions) == 1: if len(completions) == 1:
# get the only completion (since in this case we have 1) # get the only completion (since in this case we have 1)
cmd = completions[0] cmd = completions[0]
# build up the first part of the map (for illustrative purposes)
cmd_map = list() cmd_map = list()
if cmd_arg != cmd: if cmd_arg != cmd:
cmd_map.append(cmd_arg) cmd_map.append(cmd_arg)
cmd_map.append(cmd) cmd_map.append(cmd)
# map an alias to the command, and build up the map
if cmd in cmd_alias_dict.keys(): if cmd in cmd_alias_dict.keys():
alias = cmd alias = cmd
if cmd_arg == cmd: if cmd_arg == cmd:
@ -89,20 +92,23 @@ def start_cmd(argv):
cmd = cmd_alias_dict[cmd] cmd = cmd_alias_dict[cmd]
cmd_map.append(cmd) cmd_map.append(cmd)
# show command map to avoid confusion
if len(cmd_map) != 0: if len(cmd_map) != 0:
print 'Mapping command: %s' % ' -> '.join(cmd_map) print 'Mapping command: %s' % ' -> '.join(cmd_map)
# use reflection to get the function pointer # pass args and optarg data to command handler, which figures out
cmd_func = getattr(commands, cmd) # how to handle the arguments
optarg_data = cmd_dict[cmd]
handler = commands.CommandHandler(argv[2:], optarg_data)
if cmd_func: # use reflection to get the function pointer
# run the function with all of the remaining args cmd_func = getattr(handler, cmd)
cmd_func(argv[2:]) cmd_func()
else: return 0
print 'Command not yet implemented:', cmd
else: else:
print ('Command `%s` too ambiguous, ' print (
'Command `%s` too ambiguous, '
'could mean any of: %s' 'could mean any of: %s'
) % (cmd_arg, ', '.join(completions)) ) % (cmd_arg, ', '.join(completions))
else: else:
@ -114,6 +120,9 @@ def start_cmd(argv):
commands.usage(argv[2:]) commands.usage(argv[2:])
# generic error code if not returned sooner
return 1
def main(argv): def main(argv):
if sys.version_info < (2, 4): if sys.version_info < (2, 4):
@ -121,7 +130,7 @@ def main(argv):
sys.exit(1) sys.exit(1)
try: try:
start_cmd(argv) sys.exit(start_cmd(argv))
except KeyboardInterrupt: except KeyboardInterrupt:
print '\n\nUser aborted, exiting.' print '\n\nUser aborted, exiting.'