Skip to content

Commit f00a25e

Browse files
committed
Added default feature to disable ArgumentParser coloring (fixing #72)
1 parent 1a02efc commit f00a25e

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
@@ -322,6 +322,7 @@ class ArgParseDirective(SphinxDirective):
322322
'markdown': flag,
323323
'markdownhelp': flag,
324324
'index-groups': unchanged,
325+
'color': flag,
325326
}
326327
index_groups: Sequence[str] = ()
327328

@@ -762,6 +763,12 @@ def run(self):
762763
path = str(self.options['path'])
763764
if 'prog' in self.options:
764765
parser.prog = self.options['prog']
766+
767+
# Argparse in Python 3.14 uses ANSI color codes by default (#72)
768+
if hasattr(parser, 'color'):
769+
parser.color = 'color' in self.options
770+
# Disable colors, unless a flag is present in the user-settings
771+
765772
result = parse_parser(
766773
parser,
767774
skip_default_values='nodefault' in self.options,

0 commit comments

Comments
 (0)