Register Commands inside a Package
The Command Framework provides a convenient way to automatically discover and register command classes by scanning an entire package at runtime.
This feature removes the need to manually instantiate and register each command class one by one.
The #registerAllInPackage method.
Section titled “The #registerAllInPackage method.”CommandFramework commandFramework = ...;commandFramework.registerAllInPackage("com.example.project.commands");This method scans all top-level classes inside the given package and its sub-packages, creates instances of them, and registers them as commands.
How It Works
Section titled “How It Works”When registerAllInPackage is called, the framework:
- Scans all classes under the specified package
- Skips:
- Interfaces
- Abstract classes
- Attempts to create an instance using a public no-args constructor
- Registers the instance if successful
Classes without a public no-args constructor cannot be instantiated and therefore cannot be registered.
Scan Warnings (REPORT_SCAN_WARNINGS)
Section titled “Scan Warnings (REPORT_SCAN_WARNINGS)”During scanning, the framework can optionally report potential misconfigurations.
This behavior is controlled by the following option:
FrameworkOption.REPORT_SCAN_WARNINGSWhat Triggers a Warning?
Section titled “What Triggers a Warning?”A warning is logged only if all conditions below are met:
- The class contains at least one method annotated with:
@Command@Completer
- The class does not have a public no-args constructor
REPORT_SCAN_WARNINGSoption is enabled
Example warning:
Class 'ExampleCommand' contains command annotations but is missing a public no-args constructor.If multiple warnings are issued, the framework will also log a reminder explaining how to disable them.
Disabling Scan Warnings
Section titled “Disabling Scan Warnings”If this behavior is intentional (for example, you prefer manual instantiation), you may disable scan warnings globally:
CommandFramework commandFramework = ...;commandFramework .options() .disableOption(FrameworkOption.REPORT_SCAN_WARNINGS);Once disabled, the framework will silently skip non-instantiable classes.
When Should You Use Package Scanning?
Section titled “When Should You Use Package Scanning?”Package scanning is ideal when:
- You want minimal boilerplate.
- Your command classes are lightweight.
- You prefer convention over manual registration.
Manual registration may be preferable if:
- Commands require constructor dependencies.
- You want full control over instantiation.
- Commands are created dynamically.