diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-12-02 17:12:19 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-12-02 17:12:19 -0500 |
| commit | 92c8eae71fd662c2b7e76b17309c1b2f91c38d41 (patch) | |
| tree | b896163a97319e10bbe8e18302f06138ab3bd91e /src/main/java/derms/client/CLI.java | |
| parent | dfac8009ea2eaae89b64d40caa513c54f3c03181 (diff) | |
| download | soen423-92c8eae71fd662c2b7e76b17309c1b2f91c38d41.zip | |
client cli
Diffstat (limited to 'src/main/java/derms/client/CLI.java')
| -rw-r--r-- | src/main/java/derms/client/CLI.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/main/java/derms/client/CLI.java b/src/main/java/derms/client/CLI.java new file mode 100644 index 0000000..05f4e35 --- /dev/null +++ b/src/main/java/derms/client/CLI.java @@ -0,0 +1,86 @@ +package derms.client; + +import java.util.*; + +abstract class CLI implements Runnable { + protected Map<String, Command> commands = new HashMap<String, Command>(); + protected List<Description> cmdDescriptions = new ArrayList<Description>(); + protected List<Description> argDescriptions = new ArrayList<Description>(); + + protected CLI() { + commands.put("quit", new Quit()); + cmdDescriptions.add(new Description("quit", "Exit the program")); + + commands.put("help", new Help()); + cmdDescriptions.add(new Description("help", "List commands")); + } + + @Override + public void run() { + Scanner scanner = new Scanner(System.in); + System.out.println("Type 'help' for a list of commands."); + for (;;) { + System.out.print("Command: "); + String input = scanner.nextLine(); + String[] fields = input.split(" "); + if (fields.length < 1 || fields[0] == "") { + continue; + } + Command cmd = commands.get(fields[0]); + if (cmd == null) { + System.out.println("Invalid command '"+fields[0]+"'"); + System.out.println("Type 'help' for a list of commands."); + continue; + } + String[] args = null; + if (fields.length < 2) { + args = new String[0]; + } else { + args = Arrays.copyOfRange(fields, 1, fields.length); + } + cmd.exec(args); + } + } + + protected interface Command { + public void exec(String[] args); + } + + protected class Quit implements Command { + @Override + public void exec(String[] args) { + System.out.println("Shutting down..."); + System.exit(1); + } + } + + protected class Help implements Command { + @Override + public void exec(String[] args) { + System.out.println("\nCommands:"); + for (Description d : cmdDescriptions) { + System.out.println(d); + } + System.out.println("\nArguments:"); + for (Description d : argDescriptions) { + System.out.println(d); + } + System.out.println(); + } + } + + protected class Description { + String object; /// The thing being described + String description; + + protected Description(String object, String description) { + this.object = object; + this.description = description; + } + + @Override + public String toString() { + return object+"\n\t"+description; + } + } +} |