diff options
-rw-r--r-- | cliapp/genman.py | 19 | ||||
-rw-r--r-- | cliapp/settings.py | 8 | ||||
-rw-r--r-- | example.1.in | 3 |
3 files changed, 21 insertions, 9 deletions
diff --git a/cliapp/genman.py b/cliapp/genman.py index 79d1ad2..59b5416 100644 --- a/cliapp/genman.py +++ b/cliapp/genman.py @@ -30,14 +30,25 @@ class ManpageGenerator(object): return sorted(self.parser.option_list, key=lambda o: (o._long_opts + o._short_opts)[0]) + def format_template(self): + sections = (('SYNOPSIS', self.format_synopsis()), + ('OPTIONS', self.format_options())) + text = self.template + for section, contents in sections: + pattern = '\n.SH %s\n' % section + text = text.replace(pattern, pattern + contents) + return text + def format_synopsis(self): lines = [] + lines += ['.nh'] lines += ['.B %s' % self.esc_dashes(self.parser.prog)] for option in self.options: for spec in self.format_option_for_synopsis(option): lines += ['.RB [ %s ]' % spec] + lines += ['.hy'] return ''.join('%s\n' % line for line in lines) def format_option_for_synopsis(self, option): @@ -48,7 +59,11 @@ class ManpageGenerator(object): for name in option._short_opts + option._long_opts: yield '%s%s' % (self.esc_dashes(name), suffix) - def format_option(self, option): + def format_options(self): + return ''.join(self.format_option_for_options(option) + for option in self.options) + + def format_option_for_options(self, option): lines = [] lines += ['.TP'] shorts = [self.esc_dashes(x) for x in option._short_opts] @@ -58,7 +73,7 @@ class ManpageGenerator(object): else: longs = ['%s' % self.esc_dashes(x) for x in option._long_opts] - lines += ['.BP ' + ' ", " '.join(shorts + longs)] + lines += ['.BR ' + ' ", " '.join(shorts + longs)] lines += [self.esc_dots(self.expand_default(option).strip())] return ''.join('%s\n' % line for line in lines) diff --git a/cliapp/settings.py b/cliapp/settings.py index e48467d..4b2111c 100644 --- a/cliapp/settings.py +++ b/cliapp/settings.py @@ -398,9 +398,7 @@ class Settings(object): self._settingses[name].value = cp.get('config', name) def generate_manpage(self, o, os, value, p): # pragma: no cover - generator = genman.ManpageGenerator(value, p) - print generator.format_synopsis(), - for option in generator.options: - print generator.format_option(option), + template = open(value).read() + generator = genman.ManpageGenerator(template, p) + sys.stdout.write(generator.format_template()) sys.exit(0) - diff --git a/example.1.in b/example.1.in index d9228d2..dc3683f 100644 --- a/example.1.in +++ b/example.1.in @@ -18,7 +18,7 @@ example.py \- cliapp-based simplistic fgrep clone .SH SYNOPSIS .SH DESCRIPTION -.B example.py' +.B example.py is an example program for .BR cliapp , a Python framework for command line utilities. @@ -27,4 +27,3 @@ a Python framework for command line utilities. To find a pattern in a file: .IP python example.py --pattern=needle haystack.txt - |