10.3. Passing Command and Script Arguments

When connecting to the CLI using rhq-cli.sh, commands or full scripts can be passed simultaneously. This is a non-interactive way to connect to the CLI, since the CLI runs the specified command or script and then exits, rather than staying connected in interactive mode.

Example 10.9. Passing Variables to the Server

A single command can be passed to the CLI by using the -c. In this example, the server searches for and prints all supported resource types for the server and prints the results to resource_types.txt
rhq-cli.sh -u rhqadmin -p rhqadmin -c "pretty.print(ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()))" > resource_types.txt
The ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()) class invokes the findResourceTypesByCriteria operation on ResourceTypeManager. A new ResourceTypeCriteria object is passed as the argument.
Nothing has been specified on the criteria object so all resource types will be returned.
pretty is an implicit object made available to commands and scripts by the CLI. This is useful for outputting objects in a readable, tabular format which is designed for domain objects.
This single command provides a nicely formatted, text-based report of the resource types in the inventory.

Example 10.10. Running a Script

This executes the script file, my_script.js. The CLI terminates immediately after the script has finished executing.
cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -f /export/myScripts/my_script.js

Example 10.11. Handling 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:
if (args.length < 2) {
      throw "Not enough arguments!";
      }
      
      for (i in args) {
      println('args[' + i + '] = ' + args[i]);
      }
The custom args variable is only available if it is explicitly defined in a CLI script or if it is added as a module in the cliRoot/rhq-remoting-cli-4.9.0.JON320GA/samples/modules directory.
In addition to the traditional style of indexed-based arguments, named arguments can also be passed to a script:
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -s jon-server.example.com -u rhqadmin -p rhqadmin -t 7080 echo_args.js --args-style=named x=1 y=2
The echo_args.js, for example, is written to accept the two named option with the script invocation, x and y.
for (i in args) {
      println('args[' + i + '] = ' + args[i]);
      }
      println('named args...');
      println('x = ' + x);
      println('y = ' + y);
This simple script echoes the given x and y values.
args[0] = 1
      args[1] = 2
      named args...
      
      x = 1
      y = 2
Be aware of the following:
  • Explicitly specify that you are using named arguments with the --args-style option.
  • The values of the named arguments are still accessible through the implicit args array.
  • The named arguments, such as x and y, are bound into the script context as variables.

Example 10.12. Executing a Single Statement

When running the CLI interactively, commands can still be passed and executed, same as using the -c option with rhq-cli.sh.
localhost:7080> var x = 1

Example 10.13. Executing a Multi-Line Statement

The CLI is a Java shell interpreter, and it can handle multi-line statements properly. To indicate a new line in the same statement, use the backslash (\) character.
localhost:7080(rhqadmin)> for (i = 1; i < 3; ++i) { \
      localhost:7080(rhqadmin)>    println(i); \
      localhost:7080(rhqadmin)> }
      1
      2
      
      localhost:7080(rhqadmin)>

Example 10.14. Executing a Script

To run a script from a file, use the -f option with the rhq-cli.sh|bat script.
The -f option must give the absolute location of the script, even if it is in the same directory as the rhq-cli.sh script. The CLI will not find a script with only a relative path.
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 -f /absolute/path/to/myscript.js

Example 10.15. Executing a Script with Arguments

A script can be written to accept or require arguments; this is described more in Section 18.3, “Defining Arguments and Other Parameters for the CLI Scripts”. Indexed arguments can be passed simply by supplying them in the proper order, as specified in the JavaScript file.
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 -f /absolute/path/to/myscript.js 1 2 3

Example 10.16. Executing a Script with Named Arguments

Script arguments can be indexed or named.
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 --args-style=named -f /absolute/path/to/myscript.js x=1 y=2 y=3