Groovy is a great language to create scripts. Most of the time if we invoke the scripts we pass arguments to the script. These arguments are available as a String array in our script. For example to get the first argument we can use the following code s = args. To have real argument handling in our script we use Groovy's CliBuilderclass. This class uses Jakarta Commons CLI under the hood. With CliBuilder we can define the argument options and parse the arguments. CliBuilder adds Groovyness by allowing us to invoke non-existing one-letter methods, which are turned into argument options with the one-letter shortcut. Furthermore we can use named parameters to define usage text, parser implementation, formatter implementation and option properties.
Let's see the CliBuilder in action. We create a script to show a date formatted according to defined arguments. If we don't define a date the current date is assumed. And furthermore we want to be able to define a prefix text, which is added before the formatted date (is empty by default).
* Download Commons CLI