KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > console > Main


1 package console;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Arrays JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8
9
10 public class Main {
11     private static Collection JavaDoc QUIT_COMMANDS = Arrays.asList(new String JavaDoc[] {"quit", "q", "exit"});
12     private static Collection JavaDoc HELP_COMMANDS = Arrays.asList(new String JavaDoc[] {"help", "h", "?"});
13
14     public static void main(String JavaDoc[] a) throws Exception JavaDoc {
15       BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
16       while (true) {
17         System.out.print("> ");
18         String JavaDoc 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 JavaDoc[] split = command.split(" ");
27         if (split.length == 0) {
28           error(command);
29           continue;
30         }
31         
32         try {
33           String JavaDoc[] args = new String JavaDoc[split.length - 1];
34           System.arraycopy(split, 1, args, 0, args.length);
35           Class JavaDoc cl = Class.forName(split[0]+".Main");
36           Method JavaDoc m = cl.getMethod("main", new Class JavaDoc[] {String JavaDoc[].class});
37           m.invoke(null, new Object JavaDoc[] {args});
38         } catch (Exception JavaDoc 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 JavaDoc command) {
56       System.out.println("unknown command "+command);
57       System.out.println("type ? for help");
58     }
59             
60 }
61
Popular Tags