final int STEP  = 0; final int SHEET = 1; final int SCORE = 2; final int SAVE  = 3; boolean paramNeeded = false; int status = SHEET; String currentCommand = null;   for (int i = 0; i < args.length; i++) { String token = args[i];  if (token.startsWith("-")) {   if (paramNeeded) { printCommandLine(args); stopUsage("Found no parameter after command '" + currentCommand + "'"); }  if (token.equalsIgnoreCase("-help")) { stopUsage(null); } else if (token.equalsIgnoreCase("-batch")) { batchMode = true; paramNeeded = false; } else if (token.equalsIgnoreCase("-write")) { writeScore = true; paramNeeded = false; } else if (token.equalsIgnoreCase("-step")) { status = STEP; paramNeeded = true; } else if (token.equalsIgnoreCase("-sheet")) { status = SHEET; paramNeeded = true; } else if (token.equalsIgnoreCase("-score")) { status = SCORE; paramNeeded = true; } else if (token.equalsIgnoreCase("-save")) { status = SAVE; paramNeeded = true; } else { printCommandLine(args); stopUsage("Unknown command '" + token + "'"); }   currentCommand = token; } else {  switch (status) { case STEP:   targetStep = null;  for (Step step : Sheet.getSteps()) { if (token.equalsIgnoreCase(step.toString())) { targetStep = step;  break; } }  if (targetStep == null) { printCommandLine(args); stopUsage("Step name expected, found '" + token + "' instead"); }   status = SHEET; paramNeeded = false;  break;  case SHEET: addRef(token, sheetNames); paramNeeded = false;  break;  case SCORE: addRef(token, scoreNames); paramNeeded = false;  break;  case SAVE:  if (!(token.endsWith("\\") || token.endsWith("/"))) { token = token + "/"; } constants.savePath.setValue(token);   status = SHEET; paramNeeded = false;  break; } }
INSTANCE = new Main(args); try { INSTANCE.process(args); } catch (Main.StopRequired ex) { logger.info ("Exiting.");
    public static void main (String[] args)    {        Package thisPackage = Main.class.getPackage();        Main.toolName    = thisPackage.getSpecificationTitle();        Main.toolVersion = thisPackage.getSpecificationVersion();        Main.toolBuild   = thisPackage.getImplementationVersion();        // Check installation home        getHomeFolder();        // Problem, from Emacs all args are passed in one string        // sequence.  We recognize this by detecting a single        // argument starting with '-'        if ((args.length == 1) && (args[0].startsWith("-"))) {            // Redispatch the real args            StringTokenizer st = new StringTokenizer(args[0]);            int argNb = 0;            // First just count the number of real arguments            while (st.hasMoreTokens()) {                argNb++;                st.nextToken();            }            String[] newArgs = new String[argNb];            // Second copy all real arguments into newly            // allocated array            argNb = 0;            st = new StringTokenizer(args[0]);            while (st.hasMoreTokens()) {                newArgs[argNb++] = st.nextToken();            }            // Fake the args            args = newArgs;        }        // Status of the finite state machine        final int STEP  = 0;        final int SHEET = 1;        final int SCORE = 2;        final int SAVE  = 3;        boolean paramNeeded = false; // Are we expecting a param?        int status = SHEET; // By default        String currentCommand = null;        // Parse all arguments from command line        for (int i = 0; i < args.length; i++) {            String token = args[i];            if (token.startsWith("-")) {                // This is a command                // Check that we were not expecting param(s)                if (paramNeeded) {                    printCommandLine(args);                    stopUsage("Found no parameter after command '"                              + currentCommand + "'");                }                if (token.equalsIgnoreCase("-help")) {                    stopUsage(null);                } else if (token.equalsIgnoreCase("-batch")) {                    batchMode = true;                    paramNeeded = false;                } else if (token.equalsIgnoreCase("-write")) {                    writeScore = true;                    paramNeeded = false;                } else if (token.equalsIgnoreCase("-step")) {                    status = STEP;                    paramNeeded = true;                } else if (token.equalsIgnoreCase("-sheet")) {                    status = SHEET;                    paramNeeded = true;                } else if (token.equalsIgnoreCase("-score")) {                    status = SCORE;                    paramNeeded = true;                } else if (token.equalsIgnoreCase("-save")) {                    status = SAVE;                    paramNeeded = true;                } else {                    printCommandLine(args);                    stopUsage("Unknown command '" + token + "'");                }                // Remember the current command                currentCommand = token;            } else {                // This is a parameter                switch (status) {                    case STEP:                        // Read a step name                        targetStep = null;                        for (Step step : Sheet.getSteps()) {                            if (token.equalsIgnoreCase(step.toString())) {                                targetStep = step;                                break;                            }                        }                        if (targetStep == null) {                            printCommandLine(args);                            stopUsage("Step name expected, found '" + token                                      + "' instead");                        }                        // By default, sheets are now expected                        status = SHEET;                        paramNeeded = false;                        break;                    case SHEET:                        addRef(token, sheetNames);                        paramNeeded = false;                        break;                    case SCORE:                        addRef(token, scoreNames);                        paramNeeded = false;                        break;                    case SAVE:                        // Make sure that it ends with proper separator                        if (!(token.endsWith("\\")                              || token.endsWith("/"))) {                            token = token + "/";                        }                        constants.savePath.setValue(token);                        // By default, sheets are now expected                        status = SHEET;                        paramNeeded = false;                        break;                }            }        }        // Additional error checking        if (paramNeeded) {            printCommandLine(args);            stopUsage("Expecting a parameter after command '"                      + currentCommand + "'");        }        // Results        if (logger.isFineEnabled()) {            logger.fine("batchMode=" + batchMode);            logger.fine("writeScore=" + writeScore);            logger.fine("savePath="   + constants.savePath.getValue());            logger.fine("targetStep=" + targetStep);            logger.fine("sheetNames=" + sheetNames);            logger.fine("scoreNames=" + scoreNames);        }        // Launch the processing        new Main();    }