Develop a CLI tool using groovy scripts

Apache groovy, one of the best tools to develop very own CLI. We can create script behavior using groovy easily, In additionally If you’re familiar with Java, you can easily adapt to groovy, Since groovy uses java like syntax. Here I’ll use picocli with groovy which can be used to develop CLI input easily.


First, we need to download and install Groovy. After completing the groovy installation, we can go forward with development.

First ensure groovy installation by issuing the following command in command line,

$ groovy -version
Groovy Version: 3.0.0-alpha-3 JVM: 1.8.0_181 Vendor: Oracle Corporation OS: Windows 10

When checking version it will show you the groovy version you’ve installed along with JVM and OS version you’re using.

Fine now you’re ready to write script on groovy. let’s start with most simplest script,

Writing simple script

@picocli.CommandLine.Command(
description = "Simple Groovy Script",
version = '1.0.0', showDefaultValues = true
)
@picocli.groovy.PicocliScript
import groovy.transform.Field
import picocli.CommandLine

@CommandLine.Option(names = ["-h", "--help"], usageHelp = true, description = "Show this help message and exit.")
@Field private boolean helpRequested

@CommandLine.Option(names = ["-v", "--version"], versionHelp = true, description = "Show version info and exit.")
@Field private boolean versionInfoRequested

Lets execute the script…

$ groovy MediumGroovy.groovy -v
1.0.0
$ groovy MediumGroovy.groovy -h
Usage: MediumGroovy [-hv]
Simple Groovy Script
-h, --help Show this help message and exit.
-v, --version Show version info and exit.

Adding custom input capture behavior to the script,

@CommandLine.Option(names = ["-u", "--user"], description = "Username for user authentication.")
@Field private String username

if(username != null){
println('Login in with username '+username)
}

Output

$ groovy MediumGroovy.groovy -u mediumuser
Login in with username mediumuser

Adding Header to the Script

@picocli.CommandLine.Command(
header = [
$/@|bold,green ______ ______ _______ ______ _______ |@/$,
$/@|bold,green | | | | | | | | | |@/$,
$/@|bold,green |_____ | | | | | ___| | |____ |@/$,
$/@|bold,green | | | | | | | |@/$,
$/@|bold,green _____| ______ | | | |_____ |______|@/$
],
description = "Simple Groovy Script",
version = '1.0.0', showDefaultValues = true
)

Execute script with -h or -help

$ groovy MediumGroovy.groovy -h
______ ______ _______ ______ _______
| | | | | | | | |
|_____ | | | | | ___| | |____
| | | | | | |
_____| ______ | | | |_____ |______
Usage: MediumGroovy [-hv]
Simple Groovy Script
-h, --help Show this help message and exit.
-v, --version Show version info and exit.

Now all things are working perfectly, Lets add more features to the script.

Add third party library using apache maven to the script.

@groovy.lang.Grab(group= 'commons-io', module = 'commons-io', version= '2.6')
//Shorthand version
@groovy.lang.Grab('commons-io:commons-io:2.6')

Adding JDBC drivers to the groovy scripts,

When adding JDBC drivers we need to add it into the system classloader, so we can done that by using following annotation.

@GrabConfig(systemClassLoader=true)
@Grab(group='mysql', module='mysql-connector-java', version='5.1.6')

That’s all for today, Lets meet again with another medium post. Hope this will help you to develop CLI based application. Happy coding…..

Final code