Coder Social home page Coder Social logo

shell-framework's Introduction

Shell Framework

What is this?

A framework to create your own shell in Java. You can add your custom shell commands into the shell application easily.

Features

  • Definition flexibility: Define your own commands easily by extending the AbstractCommand class.
  • Simple configuration: single line to add a new command to the your shell application.
  • Clear, simple structure: Designed to add new commands to the shell without having to worry about the phases of execution
  • Environment variables: Set/get environment variables to/from the application's context.
  • Batch file execution: Throw a file with the commands which your shell app supports and the app shall take care of it.
  • Maintain history: Get the history of commands executed previously.
  • Logging: Flexible logging functionality. Define your own logger by extending the AbstractLogger class and add it to the app.

Installation/Integration

1. Download/clone the repo into an appropriate folder
2. Add custom commands in the src directory.
3. Add command/s to the application.
4. Run the application's interpreter.
5. Reward yourself with a refreshing beverage. 

Sample command integration

Adding a custom command is as easy as pie. It goes like this:

  • If you want a command called 'test', you create the Test class.

  • It has to extend the class AbstractCommand.

  • It has to have a constructor with 2 arguments:

      String baseCommand
      int minParamsNeeded
    
  • The constructor has to invoke the superclass' constructor:

      super(baseCommand, minParamsNeeded)
    
  • It overrides 4 methods:

      public Result execute()
      public String getCommandUsageSyntax()
      public String getCommandDescription()
      public String getCommandUsageExamples()
    
  • Lets add a custom command called Test (which is just to demonstrate the functionality of the shell app). So, the class Test looks like this:

      public class Test extends AbstractCommand {
    
      	public Test(String baseCommand, int minParamsNeeded) {
      		super(baseCommand, minParamsNeeded);
      	}
    
      	@Override
      	public Result execute() throws Exception {
      		Result r = new Result(this);
      		try {
      			int params = super.getCommandParser().getParamCount();
      			if (params > 0) {
      				super.getApplicationContext().showHelp();
      			} else {
      				r.setResult("This is just a simple test command.");
      			}
      		} catch (Exception e) {
      			r.setError(new Exception("Error while running test command."));
      		}
      		return r;
      	}
    
      	@Override
      	public String getCommandUsageSyntax() {
      		return "test";
      	}
    
      	@Override
      	public String getCommandDescription() {
      		return "Runs the test command." + "\n"
      				+ "This is a mere dummy command.";
      	}
    
      	@Override
      	public String getCommandUsageExamples() {
      		return "test";
      	}
    
      }
    
  • Now that you have a command class Test, lets add this to the shell application.

      public class AppMain {
      	public static void main(String[] args) throws Exception {
      		Application app = new Application();
      		app.addCommand(new Test("test", 0)); // Add the command to app. 'test' is the base command and number of params it takes is 0.
      		app.setStartupInfo("My shell v0.01");
      		app.setPrompt("shell>"); // set your prompt text
      		app.startInterpreter();
      	}
      }
    
  • Running a batch file:

    Write a batch file with the commands supported by your app and pass the file to the application context.

      public class AppMain {
      	public static void main(String[] args) throws Exception {
      		Application app = new Application();
      		app.addCommand(new Test("test", 0)); // Add the command to app. 'test' is the base command and number of params it takes is 0.
      		app.setHaltOnError(false); // if you want the app to halt on error, set it to true (default)
      		app.executeBatch(new File("/path/to/test.batch"));
      	}
      }
    
  • Test the sample(minimal) shell implementation:

    Download shell-app.jar from the 'dist' directory and run it in your commandline.

      java -jar shell-app.jar
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.