chiark / gitweb /
chpwd, subcommand.py: Only show global options in admin context help.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 7 Apr 2015 18:52:10 +0000 (19:52 +0100)
chpwd
subcommand.py

diff --git a/chpwd b/chpwd
index 52d3fa3a0d14db98f08d4b66a133f1226dbca9dd..07fe830a85b4bc920afebf9692f5dfc0fb6c0b0f 100755 (executable)
--- a/chpwd
+++ b/chpwd
@@ -90,6 +90,7 @@ def parse_options():
   """
   global OPTS
   OPTS, args = OPTPARSE.parse_args()
+  OPTPARSE.show_global_opts = False
   ## It's tempting to load the configuration here.  Don't do that.  Some
   ## contexts will want to check that the command line was handled properly
   ## upstream before believing it for anything, such as executing arbitrary
@@ -299,7 +300,9 @@ if __name__ == '__main__':
         if ctx is None: ctx = 'userv'
       else:
         D.opendb()
-        if ctx is None: ctx = 'admin'
+        if ctx is None:
+          ctx = 'admin'
+          OPTPARSE.show_global_opts = True
       with OUT.redirect_to(O.FileOutput()):
         OPTPARSE.dispatch(ctx, args)
 
index d6d850e5a4f371b54a4b53e29c5a8a1ece7ec6cc..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