Skip to content

Commit 483fcca

Browse files
AlexTereshenkovstephenfin
authored andcommitted
Strip ANSI escape codes
1 parent de245b5 commit 483fcca

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

sphinx_click/ext.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _format_option(opt):
145145
if opt[1]:
146146
yield ''
147147
for line in statemachine.string2lines(
148-
opt[1], tab_width=4, convert_whitespace=True
148+
ANSI_ESC_SEQ_RE.sub('', opt[1]), tab_width=4, convert_whitespace=True
149149
):
150150
yield _indent(line)
151151

@@ -241,12 +241,13 @@ def _format_epilog(ctx):
241241
We parse this as reStructuredText, allowing users to embed rich
242242
information in their help messages if they so choose.
243243
"""
244-
epilog_string = ctx.command.epilog
245-
if not epilog_string:
244+
if not ctx.command.epilog:
246245
return
247246

248247
for line in statemachine.string2lines(
249-
epilog_string, tab_width=4, convert_whitespace=True
248+
ANSI_ESC_SEQ_RE.sub('', ctx.command.epilog),
249+
tab_width=4,
250+
convert_whitespace=True,
250251
):
251252
yield line
252253
yield ''

tests/test_formatter.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,23 @@ def hello(name):
269269
def test_ansi_escape_sequences(self):
270270
"""Validate that ANSI escape sequences are stripped."""
271271

272-
@click.command()
272+
@click.command(epilog='\033[31mA sample epilog.\033[0m')
273+
@click.option(
274+
'--name',
275+
help='Name to say \033[94mhello\033[0m to.',
276+
required=True,
277+
type=str,
278+
)
279+
@click.option(
280+
'--choice',
281+
help='A sample option with choices',
282+
type=click.Choice(['\033[94mOption1\033[0m', '\033[94mOption2\033[0m']),
283+
)
284+
@click.option(
285+
'--param',
286+
default=lambda: None,
287+
show_default='Something computed at \033[94mruntime\033[0m',
288+
)
273289
def foobar():
274290
"""A sample command with **sparkles**.
275291
@@ -293,6 +309,24 @@ def foobar():
293309
.. code-block:: shell
294310
295311
foobar [OPTIONS]
312+
313+
.. rubric:: Options
314+
315+
.. option:: --name <name>
316+
317+
**Required** Name to say hello to.
318+
319+
.. option:: --choice <choice>
320+
321+
A sample option with choices
322+
323+
:options: Option1 | Option2
324+
325+
.. option:: --param <param>
326+
327+
:default: Something computed at runtime
328+
329+
A sample epilog.
296330
"""
297331
).lstrip(),
298332
'\n'.join(output),

0 commit comments

Comments
 (0)