@@ -192,56 +192,6 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> Optional[bool]:
192
192
return arg_decorator
193
193
194
194
195
- def _set_parser_prog (parser : argparse .ArgumentParser , prog : str ) -> None :
196
- """Recursively set prog attribute of a parser and all of its subparsers.
197
-
198
- Does so that the root command is a command name and not sys.argv[0].
199
-
200
- :param parser: the parser being edited
201
- :param prog: new value for the parser's prog attribute
202
- """
203
- # Set the prog value for this parser
204
- parser .prog = prog
205
- req_args : list [str ] = []
206
-
207
- # Set the prog value for the parser's subcommands
208
- for action in parser ._actions :
209
- if isinstance (action , argparse ._SubParsersAction ):
210
- # Set the _SubParsersAction's _prog_prefix value. That way if its add_parser() method is called later,
211
- # the correct prog value will be set on the parser being added.
212
- action ._prog_prefix = parser .prog
213
-
214
- # The keys of action.choices are subcommand names as well as subcommand aliases. The aliases point to the
215
- # same parser as the actual subcommand. We want to avoid placing an alias into a parser's prog value.
216
- # Unfortunately there is nothing about an action.choices entry which tells us it's an alias. In most cases
217
- # we can filter out the aliases by checking the contents of action._choices_actions. This list only contains
218
- # help information and names for the subcommands and not aliases. However, subcommands without help text
219
- # won't show up in that list. Since dictionaries are ordered in Python 3.6 and above and argparse inserts the
220
- # subcommand name into choices dictionary before aliases, we should be OK assuming the first time we see a
221
- # parser, the dictionary key is a subcommand and not alias.
222
- processed_parsers = []
223
-
224
- # Set the prog value for each subcommand's parser
225
- for subcmd_name , subcmd_parser in action .choices .items ():
226
- # Check if we've already edited this parser
227
- if subcmd_parser in processed_parsers :
228
- continue
229
-
230
- subcmd_prog = parser .prog
231
- if req_args :
232
- subcmd_prog += " " + " " .join (req_args )
233
- subcmd_prog += " " + subcmd_name
234
- _set_parser_prog (subcmd_parser , subcmd_prog )
235
- processed_parsers .append (subcmd_parser )
236
-
237
- # We can break since argparse only allows 1 group of subcommands per level
238
- break
239
-
240
- # Need to save required args so they can be prepended to the subcommand usage
241
- if action .required :
242
- req_args .append (action .dest )
243
-
244
-
245
195
#: Function signatures for command functions that use an argparse.ArgumentParser to process user input
246
196
#: and optionally return a boolean
247
197
ArgparseCommandFuncOptionalBoolReturn = Callable [[CommandParent , argparse .Namespace ], Optional [bool ]]
0 commit comments