Writing Your Own Commands
Build a custom command in a module, document it with help, and see which modules already extend CommandBox.
On this page
A task lives in one project. A command is global, shipped inside a module. In this lesson you will create a command and give it help text.
A module with a command
A module is a folder with a ModuleConfig.cfc. Put it in ~/.CommandBox/cfml/modules/.
component {
function configure() {}
}
Every CFC in the module's commands/ folder becomes a command. The only requirement is a run() method.
component {
function run() {
return 'Hello World!';
}
}
Reload the shell, then run it:
box reload
box hello
Namespaces
Nest the CFC in a folder and the folder name becomes the namespace. commands/say/Hello.cfc is called as:
box say hello
Document it with help
A Javadoc style comment on the component is the command's help text. Comments on the arguments describe each parameter.
/**
* Greets someone by name.
*
* {code:bash}
* hello Luis
* {code}
**/
component {
/**
* @name.hint Who to greet
*/
function run( required string name ) {
return 'Hello #name#!';
}
}
box hello help
Argument names are the flags and parameters users type, and tab completion is built from the same metadata.
Modules that extend CommandBox
Install them from ForgeBox with box install. Popular ones:
coldbox-clicontentbox-clicommandbox-boxlangtestbox-clicommandbox-migrationscommandbox-cfconfigcommandbox-docboxcommandbox-bullet-train
Run box help to list every namespace and command available, including those added by modules.
Recap
- A command is a CFC with a
run()method inside a module'scommands/folder. - Subfolders make namespaces, and a comment on the component is the
helptext. - Use
box reloadafter changes andbox installto add community modules.