[Docs] Reduce noise in docs and --help from the JSON tip (#22567)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-08-09 08:31:32 -07:00
committed by GitHub
parent 1bf5e1f25b
commit 56186474f6
4 changed files with 40 additions and 24 deletions
+18 -3
View File
@@ -1669,11 +1669,19 @@ class FlexibleArgumentParser(ArgumentParser):
"""ArgumentParser that allows both underscore and dash in names."""
_deprecated: set[Action] = set()
_json_tip: str = (
"When passing JSON CLI arguments, the following sets of arguments "
"are equivalent:\n"
' --json-arg \'{"key1": "value1", "key2": {"key3": "value2"}}\'\n'
" --json-arg.key1 value1 --json-arg.key2.key3 value2\n\n"
"Additionally, list elements can be passed individually using +:\n"
' --json-arg \'{"key4": ["value3", "value4", "value5"]}\'\n'
" --json-arg.key4+ value3 --json-arg.key4+=\'value4,value5\'\n\n")
def __init__(self, *args, **kwargs):
# Set the default 'formatter_class' to SortedHelpFormatter
if 'formatter_class' not in kwargs:
kwargs['formatter_class'] = SortedHelpFormatter
# Set the default "formatter_class" to SortedHelpFormatter
if "formatter_class" not in kwargs:
kwargs["formatter_class"] = SortedHelpFormatter
super().__init__(*args, **kwargs)
if sys.version_info < (3, 13):
@@ -1715,6 +1723,13 @@ class FlexibleArgumentParser(ArgumentParser):
self._action_groups.append(group)
return group
def format_help(self) -> str:
# Add tip about JSON arguments to the epilog
epilog = self.epilog or ""
if not epilog.startswith(FlexibleArgumentParser._json_tip):
self.epilog = FlexibleArgumentParser._json_tip + epilog
return super().format_help()
def parse_args( # type: ignore[override]
self,
args: list[str] | None = None,