Use CmdOptionParser in "be".
authorW. Trevor King <wking@drexel.edu>
Mon, 13 Jul 2009 11:23:16 +0000 (07:23 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 13 Jul 2009 11:23:16 +0000 (07:23 -0400)
All the becommands have been using cmdutil CmdOptionParser for a long
time, but "be" parsed its options by hand.  Now it used
CmdOptionParser, which makes adding new options much easier.

be
libbe/cmdutil.py

diff --git a/be b/be
index b68a414ce7710622afb10e4a5c9e5db7b1e1cc28..5bdd6b2ac5f7bff3190f943c33668a43686a72f9 100755 (executable)
--- a/be
+++ b/be
 #    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)
index 0bee9db8f3529b38cd765b83ea8805317bcc7ee3..a91b1c7446da41520ac2385166ebd1a819af285e 100644 (file)
@@ -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):