Enhancements in ColdFusion (2018 release)
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.
Getting started with REPL
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.
- REPL works even if the ColdFusion server is NOT up and running.
- REPL only supports cfscript syntax, not tags.
Support for Admin APIs
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.
Support for multi-line statements in REPL
You can type in multiple lines in the REPL mode. A multi-line can be introduced in two ways:
Auto-detection of logical blocks
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.
Adding ^ (space followed by a caret)
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.
Enhancements in ColdFusion (2016 release)
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:
- 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.
The cfm files can either be in wwwroot or in a different location.
Execution
You can pass parameters from command line to the cfm script that is being executed.
Path
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.
Arguments
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:
- cli.getArgs() - Gets all the arguments.
- cli.getNamedArgs() - Gets all the named arguments.
- cli.getUnnamedArgs() - Returns all the unnamed args as a CFML array or an empty array if none are specified.
- cli.getArg(int index) - Gets the argument at the index location.
- cli.getNamedArg(String argName) - Gets the value of the named argument with name argName.
Example
cf.bat test.cfm 10 20 foo=bar
cli.getArg(1) returns 10
cli.getArg(2) returns 20
cli.getNamedArg("foo") returns bar
Custom directories
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
Application.cfc
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.
Scopes
CLI supports the following scopes:
- application
- argument
- request
- this
Reading/writing in Command Line
Additionally, CLI supports three methods to read and write to stdin and stdout/stderr:
- cli .read() - Reads one line from stdin.
- cli.writeln(message) - Writes the message string to stdout.
- cli.writeError(errorMessage) - Writes the error message string to stderr.
- 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.
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.
Example
readwrite.cfm <cfset CLI.writeError("This is an error message from CLI writeError!")> <cfset CLI.writeln("This is CLI write method!")>
Usage
cf.bat readwrite.cfm >> c:\logFile.txt 2>> c:\errFile.txt
Other supported features
CLI supports the following features:
- Web service
- Application data sources
Other functions
- cli.exit(exitCode) - The exit function takes a status code and exits to the command prompt with the specified exit code.
Unsupported features
The following features are not supported with CLI:
- Charting
- Scheduled Tasks
- PDF features
- Document
- REST
- Solr
- Flex Integration
- DotNet Integration
- WebSocket
- Image Functions
- API Manager