summaryrefslogtreecommitdiff
path: root/cliapp/app.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2013-01-19 20:31:51 +0000
committerLars Wirzenius <liw@liw.fi>2013-01-19 20:31:51 +0000
commite1ad86a167c4179e9ea521c56b460293f3f19796 (patch)
treebf8a4f9b078c571e3abef344250b83fb4c91f679 /cliapp/app.py
parente5cf4ec31dbd7ddd8b4a97b7da2595f860015726 (diff)
downloadcliapp-e1ad86a167c4179e9ea521c56b460293f3f19796.tar.gz
Make help subcommand show full documentation for named commands
Diffstat (limited to 'cliapp/app.py')
-rw-r--r--cliapp/app.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/cliapp/app.py b/cliapp/app.py
index e01d48a..ab1c24b 100644
--- a/cliapp/app.py
+++ b/cliapp/app.py
@@ -26,6 +26,7 @@ import sys
import traceback
import time
import platform
+import textwrap
import cliapp
@@ -267,9 +268,16 @@ class Application(object):
width = 78
fmt = cliapp.TextFormat(width=width)
- usage = self._format_usage()
- description = fmt.format(self._format_description())
- text = '%s\n\n%s' % (usage, description)
+
+ if args:
+ usage = self._format_usage_for(args[0])
+ description = fmt.format(self._format_subcommand_help(args[0]))
+ text = '%s\n\n%s' % (usage, description)
+ else:
+ usage = self._format_usage()
+ description = fmt.format(self._format_description())
+ text = '%s\n\n%s' % (usage, description)
+
text = self.settings.progname.join(text.split('%prog'))
self.output.write(text)
@@ -299,6 +307,10 @@ class Application(object):
else:
return None
+ def _format_usage_for(self, cmd):
+ args = self.cmd_synopsis.get(cmd, '') or ''
+ return 'Usage: %%prog [options] %s %s' % (cmd, args)
+
def _format_description(self):
'''Format OptionParser description, with subcommand support.'''
if self.subcommands:
@@ -320,6 +332,12 @@ class Application(object):
summary = ''
return '* %%prog %s: %s\n' % (cmd, summary)
+ def _format_subcommand_help(self, cmd): # pragma: no cover
+ method = self.subcommands[cmd]
+ doc = method.__doc__ or ''
+ first, rest = doc.split('\n', 1)
+ return first + '\n' + textwrap.dedent(rest)
+
def _format_subcommand_description(self, cmd): # pragma: no cover
def remove_empties(lines):