@@ -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 )
@@ -5805,7 +5814,7 @@ def enable_command(self, command: str) -> None:
58055814
58065815 :param command: the command being enabled
58075816 """
5808- # If the commands is already enabled, then return
5817+ # If the command is already enabled, then return
58095818 if command not in self .disabled_commands :
58105819 return
58115820
@@ -5837,11 +5846,17 @@ def enable_category(self, category: str) -> None:
58375846
58385847 :param category: the category to enable
58395848 """
5849+ # If the category is already enabled, then return
5850+ if category not in self .disabled_categories :
5851+ return
5852+
58405853 for cmd_name in list (self .disabled_commands ):
58415854 func = self .disabled_commands [cmd_name ].command_function
58425855 if getattr (func , constants .CMD_ATTR_HELP_CATEGORY , None ) == category :
58435856 self .enable_command (cmd_name )
58445857
5858+ del self .disabled_categories [category ]
5859+
58455860 def disable_command (self , command : str , message_to_print : str ) -> None :
58465861 """Disable a command and overwrite its functions.
58475862
@@ -5852,7 +5867,7 @@ def disable_command(self, command: str, message_to_print: str) -> None:
58525867 command being disabled.
58535868 ex: message_to_print = f"{cmd2.COMMAND_NAME} is currently disabled"
58545869 """
5855- # If the commands is already disabled, then return
5870+ # If the command is already disabled, then return
58565871 if command in self .disabled_commands :
58575872 return
58585873
@@ -5891,13 +5906,19 @@ def disable_category(self, category: str, message_to_print: str) -> None:
58915906 of the command being disabled.
58925907 ex: message_to_print = f"{cmd2.COMMAND_NAME} is currently disabled"
58935908 """
5909+ # If the category is already disabled, then return
5910+ if category in self .disabled_categories :
5911+ return
5912+
58945913 all_commands = self .get_all_commands ()
58955914
58965915 for cmd_name in all_commands :
58975916 func = self .cmd_func (cmd_name )
58985917 if getattr (func , constants .CMD_ATTR_HELP_CATEGORY , None ) == category :
58995918 self .disable_command (cmd_name , message_to_print )
59005919
5920+ self .disabled_categories [category ] = message_to_print
5921+
59015922 def _report_disabled_command_usage (self , * _args : Any , message_to_print : str , ** _kwargs : Any ) -> None :
59025923 """Report when a disabled command has been run or had help called on it.
59035924
0 commit comments