The 2016 release of ColdFusion introduced support for Command Line Interface (CLI).
In the 2018 release of ColdFusion, there is support for Read-Eval-Print-Loop (REPL). REPL, a shell, is an interactive programming environment that takes single user inputs, evaluates them, and returns the result to the user.
In the command-prompt mode, navigate to <CF_HOME>/cfusion/bin. Enter cf.bat and the REPL mode displays.
To test if the REPL works as expected, assign a value to a variable, and display the variable.
To exit the REPL mode, enter q.
You can execute Admin APIs through CLI as well as in REPL mode. For example,
<cfscript> //login createObject("component","cfide.adminapi.administrator").login("admin"); myObj = createObject("component","cfide.adminapi.debugging"); returnValue = myObj.getIPList(); myObj.setIP("10.10.10.10"); myObj.setIP("22.22.22.22"); returnValue = myObj.getIPList(); if( listcontains(returnValue,"10.10.10.10",",") GT 0 AND listcontains(returnValue,"22.22.22.22",",") GT 0) { writeOutput("IP's added" & " "); } myObj.deleteIP("10.10.10.10"); myObj.deleteIP("22.22.22.22"); returnValue = myObj.getIPList(); if( listcontains(returnValue,"10.10.10.10",",") EQ 0 AND listcontains(returnValue,"22.22.22.22",",") EQ 0) { writeOutput("IP's deleted"); } </cfscript>
A CLI instance and the server share the same configuration files. Any admin settings changed from the CLI are reflected in CLI immediately. The changes are also applied to the server. However, to reflect the changes in the ColdFusion Administrator Pages, restart the server. Any changes to settings done from ColdFusion administrator applies to CLI as well. Restart CLI, to reflect the changes.
REPL is more enhanced
To open the console in the REPL mode, run cf.bat/cf.sh without any arguments. If you open the console in REPL mode, it does the job of CLI as well now. You can specify the path of a cfm in REPL mode and execute it. Like CLI, this also handles both positional and named arguments to the cfm .
For example,
cf-cli>C:\ColdFusion\cfusion\wwwroot\run.cfm arg1 arg2
Executing cfms in REPL mode is much faster than running from CLI mode, there is no requirement for re-initialization of services. There is null support and semicolon is optional at the end of the line. When a single line is typed, it is evaluated and printed. To see the help, type ‘help’ in CLI or REPL mode.
You can type in multiple lines in the REPL mode. A multi-line can be introduced in two ways:
If you type function add() { in the REPL mode and press Enter, it will automatically go to the next line and wait for the next line of input in the console.
Add ^ at the end of line , and if you hit Enter, it will go to the next line, and wait for the next line of input. You can use this when you have a line that does not have any logical opening block ( { / ( )
When the input line has any logical block opening, you need not type in ^. Use it when you do not have any logical block to type in.
For example, to type in ‘function add()’ and ‘{‘ in the next line, you have to type as the line as function add() ^.
When you want to exit from the multiline midway, enter multilineexit .
For example,
cf-cli>x=1
1
cf-cli>if (x==1) ^
...writeOutput("Hello ColdFusion")
Hello ColdFusion
For more information, type help in the command-prompt for all REPL usage options.
In Adobe ColdFusion (2016 release), there is a new Command Line Interface (CLI) for developers to run cfm scripts without starting the ColdFusion server.
CLI can be used to write CRON jobs with:
The cfm files can either be in wwwroot or in a different location.
You can pass parameters from command line to the cfm script that is being executed.
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.
You can pass both positional and named arguments to the executing cfm from the command line.
CLI has the following methods to read the arguments:
Example
cf.bat test.cfm 10 20 foo=bar
cli.getArg(1) returns 10
cli.getArg(2) returns 20
cli.getNamedArg("foo") returns bar
In CLI, you can set outputdir and logdir while executing the cfm. By default, both classes and logs go to the temp folder.
Usage:
cf.bat cliscript.cfm –outputdir=c:\cfclasses –logdir=c:\logs
The lookup for Application.cfc depends on the wwwroot, which is set according to the path of cfm (absolute or relative).
In Application.cfc, only onApplicationStart(), onApplicationStop(), and onError() methods are supported. There is no support for session and request methods in CLI.
CLI supports the following scopes:
Additionally, CLI supports three methods to read and write to stdin and stdout/stderr:
Use cli.Writeln() or WriteOutput() or WriteDump() methods to write 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.
readwrite.cfm <cfset CLI.writeError("This is an error message from CLI writeError!")> <cfset CLI.writeln("This is CLI write method!")>
cf.bat readwrite.cfm >> c:\logFile.txt 2>> c:\errFile.txt
CLI supports the following features:
The following features are not supported with CLI:
Sign in to your account