---
title: Task Runners
summary: Write a task file, give it targets and arguments, and run it with task run.
icon: phosphor-duotone:list-checks
tags: [course]
---

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

```bash
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:

```bash
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.

```cfml title="build.cfc"
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:

```bash
box task run build createZips
box task run path/to/build compileAssets
```

!!! note "Not global"
    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.

```cfml title="fun.cfc"
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:

=== "Positional"
    ```bash
    box task run fun greet Brad false
    ```

=== "Named"
    ```bash
    box task run taskFile=fun target=greet :name=Brad :verbose=false
    ```

=== "Boolean flag"
    ```bash
    box task run taskFile=fun target=greet :name=Brad --no:verbose
    ```

## Recap

- `task create` scaffolds a task file, `task run` executes its `run()` target by default.
- Each public method is a target. Run another with `task run <file> <target>`.
- Pass arguments positionally or as `:name=value`.
