@@ -593,11 +593,14 @@ def __init__(
593593 # being printed by a command.
594594 self .terminal_lock = threading .RLock ()
595595
596- # Commands that have been disabled from use. This is to support commands that are only available
597- # during specific states of the application. This dictionary's keys are the command names and its
598- # values are DisabledCommand objects.
596+ # Commands disabled during specific application states
597+ # Key: Command name | Value: DisabledCommand object
599598 self .disabled_commands : dict [str , DisabledCommand ] = {}
600599
600+ # Categories of commands to be disabled
601+ # Key: Category name | Value: Message to display
602+ self .disabled_categories : dict [str , str ] = {}
603+
601604 # The default key for sorting string results. Its default value performs a case-insensitive alphabetical sort.
602605 # If natural sorting is preferred, then set this to NATURAL_SORT_KEY.
603606 # cmd2 uses this key for sorting:
@@ -788,6 +791,12 @@ def register_command_set(self, cmdset: CommandSet) -> None:
788791 if default_category and not hasattr (command_method , constants .CMD_ATTR_HELP_CATEGORY ):
789792 utils .categorize (command_method , default_category )
790793
794+ # If this command is in a disabled category, then disable it
795+ command_category = getattr (command_method , constants .CMD_ATTR_HELP_CATEGORY , None )
796+ if command_category in self .disabled_categories :
797+ message_to_print = self .disabled_categories [command_category ]
798+ self .disable_command (command , message_to_print )
799+
791800 self ._installed_command_sets .add (cmdset )
792801
793802 self ._register_subcommands (cmdset )
@@ -5819,7 +5828,7 @@ def enable_command(self, command: str) -> None:
58195828
58205829 :param command: the command being enabled
58215830 """
5822- # If the commands is already enabled, then return
5831+ # If the command is already enabled, then return
58235832 if command not in self .disabled_commands :
58245833 return
58255834
@@ -5851,11 +5860,17 @@ def enable_category(self, category: str) -> None:
58515860
58525861 :param category: the category to enable
58535862 """
5863+ # If the category is already enabled, then return
5864+ if category not in self .disabled_categories :
5865+ return
5866+
58545867 for cmd_name in list (self .disabled_commands ):
58555868 func = self .disabled_commands [cmd_name ].command_function
58565869 if getattr (func , constants .CMD_ATTR_HELP_CATEGORY , None ) == category :
58575870 self .enable_command (cmd_name )
58585871
5872+ del self .disabled_categories [category ]
5873+
58595874 def disable_command (self , command : str , message_to_print : str ) -> None :
58605875 """Disable a command and overwrite its functions.
58615876
@@ -5866,7 +5881,7 @@ def disable_command(self, command: str, message_to_print: str) -> None:
58665881 command being disabled.
58675882 ex: message_to_print = f"{cmd2.COMMAND_NAME} is currently disabled"
58685883 """
5869- # If the commands is already disabled, then return
5884+ # If the command is already disabled, then return
58705885 if command in self .disabled_commands :
58715886 return
58725887
@@ -5905,13 +5920,19 @@ def disable_category(self, category: str, message_to_print: str) -> None:
59055920 of the command being disabled.
59065921 ex: message_to_print = f"{cmd2.COMMAND_NAME} is currently disabled"
59075922 """
5923+ # If the category is already disabled, then return
5924+ if category in self .disabled_categories :
5925+ return
5926+
59085927 all_commands = self .get_all_commands ()
59095928
59105929 for cmd_name in all_commands :
59115930 func = self .cmd_func (cmd_name )
59125931 if getattr (func , constants .CMD_ATTR_HELP_CATEGORY , None ) == category :
59135932 self .disable_command (cmd_name , message_to_print )
59145933
5934+ self .disabled_categories [category ] = message_to_print
5935+
59155936 def _report_disabled_command_usage (self , * _args : Any , message_to_print : str , ** _kwargs : Any ) -> None :
59165937 """Report when a disabled command has been run or had help called on it.
59175938
0 commit comments