Example usage for org.apache.commons.cli MissingArgumentException printStackTrace

List of usage examples for org.apache.commons.cli MissingArgumentException printStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.cli MissingArgumentException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.speed.ob.Obfuscator.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption("a", "all", false, "enable all obfuscations");
    options.addOption("s", "strings", false, "enable string obfuscation");
    options.addOption("l", "lexical", false, "enable lexical obfuscation");
    options.addOption("c", "control-flow", false, "enable control flow obfuscation");
    options.addOption("C", "config", true, "use <arg> as a config file");
    options.addOption("f", "file", true, "obfuscate file <arg>");
    options.addOption("o", "out", true, "output obfuscated file(s) to directory <arg>");
    options.addOption("h", "help", false, "shows this help message and then exits");
    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    try {//from w ww  . j a va2  s  .co m
        CommandLine cmd = parser.parse(options, args);
        parse(cmd, parser, options, formatter);
    } catch (MissingArgumentException e) {
        System.out.println(e.getMessage());
        formatter.printHelp("java com.speed.ob.Obfuscate", options, true);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:ape.Main.java

public static void main(String[] args) {
    // Creating the Properties object for log4j
    Properties ppt = new Properties();
    ppt.setProperty("log4j.rootLogger", "INFO, appender1");
    ppt.setProperty("log4j.appender.appender1", "org.apache.log4j.DailyRollingFileAppender");
    ppt.setProperty("log4j.appender.appender1.File", "/var/log/ape.log");
    ppt.setProperty("log4j.appender.appender1.DatePattern", ".yyyy-MM-dd");
    ppt.setProperty("log4j.appender.appender1.layout", "org.apache.log4j.PatternLayout");

    // Configuring log4j to use the Properties object created above
    PropertyConfigurator.configure(ppt);

    // Log the current date and time
    logger.info("\n---------------------------------\nStarting time:");
    logTime();/*from  ww w .  j ava  2  s.  co m*/

    // Initialize all of the Option objects for each command (these are used by the CLI parser)
    createOptions();

    // There should be an array of strings passed in as an argument (even if it's empty)
    // If we get null, we exit
    if (args == null) {
        System.err
                .println("Invalid arguments.  main(String[] args) method expected array of strings, got null.");
        logger.info("Invalid arguments.  main(String[] args) method expected array of strings, got null");
        printHelp();
        return;
    }

    // If an empty array is passed in, print the help dialog and exit
    if (args.length == 0) {
        printHelp();
        return;
    }

    // Use the CLI parser to attempt to parse the command into a series of Option objects
    try {
        System.out.println(Arrays.toString(args));
        logger.info(Arrays.toString(args));
        line = getCommand(args);
        //System.out.println(line.toString());   
    } catch (MissingArgumentException e) {
        System.out.println("Missing an argument.  Check your syntax.");
        logger.info("Missing an argument.");
        logger.info("Dumping args array:");
        for (int i = 0; i < args.length; i++) {
            logger.info(i + ": " + args[i]);
        }
        printHelp();
        return;
    } catch (ParseException e) {
        System.out.println("Parsing error, see help dialog:");
        logger.info("Parsing error, see help dialog.");
        printHelp();
        return;
    }

    // Get the array of options that were parsed from the command line 
    Option[] options = line.getOptions();

    if (line.hasOption("v")) {
        MAX_OPTION_LENGTH = 3;
    } else {
        MAX_OPTION_LENGTH = 2;
    }

    if (options == null || options.length > MAX_OPTION_LENGTH || options.length < 1) {
        System.out.println("Too many options");
        logger.info("Too many options");
        printHelp();
        return;
    }

    if (line.hasOption("v")) {
        VERBOSE = true;
        logger.info("Executing Ape verbosely.");
        System.out.println("Executing Ape verbosely");
    }

    //Find which option is cmd, which is -local/-remote, order might be disturbed
    for (int k = 0; k < options.length; k++) {
        if (VERBOSE) {
            System.out.println(options[k]);
            logger.info(options[k]);
        }
        if (!options[k].getOpt().equals("v")) {
            if (options[k].getOpt() == "L" || options[k].getOpt() == "R") {
                modeN = k;
            } else
                cmdN = k;
        }
    }

    // If the version flag was in the command, print the version and exit
    if (line.hasOption("V")) {
        logger.info("Printing out current version: " + VERSION);
        System.out.println("ChaosMonkey version: " + VERSION);
        return;
    }

    if (line.hasOption('h') || options.length < 1 || modeN == cmdN || modeN == -1 || cmdN == -1) {
        if (cmdN == -1) {
            System.out.println("Failure commands were not specified.");
            logger.info("Failure commands were not specified.");
        }
        System.out.println("Exiting ...");
        logger.info("Exiting ...");
        printHelp();
        return;
    }

    if (VERBOSE) {
        System.out.println("Mode is " + options[modeN].getLongOpt());

        if (options[modeN].getOpt() == "R") {
            System.out.println("List of Hosts:");
            for (int j = 0; j < line.getOptionValues("R").length; j++) {
                System.out.println(line.getOptionValues("R")[j]);
            }
        }

        System.out.println("Command is " + options[cmdN].getLongOpt());

        if (line.getOptionValues(options[cmdN].getOpt()) != null) {
            for (int l = 0; l < line.getOptionValues(options[cmdN].getOpt()).length; l++)
                System.out.println("Command Argument: " + line.getOptionValues(options[cmdN].getOpt())[l]);
        }
    }

    logger.info("Type of Event " + options[cmdN].getLongOpt());

    // Remote command execution
    if (line.hasOption("R")) {
        //go to remote
        String[] passIn = line.getOptionValues("R");
        logger.info("Executing a command remotely");
        logger.info("hosts: ");

        for (int k = 0; k < passIn.length; k++) {
            logger.info(passIn[k]);
        }

        CommunicationInterface r = new PDSHCommunication(options[cmdN].getOpt(),
                line.getOptionValues(options[cmdN].getOpt()), passIn);
        try {
            // If the command executed successfully
            if (r.execute()) {
                logger.info("End time");

                logTime();

                System.out.println("Running Remote Command Succeeded");
            }
            // If the command exited with an error
            else {
                System.out.println("Running remote command failed");
                logger.info("Running remote command failed");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        return;

    }
    // Local command execution
    else if (line.hasOption("L")) {
        logger.info("Running Locally");

        ApeCommand ac = ApeCommand.getCommand(options[cmdN].getLongOpt());
        if (ac == null) {
            System.out.println(options[cmdN].getLongOpt() + " is not a valid command.");
            System.out.println(
                    "This can occur if a new command class is added but an entry is not added in the ApeCommand file.");
            System.out.println(
                    "See src/main/resources/META-INF/services/ape.ApeCommand and ensure that the command's class is there.");
            logger.info(options[cmdN].getLongOpt() + " is not a valid command.");

            return;
        }

        try {
            String[] cmdArgs = line.getOptionValues(options[cmdN].getOpt());
            if (ac.exec(cmdArgs)) {
                System.out.println("Running local command succeeded");
                logger.info("End time");

                logTime();

            } else {
                System.out.println("Running local command failed");
                logger.info("Running local command failed");
            }
            return;
        } catch (ParseException e) {
            if (Main.VERBOSE) {
                System.out.println("VERBOSE: A parse exception was thrown.  ");
                System.out.println(
                        "VERBOSE: Interpreting this as an invalid number of arguments for a particular flag and printing the help dialog.");
                System.out.println("VERBOSE: Stack trace:");
                e.printStackTrace();
                logger.info("VERBOSE: A parse exception was thrown.  ");
                logger.info(
                        "VERBOSE: Interpreting this as an invalid number of arguments for a particular flag and printing the help dialog.");
                logger.info("VERBOSE: Stack trace:");
                logger.info(e);
            }
            System.out.println("Invalid number of arguments.");
            logger.info("Invalid number of arguments");
            printHelp();
        } catch (IOException e) {
            System.out.println("Running local command failed");
            logger.info("Running local command failed");
            e.printStackTrace();
        }
    }
    // If the local or remote flags were not used then print the help dialog
    else {
        printHelp();
    }
}

From source file:org.jpos.q2.Q2.java

private void parseCmdLine(String[] args) {
    CommandLineParser parser = new PosixParser();

    Options options = new Options();
    options.addOption("v", "version", false, "Q2's version");
    options.addOption("d", "deploydir", true, "Deployment directory");
    options.addOption("r", "recursive", false, "Deploy subdirectories recursively");
    options.addOption("h", "help", false, "Usage information");
    options.addOption("C", "config", true, "Configuration bundle");
    options.addOption("e", "encrypt", true, "Encrypt configuration bundle");
    options.addOption("i", "cli", false, "Command Line Interface");
    options.addOption("c", "command", true, "Command to execute");
    options.addOption("O", "osgi", false, "Start experimental OSGi framework server");

    try {//from   w  ww .  j a  va  2 s .  co m
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("v")) {
            displayVersion();
            System.exit(0);
        }
        if (line.hasOption("h")) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("Q2", options);
            System.exit(0);
        }
        if (line.hasOption("c")) {
            cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i"));
        } else if (line.hasOption("i"))
            cli = new CLI(this, null, true);

        String dir = DEFAULT_DEPLOY_DIR;
        if (line.hasOption("d")) {
            dir = line.getOptionValue("d");
        }
        recursive = line.hasOption("r");
        this.deployDir = new File(dir);
        if (line.hasOption("C"))
            deployBundle(new File(line.getOptionValue("C")), false);
        if (line.hasOption("e"))
            deployBundle(new File(line.getOptionValue("e")), true);
        if (line.hasOption("O"))
            startOSGI = true;
    } catch (MissingArgumentException e) {
        System.out.println("ERROR: " + e.getMessage());
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}