Skip to content

Commit cf1fbb7

Browse files
authored
Added default feature to disable ArgumentParser coloring (fixing #72) (#74)
1 parent 2a2a202 commit cf1fbb7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

docs/misc.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ Linking to action groups
3232
------------------------
3333

3434
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.
35+
36+
37+
Argparse color
38+
--------------
39+
40+
Since Python 3.14, ``argparse`` uses `colors <https://docs.python.org/3/library/argparse.html#color>`__ in the output by default.
41+
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.
42+
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.

sphinxarg/ext.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ class ArgParseDirective(SphinxDirective):
299299
'nodescription': unchanged,
300300
'markdown': flag,
301301
'markdownhelp': flag,
302+
'color': flag,
302303
}
303304

304305
def _construct_manpage_specific_structure(self, parser_info):
@@ -538,6 +539,12 @@ def run(self):
538539
path = str(self.options['path'])
539540
if 'prog' in self.options:
540541
parser.prog = self.options['prog']
542+
543+
# Argparse in Python 3.14 uses ANSI color codes by default (#72)
544+
if hasattr(parser, 'color'):
545+
parser.color = 'color' in self.options
546+
# Disable colors, unless a flag is present in the user-settings
547+
541548
result = parse_parser(
542549
parser,
543550
skip_default_values='nodefault' in self.options,

0 commit comments

Comments
 (0)