Skip to content
Snippets Groups Projects
Commit a652e175 authored by Thomas Bellman's avatar Thomas Bellman
Browse files

Add a non-reflowing formatter for help texts.

We want to be able to write pre-formatted command descriptions in our
plugins, where the text is not reflowed/re-wrapped by optparse when
printing (in response to the --help option).  E.g, because they have
code (Nagios config snippets) in them that don't fare well when being
reflowed.

We add a class NoReflowHelpFormatter to trh_nagioslib which can be
used for this.  It will still dedent the description text, so it
can be written indented in the plugin source, if wanted.
parent 76970534
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,8 @@ import re ...@@ -13,6 +13,8 @@ import re
import collections import collections
import netsnmp import netsnmp
import subprocess import subprocess
import optparse
import textwrap
# Cache of mappings from short names to fully qualified names. # Cache of mappings from short names to fully qualified names.
...@@ -251,3 +253,21 @@ def nagios_report(statuses): ...@@ -251,3 +253,21 @@ def nagios_report(statuses):
message = "UNKNOWN: No status report\n" message = "UNKNOWN: No status report\n"
return max_level, message return max_level, message
class NoReflowHelpFormatter(optparse.IndentedHelpFormatter):
"""A HelpFormatter for optparse that does not re-wrap/reflow text.
Intended for command descriptions that are already properly
pre-formatted.
"""
def format_description(self, description):
if not description:
return ""
desc_width = min(70, self.width - self.current_indent)
indent = " " * self.current_indent
summary,body = (description.strip().split("\n", 1) + [""])[:2]
body = textwrap.dedent(body)
description = summary + "\n" + body + "\n"
return description
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment