Script arguments

A feature common to most programming languages is the ability to pass arguments to the program to be executed. In Java, the entry point into a program is a class's main method, and it takes a String array as an argument. That array holds any arguments passed to the program. Similarly, arguments can be passed to CLI scripts. Arguments passed to a script can be accessed in the implicit args array:

Example 8.6. Handling Script Arguments

if (args.length > 2) {
    throw "Not enough arguments!";
for (i in args) {
     println('args[' + i '] = ' + args[i]);


The args variable is only available when executing a script in non-interactive mode or with exec -f.
In addition to the traditional style of indexed-based arguments, you can also pass named arguments to a script, as demonstrated in the following example:
rhqadmin@localhost:7080$ exec -f echo_args.js --args-style=named x=1, y=2

Example 8.7. echo_args.js

for (i in args) {
     println('args[' + i '] = ' + args[i]);
println('named args...');
println('x = ' + x);
println('y = ' + y);

This produces the following output:

Example 8.8. echo_args.js

args[0] = 1
args[1] = 2
named args...
x = 1
y = 2

Be aware of the following:
  • You have to explicitly specify that you are using named arguments via the --args-style option
  • The values of the named arguments are still accessible via the implicit args array
  • The named arguments, x and y, are bound into the script context as variables