Introduction

Adobe ColdFusion (2016 release) comes with a Command Line Interface (CLI) for developers to run cfm scripts without a running ColdFusion server.

CLI can be used to write CRON jobs with:

File operations for reporting, logging, archiving, and so on.

Database operations for monitoring, debugging, and so on.

Network operations like mailing an error log or thread dump to a system admin.

Note: The CLI utility is located in the cf_root/bin (server configuration) or cf_webapp_root/WEB-INF/cfusion/bin (J2EE configuration) directory. Before you can use the CLI utility in the J2EE configuration, set the CFUSION_HOME, J2EEJAR, and WEBINF variables in the cf.bat/cf.sh file.

Each time you call the CLI, a light-weight ColdFusion Server starts with minimal and default configuration settings. The server shuts down after CLI ends. You can make additonal changes to the CLI settings through Application.cfc and/or init parameters.

You can call CLI using cf.bat/cf.sh present in <cf_home>/bin folder. It is recommended that you add this path to the environment variables.

The cfm files can either be in wwwroot or in a different folder.

CLI help for usage : cf –help or cf --help

Syntax:

cf <path_to_cfm> <arguments>

Path to CFM

The path to the CFM can be either absolute or relative. An absolute path sets the directory of the cfm as wwwroot. A relative path sets the current working directory as wwwroot. This defines the level until which ColdFusion looks up for Application.cfc

Examples:

cf c:\cli\cliWriteOutput.cfm or
cf cliWriteOutput.cfm

Arguments

From the command line, you can pass parameters to the cfm script. CLI supports both positional and named arguments to the executing cfm.

To read the arguments, CLI has the following methods:

cli.getArgs() - Gets all arguments.

cli.getNamedArgs() - Gets all named arguments.

cli.getUnnamedArgs() - Gets all unnamed args as a CFML array or an empty array if none are specified.

cli.getArg(int index) - Gets the argument at index location.

cli.getNamedArg(String argName) - Gets the value of the named argument with name argName.

CLI takes custom output and log directories from command line. An output directory is used for ColdFusion compiled classes. Thelog directoryis used for logs. By default, both classes and logs go to temp folder.

cli.exit(exitCode) - The exit function takes an optional status code and exits to the console with the specified exit code. By default, CLI returns an exitcode of 0 on successful execution and 1 when execution fails.

Note: Use CLI.Writeln() or WriteOutput() or WriteDump() methods for writing to console. Any content, HTML or otherwise, outside of these functions will not be dumped to console. In other words, CLI works as if enablecfoutputonly is set to true.