@@ -93,61 +93,96 @@ def inject_local_commands_into_help(result: list[dict]) -> list[dict]:
9393 return modified_result
9494
9595
96+ def _collect_server_commands (lines : list [str ]) -> set [str ]:
97+ """Collect all command names from server output."""
98+ server_commands : set [str ] = set ()
99+ for line in lines :
100+ if _is_command_line (line ):
101+ cmd_name = _get_command_name (line )
102+ if cmd_name :
103+ server_commands .add (cmd_name )
104+ return server_commands
105+
106+
107+ def _insert_remaining_at_end (
108+ new_lines : list [str ],
109+ remaining : list [str ],
110+ ) -> None :
111+ """Insert remaining commands after the last command line."""
112+ insert_idx = len (new_lines )
113+ for i in range (len (new_lines ) - 1 , - 1 , - 1 ):
114+ if _is_command_line (new_lines [i ]):
115+ insert_idx = i + 1
116+ break
117+ for j , cmd_line in enumerate (remaining ):
118+ new_lines .insert (insert_idx + j , cmd_line )
119+
120+
96121def _process_help_text_with_local_commands (
97122 text : str ,
98123 local_commands : dict [str , str ],
99124) -> str :
100125 """Process help text and inject local commands into COMMANDS section."""
101126 lines = text .split ("\n " )
102- new_lines = []
127+ new_lines : list [ str ] = []
103128 in_commands_section = False
104- injected : set [str ] = set ()
129+ is_all_commands = False
130+
131+ # Pre-collect server commands to avoid duplicates
132+ injected = _collect_server_commands (lines )
105133
106134 for line in lines :
107- if line .strip () in {"COMMANDS" , "ALL COMMANDS" }:
135+ stripped = line .strip ()
136+
137+ # Detect section headers
138+ if stripped in {"ALL COMMANDS" , "COMMANDS" }:
108139 in_commands_section = True
140+ is_all_commands = stripped == "ALL COMMANDS"
109141 new_lines .append (line )
110142 continue
111143
112- if in_commands_section and line .strip () and not line .startswith (" " ):
113- # Leaving COMMANDS section - inject remaining commands first
114- new_lines .extend (_inject_remaining_commands (local_commands , injected ))
144+ # Detect leaving commands section
145+ if in_commands_section and stripped and not line .startswith (" " ):
146+ new_lines .extend (
147+ _inject_remaining_commands (local_commands , injected , is_all_commands )
148+ )
115149 in_commands_section = False
116150
151+ # Inject local commands before current command if in section
117152 if in_commands_section and _is_command_line (line ):
118153 current_cmd = _get_command_name (line )
119154 if current_cmd :
120155 new_lines .extend (
121- _inject_commands_before (current_cmd , local_commands , injected )
156+ _inject_commands_before (
157+ current_cmd , local_commands , injected , is_all_commands
158+ )
122159 )
123160
124161 new_lines .append (line )
125162
126- # If still in commands section at end, inject remaining
163+ # Handle remaining commands at end of section
127164 if in_commands_section :
128- remaining = _inject_remaining_commands (local_commands , injected )
165+ remaining = _inject_remaining_commands (
166+ local_commands , injected , is_all_commands
167+ )
129168 if remaining :
130- # Insert after last command line
131- insert_idx = len (new_lines )
132- for i in range (len (new_lines ) - 1 , - 1 , - 1 ):
133- if _is_command_line (new_lines [i ]):
134- insert_idx = i + 1
135- break
136- for j , cmd_line in enumerate (remaining ):
137- new_lines .insert (insert_idx + j , cmd_line )
169+ _insert_remaining_at_end (new_lines , remaining )
138170
139171 return "\n " .join (new_lines )
140172
141173
142174def _inject_remaining_commands (
143175 local_commands : dict [str , str ],
144176 injected : set [str ],
177+ is_all_commands : bool = False ,
145178) -> list [str ]:
146179 """Return all local commands not yet injected."""
147180 lines = []
148181 for cmd in sorted (local_commands .keys ()):
149182 if cmd not in injected :
150- lines .append (_format_help_command (cmd , local_commands [cmd ]))
183+ lines .append (
184+ _format_help_command (cmd , local_commands [cmd ], is_all_commands )
185+ )
151186 injected .add (cmd )
152187 return lines
153188
@@ -167,16 +202,26 @@ def _inject_commands_before(
167202 current_cmd : str ,
168203 local_commands : dict [str , str ],
169204 injected : set [str ],
205+ is_all_commands : bool = False ,
170206) -> list [str ]:
171207 """Return local commands that should appear before current_cmd alphabetically."""
172208 lines = []
173209 for cmd in sorted (local_commands .keys ()):
174210 if cmd not in injected and cmd < current_cmd :
175- lines .append (_format_help_command (cmd , local_commands [cmd ]))
211+ lines .append (
212+ _format_help_command (cmd , local_commands [cmd ], is_all_commands )
213+ )
176214 injected .add (cmd )
177215 return lines
178216
179217
180- def _format_help_command (name : str , description : str ) -> str :
181- """Format a command entry for help output."""
182- return f" { name :16} { description } "
218+ def _format_help_command (name : str , description : str , wide : bool = False ) -> str :
219+ """Format a command entry for help output.
220+
221+ Args:
222+ name: Command name
223+ description: Command description
224+ wide: If True, use 24-char width (for --all mode), otherwise 16-char
225+ """
226+ width = 24 if wide else 16
227+ return f" { name :<{width }} { description } "
0 commit comments