Skip to content

Commit abe114c

Browse files
committed
Added escape for Sphinx characters to default value
1 parent bf1429a commit abe114c

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

sphinxarg/ext.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,9 @@ def print_action_groups(data, nested_content, markdown_help=False, settings=None
142142
'"==SUPPRESS=="',
143143
'==SUPPRESS==',
144144
]:
145-
if entry['default'] == '':
146-
arg.append('Default: ""')
147-
else:
148-
arg.append(f"Default: {entry['default']}")
145+
# Put the default value in a literal block - but escape backticks already in the string
146+
default_str = str(entry['default']).replace('`', '\`')
147+
arg.append(f"Default: ``{default_str}``")
149148

150149
# Handle nested content, the term used in the dict has the comma removed for simplicity
151150
desc = arg

sphinxarg/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def parse_parser(parser, data=None, **kwargs):
111111
# Quote default values for string/None types
112112
default = action.default
113113
if action.default not in ['', None, True, False] and action.type in [None, str] and isinstance(action.default, str):
114-
default = f'"{default}"'
114+
default = f"'{default}'"
115115

116116
# fill in any formatters, like %(default)s
117117
format_dict = dict(vars(action), prog=data.get('prog', ''), default=default)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Special Characters
2+
==================
3+
4+
.. argparse::
5+
:filename: test/sample-directive-special.py
6+
:func: get_parser

test/sample-directive-special.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import argparse
2+
3+
4+
def get_parser():
5+
parser = argparse.ArgumentParser(
6+
prog='sample-directive-special',
7+
description='Support SphinxArgParse HTML testing (with defaults)',
8+
)
9+
10+
parser.add_argument(
11+
'--some-int',
12+
help='Regular scalar input with default value',
13+
default=420,
14+
type=int,
15+
)
16+
parser.add_argument(
17+
'--some-text',
18+
help='Scalar text input',
19+
default="*.rst _txt_ **strong** *italic* ``code``",
20+
)
21+
parser.add_argument(
22+
'--list-text',
23+
help='List input for some bits of text',
24+
default=["*.rst", "_txt_", "**strong**", "*italic*", "``code``"],
25+
nargs="+",
26+
)
27+
28+
return parser

test/test_default_html.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
(".//section[@id='positional-arguments']", ''),
3030
(".//section[@id='positional-arguments']/dl/dt[1]/kbd", 'baz'),
3131
],
32+
'special-characters.html': [
33+
(".//h1", 'Sample', False),
34+
(".//h1", 'Special Characters'),
35+
(".//section/dl/dd/p", 'Default:'),
36+
(".//section/dl/dd/p/code/span", '420'),
37+
(".//section/dl/dd/p/code/span", "'*.rst"),
38+
(".//section/dl/dd/p/code/span", r"\['\*.rst',"),
39+
],
3240
}
3341
),
3442
)

0 commit comments

Comments
 (0)