30
30
# setting is True
31
31
import argparse
32
32
import cmd
33
+ import copy
33
34
import functools
34
35
import glob
35
36
import inspect
140
141
from .utils import (
141
142
Settable ,
142
143
get_defining_class ,
144
+ is_callable ,
143
145
strip_doc_annotations ,
144
146
suggest_similar ,
145
147
)
@@ -510,6 +512,16 @@ def __init__(
510
512
# This does not affect self.formatted_completions.
511
513
self .matches_sorted = False
512
514
515
+ # Command parsers for this Cmd instance.
516
+ self ._command_parsers : Dict [str , argparse .ArgumentParser ] = {}
517
+
518
+ # Locates the command parser template or factory and creates an instance-specific parser
519
+ for command in self .get_all_commands ():
520
+ self ._register_command_parser (command , self .cmd_func (command )) # type: ignore[arg-type]
521
+
522
+ # Add functions decorated to be subcommands
523
+ self ._register_subcommands (self )
524
+
513
525
############################################################################################################
514
526
# The following code block loads CommandSets, verifies command names, and registers subcommands.
515
527
# This block should appear after all attributes have been created since the registration code
@@ -529,9 +541,6 @@ def __init__(
529
541
if not valid :
530
542
raise ValueError (f"Invalid command name '{ cur_cmd } ': { errmsg } " )
531
543
532
- # Add functions decorated to be subcommands
533
- self ._register_subcommands (self )
534
-
535
544
self .suggest_similar_command = suggest_similar_command
536
545
self .default_suggestion_message = "Did you mean {}?"
537
546
@@ -659,6 +668,43 @@ def register_command_set(self, cmdset: CommandSet) -> None:
659
668
cmdset .on_unregistered ()
660
669
raise
661
670
671
+ def _build_parser (self , parent : Union ['Cmd' , CommandSet ], parser_builder : Any ) -> Optional [argparse .ArgumentParser ]:
672
+ parser : Optional [argparse .ArgumentParser ] = None
673
+ if is_callable (parser_builder ):
674
+ if isinstance (parser_builder , staticmethod ):
675
+ parser = cast (argparse .ArgumentParser , parser_builder .__func__ ())
676
+ elif isinstance (parser_builder , classmethod ):
677
+ parser = cast (argparse .ArgumentParser , parser_builder .__func__ (parent if not None else self ))
678
+ else :
679
+ parser = cast (argparse .ArgumentParser , parser_builder ()) # type: ignore[misc]
680
+ elif isinstance (parser_builder , argparse .ArgumentParser ):
681
+ if sys .version_info >= (3 , 6 , 4 ):
682
+ parser = copy .deepcopy (parser_builder )
683
+ else : # pragma: no cover
684
+ parser = parser_builder
685
+ return parser
686
+
687
+ def _register_command_parser (self , command : str , command_method : Callable [..., Any ]) -> None :
688
+ if command not in self ._command_parsers :
689
+ parser_builder = getattr (command_method , constants .CMD_ATTR_ARGPARSER , None )
690
+ parent = self .find_commandset_for_command (command ) or self
691
+ parser = self ._build_parser (parent , parser_builder )
692
+ if parser is None :
693
+ return
694
+
695
+ # argparser defaults the program name to sys.argv[0], but we want it to be the name of our command
696
+ from .decorators import (
697
+ _set_parser_prog ,
698
+ )
699
+
700
+ _set_parser_prog (parser , command )
701
+
702
+ # If the description has not been set, then use the method docstring if one exists
703
+ if parser .description is None and hasattr (command_method , '__wrapped__' ) and command_method .__wrapped__ .__doc__ :
704
+ parser .description = strip_doc_annotations (command_method .__wrapped__ .__doc__ )
705
+
706
+ self ._command_parsers [command ] = parser
707
+
662
708
def _install_command_function (self , command : str , command_wrapper : Callable [..., Any ], context : str = '' ) -> None :
663
709
cmd_func_name = COMMAND_FUNC_PREFIX + command
664
710
@@ -681,6 +727,8 @@ def _install_command_function(self, command: str, command_wrapper: Callable[...,
681
727
self .pwarning (f"Deleting macro '{ command } ' because it shares its name with a new command" )
682
728
del self .macros [command ]
683
729
730
+ self ._register_command_parser (command , command_wrapper )
731
+
684
732
setattr (self , cmd_func_name , command_wrapper )
685
733
686
734
def _install_completer_function (self , cmd_name : str , cmd_completer : CompleterFunc ) -> None :
@@ -727,6 +775,8 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:
727
775
del self ._cmd_to_command_sets [cmd_name ]
728
776
729
777
delattr (self , COMMAND_FUNC_PREFIX + cmd_name )
778
+ if cmd_name in self ._command_parsers :
779
+ del self ._command_parsers [cmd_name ]
730
780
731
781
if hasattr (self , COMPLETER_FUNC_PREFIX + cmd_name ):
732
782
delattr (self , COMPLETER_FUNC_PREFIX + cmd_name )
@@ -746,14 +796,7 @@ def _check_uninstallable(self, cmdset: CommandSet) -> None:
746
796
747
797
for method in methods :
748
798
command_name = method [0 ][len (COMMAND_FUNC_PREFIX ) :]
749
-
750
- # Search for the base command function and verify it has an argparser defined
751
- if command_name in self .disabled_commands :
752
- command_func = self .disabled_commands [command_name ].command_function
753
- else :
754
- command_func = self .cmd_func (command_name )
755
-
756
- command_parser = cast (argparse .ArgumentParser , getattr (command_func , constants .CMD_ATTR_ARGPARSER , None ))
799
+ command_parser = self ._command_parsers .get (command_name , None )
757
800
758
801
def check_parser_uninstallable (parser : argparse .ArgumentParser ) -> None :
759
802
for action in parser ._actions :
@@ -792,7 +835,7 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
792
835
for method_name , method in methods :
793
836
subcommand_name : str = getattr (method , constants .SUBCMD_ATTR_NAME )
794
837
full_command_name : str = getattr (method , constants .SUBCMD_ATTR_COMMAND )
795
- subcmd_parser = getattr (method , constants .CMD_ATTR_ARGPARSER )
838
+ subcmd_parser_builder = getattr (method , constants .CMD_ATTR_ARGPARSER )
796
839
797
840
subcommand_valid , errmsg = self .statement_parser .is_valid_command (subcommand_name , is_subcommand = True )
798
841
if not subcommand_valid :
@@ -812,7 +855,7 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
812
855
raise CommandSetRegistrationError (
813
856
f"Could not find command '{ command_name } ' needed by subcommand: { str (method )} "
814
857
)
815
- command_parser = getattr ( command_func , constants . CMD_ATTR_ARGPARSER , None )
858
+ command_parser = self . _command_parsers . get ( command_name , None )
816
859
if command_parser is None :
817
860
raise CommandSetRegistrationError (
818
861
f"Could not find argparser for command '{ command_name } ' needed by subcommand: { str (method )} "
@@ -832,16 +875,17 @@ def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) ->
832
875
833
876
target_parser = find_subcommand (command_parser , subcommand_names )
834
877
878
+ subcmd_parser = cast (argparse .ArgumentParser , self ._build_parser (cmdset , subcmd_parser_builder ))
879
+ from .decorators import (
880
+ _set_parser_prog ,
881
+ )
882
+
883
+ _set_parser_prog (subcmd_parser , f'{ command_name } { subcommand_name } ' )
884
+ if subcmd_parser .description is None and method .__doc__ :
885
+ subcmd_parser .description = strip_doc_annotations (method .__doc__ )
886
+
835
887
for action in target_parser ._actions :
836
888
if isinstance (action , argparse ._SubParsersAction ):
837
- # Temporary workaround for avoiding subcommand help text repeatedly getting added to
838
- # action._choices_actions. Until we have instance-specific parser objects, we will remove
839
- # any existing subcommand which has the same name before replacing it. This problem is
840
- # exercised when more than one cmd2.Cmd-based object is created and the same subcommands
841
- # get added each time. Argparse overwrites the previous subcommand but keeps growing the help
842
- # text which is shown by running something like 'alias -h'.
843
- action .remove_parser (subcommand_name ) # type: ignore[arg-type,attr-defined]
844
-
845
889
# Get the kwargs for add_parser()
846
890
add_parser_kwargs = getattr (method , constants .SUBCMD_ATTR_ADD_PARSER_KWARGS , {})
847
891
@@ -913,7 +957,7 @@ def _unregister_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
913
957
raise CommandSetRegistrationError (
914
958
f"Could not find command '{ command_name } ' needed by subcommand: { str (method )} "
915
959
)
916
- command_parser = getattr ( command_func , constants . CMD_ATTR_ARGPARSER , None )
960
+ command_parser = self . _command_parsers . get ( command_name , None )
917
961
if command_parser is None : # pragma: no cover
918
962
# This really shouldn't be possible since _register_subcommands would prevent this from happening
919
963
# but keeping in case it does for some strange reason
@@ -2034,7 +2078,7 @@ def _perform_completion(
2034
2078
else :
2035
2079
# There's no completer function, next see if the command uses argparse
2036
2080
func = self .cmd_func (command )
2037
- argparser : Optional [ argparse . ArgumentParser ] = getattr ( func , constants . CMD_ATTR_ARGPARSER , None )
2081
+ argparser = self . _command_parsers . get ( command , None )
2038
2082
2039
2083
if func is not None and argparser is not None :
2040
2084
# Get arguments for complete()
@@ -3259,14 +3303,19 @@ def _cmdloop(self) -> None:
3259
3303
#############################################################
3260
3304
3261
3305
# Top-level parser for alias
3262
- alias_description = "Manage aliases\n " "\n " "An alias is a command that enables replacement of a word by another string."
3263
- alias_epilog = "See also:\n " " macro"
3264
- alias_parser = argparse_custom .DEFAULT_ARGUMENT_PARSER (description = alias_description , epilog = alias_epilog )
3265
- alias_subparsers = alias_parser .add_subparsers (dest = 'subcommand' , metavar = 'SUBCOMMAND' )
3266
- alias_subparsers .required = True
3306
+ @staticmethod
3307
+ def _build_alias_parser () -> argparse .ArgumentParser :
3308
+ alias_description = (
3309
+ "Manage aliases\n " "\n " "An alias is a command that enables replacement of a word by another string."
3310
+ )
3311
+ alias_epilog = "See also:\n " " macro"
3312
+ alias_parser = argparse_custom .DEFAULT_ARGUMENT_PARSER (description = alias_description , epilog = alias_epilog )
3313
+ alias_subparsers = alias_parser .add_subparsers (dest = 'subcommand' , metavar = 'SUBCOMMAND' )
3314
+ alias_subparsers .required = True
3315
+ return alias_parser
3267
3316
3268
3317
# Preserve quotes since we are passing strings to other commands
3269
- @with_argparser (alias_parser , preserve_quotes = True )
3318
+ @with_argparser (_build_alias_parser , preserve_quotes = True )
3270
3319
def do_alias (self , args : argparse .Namespace ) -> None :
3271
3320
"""Manage aliases"""
3272
3321
# Call handler for whatever subcommand was selected
@@ -3681,7 +3730,7 @@ def complete_help_subcommands(
3681
3730
3682
3731
# Check if this command uses argparse
3683
3732
func = self .cmd_func (command )
3684
- argparser = getattr ( func , constants . CMD_ATTR_ARGPARSER , None )
3733
+ argparser = self . _command_parsers . get ( command , None )
3685
3734
if func is None or argparser is None :
3686
3735
return []
3687
3736
@@ -3717,7 +3766,7 @@ def do_help(self, args: argparse.Namespace) -> None:
3717
3766
# Getting help for a specific command
3718
3767
func = self .cmd_func (args .command )
3719
3768
help_func = getattr (self , constants .HELP_FUNC_PREFIX + args .command , None )
3720
- argparser = getattr ( func , constants . CMD_ATTR_ARGPARSER , None )
3769
+ argparser = self . _command_parsers . get ( args . command , None )
3721
3770
3722
3771
# If the command function uses argparse, then use argparse's help
3723
3772
if func is not None and argparser is not None :
@@ -3853,7 +3902,7 @@ def _build_command_info(self) -> Tuple[Dict[str, List[str]], List[str], List[str
3853
3902
help_topics .remove (command )
3854
3903
3855
3904
# Non-argparse commands can have help_functions for their documentation
3856
- if not hasattr ( func , constants . CMD_ATTR_ARGPARSER ) :
3905
+ if command not in self . _command_parsers :
3857
3906
has_help_func = True
3858
3907
3859
3908
if hasattr (func , constants .CMD_ATTR_HELP_CATEGORY ):
@@ -3899,7 +3948,7 @@ def _print_topics(self, header: str, cmds: List[str], verbose: bool) -> None:
3899
3948
doc : Optional [str ]
3900
3949
3901
3950
# Non-argparse commands can have help_functions for their documentation
3902
- if not hasattr ( cmd_func , constants . CMD_ATTR_ARGPARSER ) and command in topics :
3951
+ if command not in self . _command_parsers and command in topics :
3903
3952
help_func = getattr (self , constants .HELP_FUNC_PREFIX + command )
3904
3953
result = io .StringIO ()
3905
3954
0 commit comments