From aacb372d326b33f7398b678789d7a25857beaf4d Mon Sep 17 00:00:00 2001 From: RobertoRoos Date: Tue, 25 Nov 2025 13:21:00 +0100 Subject: [PATCH] Added default feature to disable ArgumentParser coloring (fixing #72) --- docs/misc.rst | 8 ++++++++ sphinxarg/ext.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/docs/misc.rst b/docs/misc.rst index 274c4418..0e00950c 100644 --- a/docs/misc.rst +++ b/docs/misc.rst @@ -32,3 +32,11 @@ Linking to action groups ------------------------ As of version 0.2.0, action groups (e.g., "Optional arguments", "Required arguments", and subcommands) can be included in tables of contents and external links. The anchor name is the same as the title name (e.g., "Optional arguments"). In cases where titles are duplicated, as is often the case when subcommands are used, ``_repeatX``, where ``X`` is a number, is prepended to duplicate anchor names to ensure that they can all be uniquely linked. + + +Argparse color +-------------- + +Since Python 3.14, ``argparse`` uses `colors `__ in the output by default. +These ANSI color sequences won't show right at all in the Sphinx output, so this extension will disable colors first in your ``ArgumentParser`` instance before producing the output. +You can override this default behaviour by passing the ``:color:`` flag to your ``.. argparse`` directive, in this case the color setting of the argument parser is enabled. diff --git a/sphinxarg/ext.py b/sphinxarg/ext.py index 020aed2e..d318b0ad 100644 --- a/sphinxarg/ext.py +++ b/sphinxarg/ext.py @@ -299,6 +299,7 @@ class ArgParseDirective(SphinxDirective): 'nodescription': unchanged, 'markdown': flag, 'markdownhelp': flag, + 'color': flag, } def _construct_manpage_specific_structure(self, parser_info): @@ -538,6 +539,12 @@ def run(self): path = str(self.options['path']) if 'prog' in self.options: parser.prog = self.options['prog'] + + # Argparse in Python 3.14 uses ANSI color codes by default (#72) + if hasattr(parser, 'color'): + parser.color = 'color' in self.options + # Disable colors, unless a flag is present in the user-settings + result = parse_parser( parser, skip_default_values='nodefault' in self.options,