CLI Scripting
The core runtime allows you to build CLI scripting applications
Last updated
Was this helpful?
The core runtime allows you to build CLI scripting applications
Last updated
Was this helpful?
BoxLang is a modern, dynamic scripting language built for more than just simple automation—it empowers you to create full-fledged, high-performance CLI applications with ease. Designed to run seamlessly on the JVM, BoxLang provides powerful scripting capabilities, a rich standard library, and first-class support for modular development.
Whether you're automating repetitive tasks, building interactive command-line tools, or developing complex CLI-driven workflows, BoxLang offers the flexibility, expressiveness, and performance you need. With intuitive syntax, robust error handling, and seamless integration with Java and other JVM-based technologies, BoxLang makes CLI scripting more efficient and enjoyable.
With BoxLang, you can execute a few types of files right from any OS CLI by adding them as the second argument to our boxlang
binary:
*.bx
BoxLang classes with a main()
method
*.bxs
BoxLang scripts
*.bxm
BoxLang Templating Language
*.cfs
CFML scripts (If using the bx-compat-cfml
module)
*.cfm
CFML templates (If using the bx-compat-cfml
module)
*.sh
Shebang scripts using boxlang
as the environment
Please note that you will need the bx-compat-cfml
module if you want to execute CFML scripts
Here are some examples of executing the files. Just pass in the file by relative or absolute path location.
Please note that you have access to other persistent scopes when building CLI applications:
application
- This scope lives as long as your application lives as well, but it is technically attached to an Application.bx
file that activates framework capabilities for your application.
request
- A scope that matches a specific request for your application. We also get one per CLI app since there is no concept of sessions or user state. There is always only one request.
For CLI applications, we recommend you use the server
or request
scope for singleton persistence. Also note that you can use all the caches as well for persistence. You can use application
scope if you have an Application.bx.
BoxLang allows you to execute any *.bx
class as long as it has a method called main()
by convention. All the arguments passed into the file execution will be collected and passed into the function via the args
argument.
The args
argument is an array and it will contain all the arguments passed to the execution of the class.
If you execute this function above, the output will be:
Class executions are a great way to build tasks that have a deterministic approach to execution. We parse the arguments for you, and you can focus on building your task.
In addition to executing classes, you can execute *.bxs
scripts that can do your bidding. The difference is that this is a flat source code script that executes from the top down. It can contain functions, scope usage, imports, and create any class.
Then, if we execute it, we can see this output:
What do you see that's different? We don't have the incoming arguments as an argument since it's a script. However, we can use the CLIGetArgs()
BIF, and it will give you a structure of two keys:
positionals
- An array of positional values passed to the script
options
- Name value pairs detected as options
You can also get the arguments via the server.cli.parsed
variable, which already contains this structure.
Here is the output:
Please note that executing templates is the same as scripts, but your template uses templating language instead, which can be helpful if you produce some markup (HTML, Markdown, etc.)
SheBang scripts are text files containing a sequence of commands for a computer operating system. The term "shebang" refers to the #!
characters at the beginning of the script, which specify the interpreter that should be used to execute the script. These scripts are commonly used in Unix-like operating systems to automate tasks. You can run scripts directly from the command line using a shebang line without explicitly invoking the interpreter. BoxLang supports these scripts, so the OS sees them as just pure shell scripts, but you are coding in BoxLang scripting.
A SheBang script is just basically a *.bxs
script.
As you can see from the sample above, the first line is what makes it a SheBang script the operating system can use. It passes it to the boxlang
binary for interpretation. Also, note that you can pass arguments to these scripts like any other script and the CLIGetArgs()
or the server.cli.parsed
variables will be there for you to use.
BoxLang also gives you several built-in functions for interacting with the CLI:
CLIGetArgs():struct
- Return a structure of the parsed incoming arguments
CLIRead( [prompt] ):any
- Read input from the CLI and return the value
CLIExit( [exitCode=0] )
- Do a System.exit()
with the passed-in exit code
Please note that you have a wealth of built-in functions and components that you can use to build your scripts.
BoxLang will automatically parse the incoming arguments into a structure of two types when using the CLIGetArgs()
BIF or by using the server.cli.parsed
variable.
options
- A structure of the options (name-value pairs) used to invoke the script
positionals
- An array of the positional arguments used to invoke the script
The options can be provided in the following formats, which are standard in CLI applications:
--option
- A boolean option that is set to true
--option=value
- An option with a value
--option="value"
- An option with a quoted value
--option='value'
- An option with a single quoted value
-o=value
- A shorthand option with a value
-o
- A shorthand boolean option that is set to true
--!option
- A negation option that is set to false
--no-{option}
- A negation option
For example, the following CLI arguments:
Will be parsed into the following struct:
Options are prefixed with --
Shorthand options are prefixed with -
Options can be negated with --!
or --no-
Options can have values separated by =
Values can be quoted with single or double quotes
Repeated options will override the previous value
You can easily read input from users by using our handy CLIRead()
bif. You can also pass in a prompt
as part of the method call.
As you navigate all the built-in functions and capabilities of BoxLang, let's learn how to produce output to the system console.
printLn()
- Print with a line break to System out
print()
- Print with no line break to System out
writeOutput(), echo()
- Writes to the output buffer (Each runtime decides what its buffer is. The CLI is the system output, the Web is the HTML response buffer, etc)
writeDump()
- Takes any incoming output and will serialize to a nice string output representation. This will also do complex objects deeply.
I get the output:
Hooray! You have executed your first script using BoxLang. Now let's build a class with a main( args=[] )
convention. This is similar to Java or Groovy.
You can now call it with zero or more arguments!
You can also pipe statements into the BoxLang binary for execution as well. This assumes script, not tags.
or
Thanks to our evangelist Raymond Camden, we have a cool dad joke script you can find in our demos: https://github.com/ortus-boxlang/bx-demos
Now you execute it
Let's modify it now so that we can prompt the user for the term using the CLIRead()
BIF instead of passing it: