Skip to content

Commit 87de191

Browse files
committed
Moved color setting to conf.py
1 parent f00a25e commit 87de191

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

docs/misc.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,3 @@ 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.

docs/usage.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ The next sections will describe the options in more detail.
9595
sphinxarg_commands_by_group_index_file_suffix = "by-group"
9696
sphinxarg_commands_by_group_index_title = "Commands by Group"
9797
98+
sphinxarg_ansi_colors = False
9899
99-
.. _about-subcommands:
100100
101+
.. _about-subcommands:
101102

102103
About Sub-Commands
103104
==================
@@ -251,3 +252,11 @@ You can configure Sphinx to use the additional template for an index by modifyin
251252
app.connect('html-page-context', page_template)
252253
253254
For more information, refer to the Sphinx documentation for :ref:`sphinx:templating` and the :doc:`sphinx:extdev/appapi`.
255+
256+
257+
Argparse color
258+
==============
259+
260+
Since Python 3.14, ``argparse`` uses `colors <https://docs.python.org/3/library/argparse.html#color>`__ in the output by default.
261+
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.
262+
You can override this default behaviour by adding ``sphinxarg_ansi_colors = False`` to your Sphinx ``conf.py``, now the color setting of the argument parser is enabled.

sphinxarg/ext.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ class ArgParseDirective(SphinxDirective):
322322
'markdown': flag,
323323
'markdownhelp': flag,
324324
'index-groups': unchanged,
325-
'color': flag,
326325
}
327326
index_groups: Sequence[str] = ()
328327

@@ -766,8 +765,8 @@ def run(self):
766765

767766
# Argparse in Python 3.14 uses ANSI color codes by default (#72)
768767
if hasattr(parser, 'color'):
769-
parser.color = 'color' in self.options
770-
# Disable colors, unless a flag is present in the user-settings
768+
parser.color = self.env.config.sphinxarg_ansi_colors
769+
# Disable colors, unless a flag is present in the config
771770

772771
result = parse_parser(
773772
parser,
@@ -1030,6 +1029,8 @@ def setup(app: Sphinx):
10301029
'sphinxarg_commands_by_group_index_title', CommandsByGroupIndex.localname, 'html', str
10311030
)
10321031

1032+
app.add_config_value('sphinxarg_ansi_colors', False, 'html', bool)
1033+
10331034
app.connect('builder-inited', configure_ext)
10341035
app.connect('build-finished', _delete_temporary_files)
10351036
return {

0 commit comments

Comments
 (0)