|
Project Information
Featured
Downloads
Links
|
Clamshell-CliClamshell-Cli is a framework for building console-based command-line applications in Java. Clamshell uses a simple plugin architecture (based on the ServiceLoader API) that let developers deploy components to build components with control over all aspects of console-based applications including splashscreen display, IO, prompt, input control, and command delegation. Clamshell-Cli comes with a default runtime that implements all of the basic components needed for a fully functioning console-based app. To customize the runtime with your own command, you simply create and deploy your own plugin jars (see example below). Features
Getting StartedIt's easy to get started with Clamshell-Cli to build your first console application.
-rw-r--r--@ 1 299 Mar 17 cli.config -rw-r--r--@ 1 3748 Mar 18 cli.jar drwxrwxrwx 5 170 Mar 18 clilib drwxrwxrwx 4 136 Mar 18 lib drwxrwxrwx 5 170 Mar 18 plugins
From within {CLI_HOME}, start the Clamshell-Cli launcher by typing: > java -jar cli.jar .d8888b. 888 .d8888b. 888 888 888
d88P Y88b 888 d88P Y88b 888 888 888
888 888 888 Y88b. 888 888 888
888 888 8888b. 88888b.d88b. :Y888b. 88888b. .d88b. 888 888
888 888 :88b 888 :888 :88b :Y88b. 888 :88b d8P Y8b 888 888
888 888 888 .d888888 888 888 888 :888 888 888 88888888 888 888
Y88b d88P 888 888 888 888 888 888 Y88b d88P 888 888 Y8b. 888 888
:Y8888P: 888 :Y888888 888 888 888 :Y8888P: 888 888 :Y8888 888 888
Command-Line Interpreter
Java version: 1.6.0_22
Java Home: /usr/lib/jvm/java-6-openjdk/jre
OS: Linux, Version: 2.6.38-10-generic
prompt> _Rightaway, you see three plugins in action:
If you type 'help' at the prompt, for instance, you will be actually invoking the HelpCmd plugin which lists help info for all Command objects that are mapped. Each command is backed by a plugin class installed in the plugins directory. prompt> help
Available Commands
------------------
exit Exits ClamShell.
help Displays help information for available commands.
sysinfo Displays current JVM runtime information.
time Prints current date/time
prompt> _Adding A CommandAs stated above, Clamshell-Cli uses a plugin architecture. All extension points of the framework can be customized by providing your own plugins. This section shows how to add a new Command plugin and deploy it. It examines the implementation of the time command using the TimeCmd plugin (deployed with the runtime). Here are the simple steps for creating your own Command:
The Codepublic class TimeCmd implements Command {
private static final String NAMESPACE = "syscmd";
private static final String ACTION_NAME = "time";
@Override
public Object execute(Context ctx) {
IOConsole console = ctx.getIoConsole();
console.writeOutput(String.format("%n%s%n%n",new Date().toString()));
return null;
}
@Override
public void plug(Context plug) {
// no load-time setup needed
}
@Override
public Command.Descriptor getDescriptor(){
return new Command.Descriptor() {
@Override public String getNamespace() {return NAMESPACE;}
@Override
public String getName() {
return ACTION_NAME;
}
@Override
public String getDescription() {
return "Prints current date/time";
}
@Override
public String getUsage() {
return "Type 'time'";
}
@Override
public Map<String, String> getArguments() {
return Collections.emptyMap();
}
};
}
}A quick explanation of the code is in order:
Package Your PluginOnce your class compiles properly, package the project as a standard Java Service provider (SPI). To do this, do the followings:
Find more information on Java's Service Loader API: Update File cli.configBefore your command can be made available in the console, you must let Clamshell-cli know about it. cli.config is JSON-formatted configuration file where you declare and configure your input controller components. In order for your controller to respond to your command, you must configure the controller properly to respond to the input pattern corresponding to your command. The sample code below shows the configuration of the Command Controller (default controller that comes with Clamshell-Cli): "controllers":{
"org.clamshellcli.impl.CmdController":{
"enabled":"true",
"inputPattern":"\\s*(exit|help|sysinfo|time)\\b.*",
"expectedInputs":[]
}
...
}Note the inputPattern parameter in the snippet above. It is specified as a regular expression for flexibility. In the configuration above, The CmdController class will respond to any input pattern that matches the specified regular expression. So, if your shell tool will only have one input controller, you can change the expression to respond to any input at the command line by changing inputPattern to "*". Deploy and Test the Command
Developing with the APIWhen creating your own console-based tool, you can integrate the Clamshell-Cli API by directly pointing to the jar files or use Maven. Direct SetupFrom within your Java IDE, add the followings to your classpath
Maven Artifacts
<repositories> <repository> <id>clamshellcli.repo</id> <name>Clamshell-Cli Repository</name> <url>http://s3.amazonaws.com/repo.clamshellcli.org/release/</url> </repository> </repositories> <dependency> <groupId>org.clamshellcli</groupId> <artifactId>clamshell-api</artifactId> <version>0.5.1</version> </dependency> <dependency> <groupId>org.clamshellcli</groupId> <artifactId>clamshell-impl-core</artifactId> <version>0.5.1</version> </dependency> Clamshell-Cli ExamplesThe best way to learn how to use the Clamshell-Cli API is to download the source code and look at how the Command plugins are implemented. You can also check out:
|