Skip to content

Formatters

Opt-in argparse help formatters — defaults-in-help and ANSI color — selected via a class's _help_formatter_ attribute.

duho.formatters

Opt-in argparse help formatters (F8): defaults-in-help + ANSI color.

Both formatters are plain :class:argparse.HelpFormatter subclasses a class opts into via the sandwich-named _help_formatter_ attribute, which Args._parser_ plumbs into argparse's formatter_class. They are off by default -- duho's --help output is unchanged unless a class sets _help_formatter_.

  • :class:DefaultsFormatter -- append (default: X) to each option's help, but (unlike argparse's own ArgumentDefaultsHelpFormatter) skip the noise of None/""/False defaults.
  • :class:ColorHelpFormatter -- ANSI-color section headings and option flags, gated on a TTY (and NO_COLOR/FORCE_COLOR). When color is off the output is byte-identical to the base formatter, so alignment and piping are unaffected.
  • :class:ColorDefaultsFormatter -- both composed.

The ANSI codes reuse logging.py's _asicode (hard-coded escapes -- no colorama import, so import duho pays nothing for these).

__all__ = ['DefaultsFormatter', 'ColorHelpFormatter', 'ColorDefaultsFormatter'] module-attribute

ColorDefaultsFormatter(*args, **kwargs)

Bases: ColorHelpFormatter, DefaultsFormatter

Compose :class:ColorHelpFormatter + :class:DefaultsFormatter.

Colors headings/flags AND appends (default: X) -- the batteries-included pretty-help formatter. The two mix cleanly: color overrides start_section / _format_action_invocation, defaults overrides _get_help_string.

Source code in src/duho/formatters.py
 98
 99
100
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self._duho_color = _color_enabled()

ColorHelpFormatter(*args, **kwargs)

Bases: HelpFormatter

ANSI-color section headings and option flags, when color is enabled.

Color is resolved once at formatter construction via :func:_color_enabled (NO_COLOR/FORCE_COLOR/TTY). When it is OFF, every override falls through to the base :class:argparse.HelpFormatter, so the output -- and its column alignment -- is byte-identical to duho's default help. When ON, section headings are bold and option invocations (-v, --verbose) are colored.

Source code in src/duho/formatters.py
 98
 99
100
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self._duho_color = _color_enabled()

start_section(heading)

Source code in src/duho/formatters.py
102
103
104
105
def start_section(self, heading):
    if self._duho_color and heading is not None:
        heading = f"{_HEADING_CODE}{heading}{_RESET}"
    super().start_section(heading)

DefaultsFormatter

Bases: HelpFormatter

Append (default: X) to each option's help, skipping empty defaults.

Like argparse's own ArgumentDefaultsHelpFormatter but it does NOT add the suffix when the effective default is None, "" or False (an unset optional, a store_true flag) -- those contribute noise, not information. An explicit %(default)s already in the help text is left untouched, and a SUPPRESS-defaulted action (--help/--version, inherited-suppressed fields) never gains a suffix.