summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cliapp/app.py21
-rw-r--r--example4.py49
-rw-r--r--without-tests1
3 files changed, 64 insertions, 7 deletions
diff --git a/cliapp/app.py b/cliapp/app.py
index 93ba966..e6eaeab 100644
--- a/cliapp/app.py
+++ b/cliapp/app.py
@@ -102,6 +102,7 @@ class Application(object):
self.subcommands = {}
self.subcommand_aliases = {}
+ self.hidden_subcommands = set()
for method_name in self._subcommand_methodnames():
cmd = self._unnormalize_cmd(method_name)
self.subcommands[cmd] = getattr(self, method_name)
@@ -239,7 +240,8 @@ class Application(object):
'''
- def add_subcommand(self, name, func, arg_synopsis=None, aliases=None):
+ def add_subcommand(
+ self, name, func, arg_synopsis=None, aliases=None, hidden=False):
'''Add a subcommand.
Normally, subcommands are defined by add ``cmd_foo`` methods
@@ -256,6 +258,8 @@ class Application(object):
self.subcommands[name] = func
self.cmd_synopsis[name] = arg_synopsis
self.subcommand_aliases[name] = aliases or []
+ if hidden: # pragma: no cover
+ self.hidden_subcommands.add(name)
def add_default_subcommands(self):
if 'help' not in self.subcommands:
@@ -296,15 +300,17 @@ class Application(object):
assert method.startswith('cmd_')
return method[len('cmd_'):].replace('_', '-')
- def _format_usage(self):
+ def _format_usage(self, all=False):
'''Format usage, possibly also subcommands, if any.'''
if self.subcommands:
lines = []
prefix = 'Usage:'
for cmd in sorted(self.subcommands.keys()):
- args = self.cmd_synopsis.get(cmd, '') or ''
- lines.append('%s %%prog [options] %s %s' % (prefix, cmd, args))
- prefix = ' ' * len(prefix)
+ if all or cmd not in self.hidden_subcommands:
+ args = self.cmd_synopsis.get(cmd, '') or ''
+ lines.append(
+ '%s %%prog [options] %s %s' % (prefix, cmd, args))
+ prefix = ' ' * len(prefix)
return '\n'.join(lines)
else:
return None
@@ -313,12 +319,13 @@ class Application(object):
args = self.cmd_synopsis.get(cmd, '') or ''
return 'Usage: %%prog [options] %s %s' % (cmd, args)
- def _format_description(self):
+ def _format_description(self, all=False):
'''Format OptionParser description, with subcommand support.'''
if self.subcommands:
summaries = []
for cmd in sorted(self.subcommands.keys()):
- summaries.append(self._format_subcommand_summary(cmd))
+ if all or cmd not in self.hidden_subcommands:
+ summaries.append(self._format_subcommand_summary(cmd))
cmd_desc = ''.join(summaries)
return '%s\n%s' % (self._description or '', cmd_desc)
else:
diff --git a/example4.py b/example4.py
new file mode 100644
index 0000000..94e009c
--- /dev/null
+++ b/example4.py
@@ -0,0 +1,49 @@
+# Copyright (C) 2013 Lars Wirzenius
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import cliapp
+import logging
+
+
+class ExampleApp(cliapp.Application):
+
+ def setup(self):
+ self.add_subcommand('insult', self.insult, hidden=True)
+
+ def cmd_greet(self, args):
+ '''Greet the user.
+
+ The user is treated to a a courteus,
+ but terse form of greeting.
+
+ '''
+ for arg in args:
+ self.output.write('greetings, %s\n' % arg)
+
+ def insult(self, args):
+ '''Insult the user. (hidden command)
+
+ Sometimes, though rarely, it happens that a user is really a bit of
+ a prat, and needs to be told off. This is the command for that.
+
+ '''
+ for arg in args:
+ self.output.write('you suck, %s\n' % arg)
+
+
+ExampleApp().run()
+
diff --git a/without-tests b/without-tests
index b362003..9f87e60 100644
--- a/without-tests
+++ b/without-tests
@@ -9,3 +9,4 @@
./test-plugins/wrongversion_plugin.py
./test-plugins/aaa_hello_plugin.py
example3.py
+example4.py