@@ -589,9 +589,11 @@ def script_base_command(script: Path) -> list[str]:
589589
590590 return [sys .executable , str (script )]
591591
592+
592593def _advertised_flags (help_text : str ) -> set [str ]:
593594 return {match .group (1 ) for match in _FLAG_PATTERN .finditer (help_text )}
594595
596+
595597@lru_cache (maxsize = None )
596598def script_help_text (script : Path ) -> str :
597599 try :
@@ -613,10 +615,15 @@ def determine_flag(script: Path, candidates: Sequence[str]) -> str | None:
613615 try :
614616 source = script .read_text (encoding = "utf-8" )
615617 except (OSError , UnicodeDecodeError ):
616- return None
618+ source = ""
619+
620+ for flag in candidates :
621+ if flag and flag in advertised :
622+ return flag
623+
617624 for flag in candidates :
618625 if flag and flag in source :
619- return flag
626+ return flag
620627 return None
621628
622629
@@ -633,6 +640,20 @@ def _script_supports_flag(
633640 return None
634641
635642
643+ def script_supports_subcommand (script : Path , name : str ) -> bool :
644+ """Return True if the script advertises a Typer-style subcommand."""
645+
646+ help_text = script_help_text (script )
647+ if "Commands:" not in help_text :
648+ return False
649+ _ , commands_section = help_text .split ("Commands:" , 1 )
650+ for line in commands_section .splitlines ():
651+ command_name = line .strip ().split (" " , 1 )[0 ] if line .strip () else ""
652+ if command_name == name :
653+ return True
654+ return False
655+
656+
636657def build_question_command (
637658 script : Path , prompt : str , base_args : List [str ]
638659) -> List [str ]:
@@ -641,7 +662,6 @@ def build_question_command(
641662 If the script advertises a question flag, use it; otherwise pass the prompt positionally.
642663 """
643664
644-
645665 argv : List [str ] = [* script_base_command (script ), * base_args ]
646666 flag = _script_supports_flag (script , ["--question" , "-q" ])
647667
@@ -651,28 +671,6 @@ def build_question_command(
651671 argv .append (prompt )
652672 return argv
653673
654-
655- def build_builder_command (
656- * ,
657- builder : Path ,
658- index_key : str ,
659- pdf_dir : Path ,
660- chunks_dir : Path ,
661- index_dir : Path ,
662- ) -> list [str ]:
663- """Construct the lc_build_index command for the verification run."""
664-
665- return [
666- * script_base_command (builder ),
667- index_key ,
668- "--input-dir" ,
669- str (pdf_dir ),
670- "--chunks-dir" ,
671- str (chunks_dir ),
672- "--index-dir" ,
673- str (index_dir ),
674- ]
675-
676674
677675def build_builder_command (
678676 * ,
@@ -883,14 +881,13 @@ def build_question_invocation(
883881 if script is None :
884882 raise RuntimeError ("No asker script available" )
885883 route = "multi" if use_multi else "asker"
886- def _append_flag (script_path : Path , args : list [str ], candidates : list [str ], value : str ) -> None :
884+
885+ def _append_flag (
886+ script_path : Path , args : list [str ], candidates : list [str ], value : str
887+ ) -> None :
887888 flag = determine_flag (script_path , candidates )
888889 if not flag :
889- # Typer-based CLIs such as multi_agent.py require running via ``-m`` to
890- # expose subcommand help. When invoked as a script from the harness the
891- # ``--help`` probe used by ``determine_flag`` fails, so fall back to the
892- # primary candidate.
893- flag = candidates [0 ]
890+ return
894891 args .extend ([flag , value ])
895892
896893 if not use_multi :
@@ -912,6 +909,9 @@ def _append_flag(script_path: Path, args: list[str], candidates: list[str], valu
912909 else :
913910 base_args : list [str ] = []
914911
912+ if script_supports_subcommand (multi , "ask" ):
913+ base_args .append ("ask" )
914+
915915 _append_flag (multi , base_args , ["--key" , "-k" ], index_key )
916916 _append_flag (multi , base_args , ["--index-dir" , "--index" ], str (index_dir ))
917917
0 commit comments