Created
November 20, 2015 11:25
-
-
Save serhii-shnurenko/9c3be271c8dc62e356ca to your computer and use it in GitHub Desktop.
Short and simple command interpreter using Java programming language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline.commands; | |
/** | |
* Created by Сергій Шнуренко on 11.11.2015. | |
*/ | |
public class Actioner implements Command { | |
@Override | |
public void execute(Object[] args) { | |
//TODO: Implement code to get info based on args | |
//Too much code will be here so I just do sth | |
System.out.println("You running command action. It's implementation awesome"); | |
System.out.println("Your parameter list is:"); | |
if(args.length==0){ | |
System.out.println("[EMPTY]"); | |
}else | |
for(int i=0; i<args.length;i++){ | |
System.out.format("[%d]\t"+args[i]+"\n",i); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline.commands; | |
/** | |
* Created by Сергій Шнуренко on 11.11.2015. | |
*/ | |
public interface Command { | |
void execute(Object[] args); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline.commands; | |
/** | |
* Created by Сергій Шнуренко on 11.11.2015. | |
*/ | |
public abstract class CommandFactory { | |
public static Command getCommand(String commandName){ | |
if(commandName.equalsIgnoreCase("action")){ | |
return new Actioner(); | |
}else if(commandName.equalsIgnoreCase("exit")){ | |
return new Exiter(); | |
}else if(commandName.equalsIgnoreCase("help")){ | |
return new Helper(); | |
} | |
else{ | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline.commands; | |
/** | |
* Created by Сергій Шнуренко on 11.11.2015. | |
*/ | |
public class Exiter implements Command { | |
@Override | |
public void execute(Object[] args) { | |
if(args.length!=0){ | |
System.out.println("No reason to give me parameters, anyway I'll ignore them"); | |
} | |
System.exit(0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline.commands; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by Сергій Шнуренко on 12.11.2015. | |
*/ | |
public class Helper implements Command { | |
@Override | |
public void execute(Object[] args) { | |
if(args.length==0){ | |
printAll(); | |
} else{ | |
printCommandHelp((String)args[0]); | |
} | |
} | |
private void printAll(){ | |
System.out.println( | |
"action - do some action\n" + | |
"exit - exiting program(take no params)"); | |
//TODO: Print all methods help | |
} | |
private void printCommandHelp(String commandName){ | |
//Todo: implement me | |
System.out.println("Here should be detailed command info about "+commandName+" if exist"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commandline; | |
import commandline.commands.*; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
while(true){ | |
//getting user input | |
System.out.print("Enter your command:"); | |
String userInput = scanner.nextLine(); | |
String[] splitedInput = userInput.split("[ ]+"); | |
//preparing data for creating and executing new command | |
String commandName = splitedInput[0]; | |
Object[] commandArgs = new String[splitedInput.length-1]; | |
System.arraycopy(splitedInput,1,commandArgs,0,commandArgs.length); | |
//executing command | |
Command command =CommandFactory.getCommand(commandName); | |
if(command!=null) | |
command.execute(commandArgs); | |
else | |
System.out.println("Bad command. Try again or use help."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment