@@ -903,20 +903,18 @@ def update_prog(self, prog: str) -> None:
903903 return
904904
905905 # argparse includes positional arguments that appear before the subcommand in its
906- # subparser prog strings. We must track these while iterating through actions.
907- positionals : list [str ] = []
908-
909- # Use a formatter to get the same argument strings that argparse uses.
910- formatter = self ._get_formatter ()
906+ # subparser prog strings. Track these while iterating through actions.
907+ positionals : list [argparse .Action ] = []
911908
912909 # Set the prog value for the parser's subcommands
913910 for action in self ._actions :
914911 if isinstance (action , argparse ._SubParsersAction ):
915- # Set the _SubParsersAction's _prog_prefix value. This ensures that any subcommands
916- # added later via add_parser() will have the correct prog value.
917- action ._prog_prefix = self .prog
918- if positionals :
919- action ._prog_prefix += " " + " " .join (positionals )
912+ # Use a formatter to generate _prog_prefix exactly as argparse does in
913+ # add_subparsers(). This ensures that any subcommands added later via
914+ # add_parser() will have the correct prog value.
915+ formatter = self ._get_formatter ()
916+ formatter .add_usage (self .usage , positionals , self ._mutually_exclusive_groups , '' )
917+ action ._prog_prefix = formatter .format_help ().strip ()
920918
921919 # The keys of action.choices are subcommand names as well as subcommand aliases.
922920 # The aliases point to the same parser as the actual subcommand. We want to avoid
@@ -944,7 +942,7 @@ def update_prog(self, prog: str) -> None:
944942
945943 # Save positional argument
946944 if not action .option_strings :
947- positionals .append (formatter . _format_args ( action , action . dest ) )
945+ positionals .append (action )
948946
949947 def _find_parser (self , subcommand_path : Iterable [str ]) -> 'Cmd2ArgumentParser' :
950948 """Find a parser in the hierarchy based on a sequence of subcommand names.
0 commit comments