001package daikon.tools.runtimechecker;
002
003import java.util.Collections;
004import java.util.List;
005
006/**
007 * Main entrypoint for the instrumenter. Passes control to whichever handler can handle the
008 * user-specified command.
009 */
010public class Main extends CommandHandler {
011
012  protected void usageMessage(List<CommandHandler> handlers) {
013    for (CommandHandler h : handlers) {
014      h.usageMessage();
015    }
016  }
017
018  public void nonStaticMain(String[] args) {
019
020    List<CommandHandler> handlers =
021        Collections.singletonList((CommandHandler) new InstrumentHandler());
022
023    if (args.length < 1) {
024      System.err.println("ERROR:  No command given.");
025      System.err.println(
026          "For more help, invoke the instrumenter with \"help\" as its sole argument.");
027      System.exit(1);
028    }
029    if (args[0].toUpperCase().equals("HELP") || args[0].equals("?")) {
030      usageMessage();
031      usageMessage(handlers);
032      System.exit(0);
033    }
034
035    String command = args[0];
036
037    boolean success = false;
038
039    CommandHandler h = null;
040    try {
041
042      for (CommandHandler handler : handlers) {
043        if (handler.handles(command)) {
044          h = handler;
045          success = h.handle(args);
046          if (!success) {
047            System.err.println("The command you issued returned a failing status flag.");
048          }
049          break;
050        }
051      }
052
053    } catch (Throwable e) {
054      System.out.println("Throwable thrown while handling command:" + e);
055      e.printStackTrace();
056      success = false;
057    } finally {
058      if (!success) {
059        System.err.println("The instrumenter failed.");
060        if (h == null) {
061          System.err.println("Unknown command: " + command);
062          System.err.println(
063              "For more help, invoke the instrumenter with \"help\" as its sole argument.");
064        } else {
065          h.usageMessage();
066        }
067        System.exit(1);
068      } else {
069      }
070    }
071  }
072
073  public static void main(String[] args) {
074    Main main = new Main();
075    main.nonStaticMain(args);
076  }
077}