1 package console; 2 3 import java.io.BufferedReader ; 4 import java.io.InputStreamReader ; 5 import java.util.Collection ; 6 import java.util.Arrays ; 7 import java.lang.reflect.Method ; 8 9 10 public class Main { 11 private static Collection QUIT_COMMANDS = Arrays.asList(new String [] {"quit", "q", "exit"}); 12 private static Collection HELP_COMMANDS = Arrays.asList(new String [] {"help", "h", "?"}); 13 14 public static void main(String [] a) throws Exception { 15 BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 16 while (true) { 17 System.out.print("> "); 18 String command = in.readLine().trim(); 19 if (QUIT_COMMANDS.contains(command)) { 20 return; 21 } 22 if (HELP_COMMANDS.contains(command)) { 23 help(); 24 continue; 25 } 26 String [] split = command.split(" "); 27 if (split.length == 0) { 28 error(command); 29 continue; 30 } 31 32 try { 33 String [] args = new String [split.length - 1]; 34 System.arraycopy(split, 1, args, 0, args.length); 35 Class cl = Class.forName(split[0]+".Main"); 36 Method m = cl.getMethod("main", new Class [] {String [].class}); 37 m.invoke(null, new Object [] {args}); 38 } catch (Exception ex) { 39 error(command); 40 continue; 41 } 42 } 43 } 44 45 private static void help() { 46 System.out.println("available commands:"); 47 System.out.println("\tquit: quit the console"); 48 System.out.println("\thelp: displays this message"); 49 System.out.println("\tlist -dir <dir>: list files in given directory"); 50 System.out.println("\tfind -dir <dir> -name <name>: list files with given name in given dir"); 51 System.out.println("\tsizewhere -dir <dir> -name <name>: compute total size of files with given name in given dir"); 52 System.out.println("\thelp: displays this message"); 53 } 54 55 private static void error(String command) { 56 System.out.println("unknown command "+command); 57 System.out.println("type ? for help"); 58 } 59 60 } 61 | Popular Tags |