Cliche: Speeding up Command Line Interface Development

0
6348

Untitled1

Cliche is a small Java library that makes it simple to create interactive command line user interfaces.

Recently, I was evaluating CLI frameworks in Java and went through some of the commonly tried ones like Apache Commons and args4j.
I found that most of these frameworks were good at parsing the input passed in the following statement:

public static void main(String[] args)

But, one still has to write a lot of code to recognise whether a particular option had been passed, and whether its value was present or not.
For example, in Apache Commons, one has to create an org.apache.commons.cli.Options object, add different options that this particular command expects, and then pass this to org.apache.commons.cli.DefaultParser. After this, one gets an org.apache.commons.cli.CommandLine object, and then you can start to figure out whether a particular user input had that option or not.

Options options = new Options();
options.addOption(new Option(“u”, “username”, true, “The username for this account.”));
....
CommandLineParser parser = new DefaultParser();
...
CommandLine cmd = parser.parse(options, args);
...
if(cmd.hasOption(“u”)) {
//Work on the username.
}

Every option of each of the commands should result in the above output!

Some optimisation is possible here, but doing this for each command is a bit tiresome, and doesn’t help when you want to do a quick UT of your Java API.
I was looking for a framework that would reduce my development time substantially, and give me a way to call my API quickly.
A further search on Google brought me to https://code.google.com/p/cliche/ and I was smitten! By Cliché’s simplicity, ease-of-use, speed of development and the sheer fun of experimenting with it!
It creates a shell for you, from where you can fire commands. It doesn’t exit the shell until you fire the built-in ‘Exit’ command.
Cliche also offers some inbuilt commands to list existing commands, and a pretty decent help menu. Moreover, it adds your commands to this help menu automatically. It even creates an abbreviation for your commands.
Adding commands is ridiculously easy—as you will see below!
Here’s how you go about using Cliche:

  • Download the JAR from the above link.
  • If you use Maven as your build tool, you can add this as your dependency in the pom.xml. A little more of searching on Google can help you with bringing Cliche into your other build frameworks.
<groupId>com.googlecode.clichemaven</groupId>
<artifactId>cliche</cliche>
<version>110413</cliche>
  • Create a console shell, give it your own name and the text you want on your shell prompt, and a class that lists all your commands.
try {
ShellFactory.createConsoleShell(“MyShellPrompt”, “MyShell”, new MYCLIClass()).commandLoop();
} catch(IOException e) {
e.printStackTrace();
}
@Command(description=”Fetch the version of blah-blah installed on this machine”)
public int getversion(String args) {
return Integer.parseInt(MyAPI.getVersion());
}
  • You can even add descriptions to your parameters as follows:
@Command(description=”my command description”)
public int someCommand(
@Param(name=”param1”, description=”description of param1”)
int param1,
@Param(name=”param2”, description=”description of param2”)
int param2) {
…....
}

Basically, with just two steps (adding dependency/putting the JAR in your classpath) and by creating the shell and a function for your command, you’re on your way!

Sample outputs
The green output below is all from the inbuilt features of the Cliche jar.

MyShell
MyShellPrompt> ?la

2

h hello ()

hw hello-world (p1)

MyShellPrompt> ?h hw
Command: hello-world
Abbrev: hw
Params: (p1)
Description: prints hello world and an integer ahead
Number of parameters: 1
p1 int prime number

MyShellPrompt> hw 13
Hello World: 13
MyShellPrompt>

Here is the sample code:

package cliché.trial;
import java.io.IOException;
import asg.cliche.Command;
import asg.cliche.Param;
import asg.cliche.Shell;
import asg.cliche.ShellFactory;

public class Trials {

@Command(description=”prints hello.”)
public int hello() {
System.out.println(“Hello”);
return 1;
}

@Command(description=”prints hello world and an integer ahead”)
public void helloWorld(@Param(name=”p1”, description=”prime number”) int p1) {
System.out.println(“Hello World: “ + p1);
}
public static void main(String[] args) throws IOException
{
try {
Shell myShell = ShellFactory.createConsoleShell(“MyShellPrompt”, “MyShell”, new Trials());
myShell.commandLoop();
} catch (IOException e) {
throw e;
}
}
}

Copyright information: The Cliche library is distributed under the MIT licence and written by Anton Grigoryev (ansgri@gmail.com).

LEAVE A REPLY

Please enter your comment!
Please enter your name here