Wiki

Clone wiki

Groovy Cmd / Localization

Localization

You can translate or specify default prompt strings in your cli application.

In the table below, you can see Cmd variables, which prints during work and their descriptions with default values:

Variable Description Default value
PROMPT Input prompt 'cmd:> '
WELCOME Welcome text at program start 'Welcome\nPrint "help" for reference'
GOODBYE Goodbye text at program end 'Goodbye!'
HELP Help text, then user types help 'List of all available commands:\n(for reference on command print "help <command_name>")'
NO_COMMAND Message, when user has typed unknown command 'No such command'
EXECUTION_ERROR Message, when exception uccure 'Execution error! Command was\'t unidentified'

You can set your own values for this localization variables in constructor.

Example

Go to a directory, where you place groovy cmd:

$ cd <groovy_cmd_home>

Let's write a simple script:

cli.groovy

class MyCli extends Cmd {

    MyCli() {
        super();
        PROMPT = '$> ';
    }

    @Description(
        brief='prints greetings',
        full='prints greetins for all setted arguments, if arguments list is empty prints default message "Hello world!"'
    )
    boolean do_hello(String[] args) {
        if (args.length == 0) {
            writer.println('Hello world!');
        } else {
            args.each {
                writer.println("Hello ${it}!");
            }
        }
        return true;
    }
}

def cli = new MyCli();
if (this.args.length > 0) {
    cli.execute(this.args);
} else {
    cli.start();
}

Build it using Groovy Merger script:

$ merger -sh Cmd.groovy cli.groovy cli

Now, you can run script like this:

#####NOTE: Before running the cli command, you should to give execution rights by typing command:

$ chmod +x cli
$ ./cli

Output will be the following:

Welcome
Print "help" for reference
$> _

We have changed a default value of PROMPT variable from 'cmd:> ' to '$> ' in MyCli constructor.

Updated