Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit ace16b4af175b2dd7742dd6456a1aef2aa5aa058 @ptgoetz committed
0 README.md
No changes.
34 pom.xml
@@ -0,0 +1,34 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.github.ptgoetz</groupId>
+ <artifactId>stemshell</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ <name>StemShell CLI Framework</name>
+ <description>Framework for creating Java-based command line shell applications.</description>
+ <dependencies>
+ <dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.jline</groupId>
+ <artifactId>jline</artifactId>
+ <version>2.5</version>
+ </dependency>
+
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
102 src/main/java/com/instanceone/stemshell/AbstractShell.java
@@ -0,0 +1,102 @@
+// Copyright (c) 2012 Health Market Science, Inc.
+
+package com.instanceone.stemshell;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jline.console.ConsoleReader;
+import jline.console.completer.AggregateCompleter;
+import jline.console.completer.ArgumentCompleter;
+import jline.console.completer.Completer;
+import jline.console.completer.StringsCompleter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.fusesource.jansi.AnsiConsole;
+
+import com.instanceone.stemshell.command.Command;
+
+public abstract class AbstractShell {
+ private static CommandLineParser parser = new PosixParser();
+ private Environment env = new Environment();
+
+ public final void run(String[] arguments) throws Exception {
+ initialize(this.env);
+
+ // create completers
+ ArrayList<Completer> completers = new ArrayList<Completer>();
+ for (String cmdName : env.commandList()) {
+ // command name
+ StringsCompleter sc = new StringsCompleter(cmdName);
+
+
+ ArrayList<Completer> cmdCompleters = new ArrayList<Completer>();
+ // add a completer for the command name
+ cmdCompleters.add(sc);
+ // add the completer for the command
+ cmdCompleters.add(env.getCommand(cmdName).getCompleter());
+ // add a terminator for the command
+ //cmdCompleters.add(new NullCompleter());
+
+ ArgumentCompleter ac = new ArgumentCompleter(cmdCompleters);
+ completers.add(ac);
+ }
+
+ AggregateCompleter aggComp = new AggregateCompleter(completers);
+
+ ConsoleReader reader = new ConsoleReader();
+ reader.addCompleter(aggComp);
+
+ AnsiConsole.systemInstall();
+ String line;
+
+ while ((line = reader.readLine(this.env.getPrompt() +" ")) != null) {
+ String[] argv = line.split("\\s");
+ String cmdName = argv[0];
+
+ Command command = env.getCommand(cmdName);
+ if (command != null) {
+ System.out.println("Running: " + command.getName() + " ("
+ + command.getClass().getName() + ")");
+ String[] cmdArgs = Arrays.copyOfRange(argv, 1, argv.length);
+ CommandLine cl = parse(command, cmdArgs);
+ if (cl != null) {
+ try {
+ command.execute(env, cl, reader);
+ }
+ catch (Throwable e) {
+ System.out.println("Command failed with error: " + e.getMessage());
+ if(cl.hasOption("v")){
+ e.printStackTrace();
+ }
+ }
+ }
+
+ }
+ else {
+ if(cmdName != null && cmdName.length() > 0){
+ System.out.println(cmdName + ": command not found");
+ }
+ }
+ }
+ }
+
+ private static CommandLine parse(Command cmd, String[] args) {
+ Options opts = cmd.getOptions();
+ CommandLine retval = null;
+ try {
+ retval = parser.parse(opts, args);
+ }
+ catch (ParseException e) {
+ System.err.println(e.getMessage());
+ }
+ return retval;
+ }
+
+ public abstract void initialize(Environment env) throws Exception;
+
+}
67 src/main/java/com/instanceone/stemshell/Environment.java
@@ -0,0 +1,67 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell;
+
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.Set;
+
+import com.instanceone.stemshell.command.Command;
+
+public class Environment {
+
+ private String prompt = "eggshell$ ";
+ private Properties props = new Properties();
+ private HashMap<String, Object> values = new HashMap<String, Object>();
+
+ public Environment(){
+ }
+
+ private HashMap<String, Command> commands = new HashMap<String, Command>();
+
+
+ public void addCommand(Command cmd){
+ this.commands.put(cmd.getName(), cmd);
+ }
+
+ public Command getCommand(String name){
+ return this.commands.get(name);
+ }
+
+ public Set<String> commandList(){
+ return this.commands.keySet();
+ }
+
+ public void setProperty(String key, String value){
+ if(value == null){
+ this.props.remove(key);
+ } else{
+ this.props.setProperty(key, value);
+ }
+ }
+
+ public String getProperty(String key){
+ return this.props.getProperty(key);
+ }
+
+ public Properties getProperties(){
+ return this.props;
+ }
+
+ public void setValue(String key, Object value){
+ this.values.put(key, value);
+ }
+
+ public Object getValue(String key){
+ return this.values.get(key);
+ }
+
+
+ public String getPrompt(){
+ return this.prompt;
+ }
+
+ public void setPrompt(String prompt){
+ this.prompt = prompt;
+ }
+}
11 src/main/java/com/instanceone/stemshell/Shell.java
@@ -0,0 +1,11 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell;
+
+
+public abstract class Shell extends AbstractShell {
+
+ @Override
+ public abstract void initialize(Environment env) throws Exception;
+
+}
56 src/main/java/com/instanceone/stemshell/command/AbstractCommand.java
@@ -0,0 +1,56 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.command;
+
+import jline.console.completer.Completer;
+import jline.console.completer.NullCompleter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+public abstract class AbstractCommand implements Command{
+ private String name;
+ protected Completer completer = new NullCompleter();
+
+
+ public AbstractCommand(String name){
+ this.name = name;
+ }
+
+ public String getHelpHeader() {
+ return "Options:";
+ }
+
+ public String gethelpFooter() {
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+
+ public Options getOptions() {
+ Options opts = new Options();
+ opts.addOption("v", "verbose", false, "show verbose output");
+ return opts;
+ }
+
+ public String getUsage(){
+ return getName() + " [OPTION ...] [ARGS ...]";
+ }
+
+ protected static void logv(CommandLine cmd, String log){
+ if(cmd.hasOption("v")){
+ System.out.println(log);
+ }
+ }
+
+ protected static void log(CommandLine cmd, String log){
+ System.out.println(log);
+ }
+
+ public Completer getCompleter() {
+ return this.completer;
+ }
+}
26 src/main/java/com/instanceone/stemshell/command/Command.java
@@ -0,0 +1,26 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.command;
+
+import jline.console.ConsoleReader;
+import jline.console.completer.Completer;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import com.instanceone.stemshell.Environment;
+
+public interface Command {
+
+ String getHelpHeader();
+ String gethelpFooter();
+ String getUsage();
+
+ String getName();
+
+ void execute(Environment env, CommandLine cmd, ConsoleReader reader);
+
+ Options getOptions();
+
+ Completer getCompleter();
+}
46 src/main/java/com/instanceone/stemshell/commands/Env.java
@@ -0,0 +1,46 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.commands;
+
+import java.util.Properties;
+
+import jline.console.ConsoleReader;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.command.AbstractCommand;
+
+public class Env extends AbstractCommand {
+
+ public Env(String name) {
+ super(name);
+ }
+
+ public void execute(Environment env, CommandLine cmd, ConsoleReader reader) {
+ if (cmd.hasOption("l") || !cmd.hasOption("s")) {
+ Properties props = env.getProperties();
+ log(cmd, "Local Properties:");
+ for (Object key : props.keySet()) {
+ log(cmd, "\t" + key + "=" + props.get(key));
+ }
+ }
+ if (cmd.hasOption("s")) {
+ log(cmd, "System Properties:");
+ Properties props = System.getProperties();
+ for (Object key : props.keySet()) {
+ log(cmd, "\t" + key + "=" + props.get(key));
+ }
+ }
+ }
+
+ @Override
+ public Options getOptions() {
+ Options opts = super.getOptions();
+ opts.addOption("s", "system", false, "list system properties.");
+ opts.addOption("l", "local", false, "list local properties.");
+ return opts;
+ }
+
+}
34 src/main/java/com/instanceone/stemshell/commands/Exit.java
@@ -0,0 +1,34 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.commands;
+
+import jline.console.ConsoleReader;
+
+import org.apache.commons.cli.CommandLine;
+
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.command.AbstractCommand;
+
+public class Exit extends AbstractCommand {
+
+ public Exit(String name) {
+ super(name);
+ }
+
+ public void execute(Environment env, CommandLine cmd, ConsoleReader reader) {
+ System.exit(0);
+ }
+
+ @Override
+ public String getHelpHeader() {
+ return "exit the command shell";
+ }
+
+ @Override
+ public String getUsage() {
+ return this.getName();
+ }
+
+
+
+}
51 src/main/java/com/instanceone/stemshell/commands/Help.java
@@ -0,0 +1,51 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.commands;
+
+import jline.console.ConsoleReader;
+import jline.console.completer.AggregateCompleter;
+import jline.console.completer.Completer;
+import jline.console.completer.NullCompleter;
+import jline.console.completer.StringsCompleter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.command.AbstractCommand;
+import com.instanceone.stemshell.command.Command;
+
+public class Help extends AbstractCommand {
+ private Environment env;
+
+ public Help(String name, Environment env) {
+ super(name);
+ this.env = env;
+
+ StringsCompleter strCompleter = new StringsCompleter(this.env.commandList());
+ NullCompleter nullCompleter = new NullCompleter();
+ Completer completer = new AggregateCompleter(strCompleter, nullCompleter);
+
+ this.completer = completer;
+
+ }
+
+
+ public void execute(Environment env, CommandLine cmd, ConsoleReader reader) {
+ if (cmd.getArgs().length == 0) {
+ for (String str : env.commandList()) {
+ log(cmd, str);
+ }
+ } else {
+ Command command = env.getCommand(cmd.getArgs()[0]);
+ logv(cmd, "Get Help for command: " + command.getName() + "(" + command.getClass().getName() + ")");
+ printHelp(command);
+ }
+
+ }
+
+ private void printHelp(Command cmd){
+ HelpFormatter hf = new HelpFormatter();
+ hf.printHelp(cmd.getUsage(), cmd.getHelpHeader(), cmd.getOptions(), cmd.gethelpFooter());
+ }
+}
25 src/main/java/com/instanceone/stemshell/example/ExampleShell.java
@@ -0,0 +1,25 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.example;
+
+import com.instanceone.stemshell.AbstractShell;
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.commands.Env;
+import com.instanceone.stemshell.commands.Exit;
+import com.instanceone.stemshell.commands.Help;
+
+public class ExampleShell extends AbstractShell {
+
+ public static void main(String[] args) throws Exception{
+ new ExampleShell().run(args);
+ }
+
+ @Override
+ public void initialize(Environment env) throws Exception {
+ env.addCommand(new Exit("exit"));
+ env.addCommand(new Env("env"));
+ env.addCommand(new Help("help", env));
+ env.setPrompt("stemshell%");
+ }
+
+}
39 src/main/java/com/instanceone/stemshell/format/ANSIStyle.java
@@ -0,0 +1,39 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.format;
+
+public class ANSIStyle {
+ public static final int OFF = 0;
+ public static final int BOLD = 1;
+ public static final int UNDERSCORE = 4;
+ public static final int BLINK = 5;
+ public static final int REVERSE = 7;
+ public static final int CONCEALED = 8;
+ public static final int FG_BLACK = 30;
+ public static final int FG_RED = 31;
+ public static final int FG_GREEN = 32;
+ public static final int FG_YELLOW = 33;
+ public static final int FG_BLUE = 34;
+ public static final int FG_MAGENTA = 35;
+ public static final int FG_CYAN = 36;
+ public static final int FG_WHITE = 37;
+ static final char ESC = 27;
+
+ private static final String START = "\u001B[";
+ private static final String END = "m";
+
+
+ public static String style(String input, int... styles){
+ StringBuffer buf = new StringBuffer();
+ for(int style : styles){
+ buf.append(START);
+ buf.append(style);
+ buf.append(END);
+ }
+ buf.append(input);
+ buf.append(START);
+ buf.append(OFF);
+ buf.append(END);
+ return buf.toString();
+ }
+}

0 comments on commit ace16b4

Please sign in to comment.
Something went wrong with that request. Please try again.