chiark / gitweb /
chpwd, subcommand.py: Only show global options in admin context help.
[chopwood] / subcommand.py
index b2859155618f4628125a81be02420d4e09110b72..c8644be788245ec07c53407c47b237c95dae00b5 100644 (file)
@@ -285,7 +285,8 @@ class SubcommandOptionParser (OP.OptionParser, object):
   """
 
   def __init__(me, usage = '%prog [-OPTIONS] COMMAND [ARGS ...]',
-               contexts = ['cli'], commands = [], *args, **kw):
+               contexts = ['cli'], commands = [], show_global_opts = True,
+               *args, **kw):
     """
     Constructor for the options parser.  As for the superclass, but with an
     additional argument CONTEXTS used for initializing the `help' command.
@@ -297,6 +298,7 @@ class SubcommandOptionParser (OP.OptionParser, object):
     ## eat the subcommand's arguments.
     me.disable_interspersed_args()
     me.context = list(contexts)[0]
+    me.show_global_opts = show_global_opts
 
     ## Provide a default `help' command.
     me._cmds = {}
@@ -318,7 +320,11 @@ class SubcommandOptionParser (OP.OptionParser, object):
     synopses for the available subcommands.
     """
     if file is None: file = SYS.stdout
-    super(SubcommandOptionParser, me).print_help(file = file, *args, **kw)
+    if me.show_global_opts:
+      super(SubcommandOptionParser, me).print_help(file = file, *args, **kw)
+    else:
+      file.write(me.get_usage() + '\n')
+      file.write(me.get_description())
     file.write('\nCommands:\n')
     for sub in sorted(set(me._cmds.values()), key = lambda c: c.name):
       if sub.desc is None or me.context not in sub.contexts: continue
@@ -392,12 +398,10 @@ class SubcommandOptionParser (OP.OptionParser, object):
 ## ready to roll.
 COMMANDS = []
 
-def subcommand(name, contexts, desc, cls = Subcommand,
-               opts = [], params = [], oparams = [], rparam = None):
+def subcommand(name, contexts, desc, cls = Subcommand, *args, **kw):
   """Decorator for defining subcommands."""
   def _(func):
-    COMMANDS.append(cls(name, contexts, desc, func,
-                        opts, params, oparams, rparam))
+    COMMANDS.append(cls(name, contexts, desc, func, *args, **kw))
   return _
 
 ###----- That's all, folks --------------------------------------------------