Skip to content

Commit 4e80994

Browse files
committed
chore: improve create_command_partials runtime by not calling subprocess several times
1 parent 89c4aa2 commit 4e80994

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

build_helpers/create_command_partials.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import os
12
import subprocess # noqa: S404, RUF100
23
import sys
4+
from io import StringIO
35
from pathlib import Path
46

57

@@ -8,7 +10,20 @@ def _write_partial_file(filename: str, content: str):
810
f.write(f"``` output\n{content}\n```\n")
911

1012

13+
def _get_help_output(parser) -> str:
14+
"""Capture the help output from a parser."""
15+
output = StringIO()
16+
parser.print_help(file=output)
17+
return output.getvalue()
18+
19+
1120
def extract_command_partials():
21+
# Set terminal width to 80 columns for consistent output formatting
22+
os.environ["COLUMNS"] = "80"
23+
24+
# Import Arguments here to avoid circular imports and ensure COLUMNS is set
25+
from freqtrade.commands.arguments import Arguments
26+
1227
subcommands = [
1328
"trade",
1429
"create-userdir",
@@ -46,16 +61,35 @@ def extract_command_partials():
4661
"recursive-analysis",
4762
]
4863

49-
result = subprocess.run(["freqtrade", "--help"], capture_output=True, text=True)
64+
# Build the Arguments class to get the parser with all subcommands
65+
args = Arguments(None)
66+
args._build_subcommands()
5067

51-
_write_partial_file("docs/commands/main.md", result.stdout)
68+
# Get main help output
69+
main_help = _get_help_output(args.parser)
70+
_write_partial_file("docs/commands/main.md", main_help)
71+
72+
# Get subparsers from the main parser
73+
# The subparsers are stored in _subparsers._group_actions[0].choices
74+
subparsers_action = None
75+
for action in args.parser._subparsers._group_actions:
76+
if hasattr(action, "choices"):
77+
subparsers_action = action
78+
break
79+
80+
if subparsers_action is None:
81+
raise RuntimeError("Could not find subparsers in the main parser")
5282

5383
for command in subcommands:
5484
print(f"Running for {command}")
55-
result = subprocess.run(["freqtrade", command, "--help"], capture_output=True, text=True)
56-
57-
_write_partial_file(f"docs/commands/{command}.md", result.stdout)
85+
if command in subparsers_action.choices:
86+
subparser = subparsers_action.choices[command]
87+
help_output = _get_help_output(subparser)
88+
_write_partial_file(f"docs/commands/{command}.md", help_output)
89+
else:
90+
print(f" Warning: subcommand '{command}' not found in parser")
5891

92+
# freqtrade-client still uses subprocess as requested
5993
print("Running for freqtrade-client")
6094
result_client = subprocess.run(["freqtrade-client", "--show"], capture_output=True, text=True)
6195

0 commit comments

Comments
 (0)