diff options
author | W. Trevor King <wking@drexel.edu> | 2008-11-27 09:26:52 -0500 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2008-11-27 09:26:52 -0500 |
commit | adb7e854b56aa7c3df6fae677fe383f417e364c4 (patch) | |
tree | a396ad3ba0d175f8a157c2e8ec6a19240b56510e /libbe/cmdutil.py | |
parent | 998525bf43305b1d53927975621ab9f211d0ed95 (diff) | |
download | bugseverywhere-adb7e854b56aa7c3df6fae677fe383f417e364c4.tar.gz |
Basic bash completion is now supported.
I'm still working on a clean implementation though...
Diffstat (limited to 'libbe/cmdutil.py')
-rw-r--r-- | libbe/cmdutil.py | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py index 1a321e9..aad6bbe 100644 --- a/libbe/cmdutil.py +++ b/libbe/cmdutil.py @@ -18,6 +18,7 @@ import optparse import os from textwrap import TextWrapper from StringIO import StringIO +import sys import doctest import bugdir @@ -34,12 +35,17 @@ class UserErrorWrap(UserError): UserError.__init__(self, str(exception)) self.exception = exception -class GetHelp(Exception): +class UsageError(Exception): pass -class UsageError(Exception): +class GetHelp(Exception): pass +class GetCompletions(Exception): + def __init__(self, completions=[]): + msg = "Get allowed completions" + Exception.__init__(self, msg) + self.completions = completions def iter_commands(): for name, module in plugin.iter_plugins("becommands"): @@ -80,15 +86,32 @@ def help(cmd=None): ret.append("be %s%*s %s" % (name, numExtraSpaces, "", desc)) return "\n".join(ret) +def options(cmd=None): + if cmd != None: + parser = get_command(cmd).get_parser() + longopts = [] + for opt in parser.option_list: + longopts.append(opt.get_opt_string()) + return longopts + else: + # These probably shouldn't be hardcoded... + return ["--help","--commands","--options"] + def raise_get_help(option, opt, value, parser): raise GetHelp - + +def raise_get_completions(option, opt, value, parser): + raise GetCompletions(options(sys.argv[1])) + class CmdOptionParser(optparse.OptionParser): def __init__(self, usage): optparse.OptionParser.__init__(self, usage) self.remove_option("-h") self.add_option("-h", "--help", action="callback", callback=raise_get_help, help="Print a help message") + self.add_option("--options", action="callback", + callback=raise_get_completions, + help="Print a list of available long options") def error(self, message): raise UsageError(message) |