Skip to content

Commit 9eeeb0d

Browse files
committed
Use parser formatter for help
1 parent 640b411 commit 9eeeb0d

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

argmark/argmark.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _add_usage_section(md_file: MdUtils, parser: _argparse.ArgumentParser) -> No
3737
usage_string = parser.format_usage() if parser.format_usage() is not None else ""
3838
md_file.insert_code(usage_string, language="bash")
3939

40-
def _format_action_for_table_row(action: _argparse.Action) -> List[str]:
40+
def _format_action_for_table_row(action: _argparse.Action, parser: _argparse.ArgumentParser) -> List[str]:
4141
"""
4242
Formats a single argparse.Action into a list of 4 strings for the arguments table.
4343
Handles both optional and positional arguments based on action.option_strings.
@@ -72,8 +72,11 @@ def _format_action_for_table_row(action: _argparse.Action) -> List[str]:
7272
# Help text string formatting
7373
if action.help is None:
7474
help_text_str = inline_code("None")
75+
elif action.help == _argparse.SUPPRESS:
76+
help_text_str = ""
7577
else:
76-
help_text_str = str(action.help).replace("\n", " ")
78+
formatter = parser._get_formatter()
79+
help_text_str = formatter._expand_help(action).replace("\n", " ")
7780

7881
return [short_opt_str, long_opt_str, default_cell_str, help_text_str]
7982

@@ -93,7 +96,7 @@ def _build_arguments_table_data(parser: _argparse.ArgumentParser) -> List[str]:
9396

9497
# This pass is primarily for optionals.
9598
# _format_action_for_table_row handles based on action.option_strings
96-
table_rows_data.append(_format_action_for_table_row(action))
99+
table_rows_data.append(_format_action_for_table_row(action, parser))
97100
seen_action_ids.add(id(action))
98101

99102
# Second Pass (Positionals): Iterate through parser._actions
@@ -105,7 +108,7 @@ def _build_arguments_table_data(parser: _argparse.ArgumentParser) -> List[str]:
105108

106109
# If it has no option_strings, it's a positional argument
107110
if not action.option_strings:
108-
table_rows_data.append(_format_action_for_table_row(action))
111+
table_rows_data.append(_format_action_for_table_row(action, parser))
109112
# No need to add to seen_action_ids here as this is the final pass for this action
110113

111114
# Flatten the table_rows_data with the header

tests/answer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ usage: sample_argparse.py [-h] -f FILES [FILES ...] [-b BAR]
1818
|short|long|default|help|
1919
| :--- | :--- | :--- | :--- |
2020
|`-h`|`--help`||show this help message and exit|
21-
|`-f`|`--files`|`None`|Files to read.|
21+
|`-f`|`--files`|`None`|Files to read. (default: None)|
2222
|`-b`|`--bar`|`None`|`None`|

tests/answer_subparser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
app
23
===
34

0 commit comments

Comments
 (0)