Task Runners
Write a task file, give it targets and arguments, and run it with task run.
On this page
Scripts chain commands. When you need real logic, loops or arguments, write a task: a single file with one or more targets you run from the CLI.
Create and run one
box task create --open
box task run
task create writes a task.cfc in the current folder with a run target. task run executes it. You can name the file and target yourself:
box task create build.cfc createZips
The create command also takes --directory to choose where the file goes and --force to overwrite an existing one.
Task anatomy
A task is a component. Each public method is a target. CommandBox gives your task the same helpers commands have, such as print, so you do not extend anything yourself.
component {
function createZips() {
print.line( 'Zips created!' );
}
function compileAssets() {
print.greenLine( 'Assets compiled!' );
}
}
By convention task run looks for task.cfc in the current folder and calls its run() target. Point it elsewhere with the file name and target:
box task run build createZips
box task run path/to/build compileAssets
Tasks are not registered with CommandBox. They are found from the folder you run in, so every project can have its own build.cfc.
Arguments
Target methods can declare arguments, with defaults.
component {
function greet( string name, boolean verbose ) {
print.line( 'Well, hello there #name#!' );
if( verbose ) {
print.line( "You're looking quite well today." );
}
}
}
Pass them by position (task and target name required) or by name with a leading colon:
box task run fun greet Brad false
box task run taskFile=fun target=greet :name=Brad :verbose=false
box task run taskFile=fun target=greet :name=Brad --no:verbose
Recap
task createscaffolds a task file,task runexecutes itsrun()target by default.- Each public method is a target. Run another with
task run <file> <target>. - Pass arguments positionally or as
:name=value.