diff options
-rwxr-xr-x | be | 74 | ||||
-rw-r--r-- | libbe/cmdutil.py | 23 |
2 files changed, 59 insertions, 38 deletions
@@ -19,38 +19,54 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - import sys + from libbe import cmdutil, _version -__doc__ == cmdutil.help() +__doc__ = cmdutil.help() + +usage = "be [options] [command] [command_options ...] [command_args ...]" + +parser = cmdutil.CmdOptionParser(usage) +parser.command = "be" +parser.add_option("--version", action="store_true", dest="version", + help="Print version string and exit") -if len(sys.argv) == 1 or sys.argv[1] in ('--help', '-h'): - print cmdutil.help() -elif sys.argv[1] == '--complete': - for command, module in cmdutil.iter_commands(): - print command - print '\n'.join(["--help","--complete","--options","--version"]) -elif sys.argv[1] == '--version': +try: + options,args = parser.parse_args() +except cmdutil.GetHelp: + print cmdutil.help(parser=parser) + sys.exit(0) +except cmdutil.GetCompletions, e: + print '\n'.join(e.completions) + sys.exit(0) + +if options.version == True: print _version.version_info["revision_id"] -else: - try: - sys.exit(cmdutil.execute(sys.argv[1], sys.argv[2:])) - except cmdutil.GetHelp: - print cmdutil.help(sys.argv[1]) - sys.exit(0) - except cmdutil.GetCompletions, e: - print '\n'.join(e.completions) - sys.exit(0) - except cmdutil.UnknownCommand, e: - print e - sys.exit(1) - except cmdutil.UsageError, e: - print "Invalid usage:", e - print "\nArgs:", sys.argv[1:] + sys.exit(0) + +try: + if len(args) == 0: + raise cmdutil.UsageError, "must supply a command" + sys.exit(cmdutil.execute(args[0], args[1:])) +except cmdutil.GetHelp: + print cmdutil.help(sys.argv[1]) + sys.exit(0) +except cmdutil.GetCompletions, e: + print '\n'.join(e.completions) + sys.exit(0) +except cmdutil.UnknownCommand, e: + print e + sys.exit(1) +except cmdutil.UsageError, e: + print "Invalid usage:", e + if len(args) == 0: + print cmdutil.help(parser=parser) + else: + print "\nArgs:", args print cmdutil.help(sys.argv[1]) - sys.exit(1) - except cmdutil.UserError, e: - print "ERROR:" - print e - sys.exit(1) + sys.exit(1) +except cmdutil.UserError, e: + print "ERROR:" + print e + sys.exit(1) diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py index 0bee9db..a91b1c7 100644 --- a/libbe/cmdutil.py +++ b/libbe/cmdutil.py @@ -75,7 +75,7 @@ def execute(cmd, args): cmd.execute([a.decode(enc) for a in args]) return 0 -def help(cmd=None): +def help(cmd=None, parser=None): if cmd != None: return get_command(cmd).help() else: @@ -84,17 +84,15 @@ def help(cmd=None): cmdlist.append((name, module.__desc__)) longest_cmd_len = max([len(name) for name,desc in cmdlist]) ret = ["Bugs Everywhere - Distributed bug tracking", - "", - "usage: be [command] [command_options ...] [command_args ...]", - "or: be help", - "or: be help [command]", - "", - "Supported commands"] + "", "Supported commands"] for name, desc in cmdlist: numExtraSpaces = longest_cmd_len-len(name) ret.append("be %s%*s %s" % (name, numExtraSpaces, "", desc)) - - return "\n".join(ret) + ret.extend(["", "Run", " be help [command]", "for more information."]) + longhelp = "\n".join(ret) + if parser == None: + return longhelp + return parser.help_str() + "\n" + longhelp def completions(cmd): parser = get_command(cmd).get_parser() @@ -108,6 +106,13 @@ def raise_get_help(option, opt, value, parser): def raise_get_completions(option, opt, value, parser): print "got completion arg" + if hasattr(parser, "command") and parser.command == "be": + comps = [] + for command, module in iter_commands(): + comps.append(command) + for opt in parser.option_list: + comps.append(opt.get_opt_string()) + raise GetCompletions(comps) raise GetCompletions(completions(sys.argv[1])) class CmdOptionParser(optparse.OptionParser): |