Search in sources :

Example 86 with GnuParser

use of org.apache.commons.cli.GnuParser in project opennms by OpenNMS.

the class SpectrumTrapImporter method configureArgs.

private void configureArgs(String[] argv) {
    Options opts = new Options();
    opts.addOption("d", "dir", true, "Directory where Spectrum custom events are located");
    opts.addOption("t", "model-type-asset-field", true, "Name of asset field containing equivalent of Spectrum model type.  Defaults to 'manufacturer'.");
    opts.addOption("u", "base-uei", true, "Base value for UEI of generated OpenNMS events.  Defaults to 'uei.opennms.org/import/Spectrum'.");
    opts.addOption("f", "output-file", true, "File to which OpenNMS events will be written.  Defaults to standard output.");
    opts.addOption("k", "key", true, "Middle part of reduction- and clear-key, after UEI and before discriminators.  Defaults to '%dpname%:%nodeid%:%interface%'.");
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(opts, argv);
        if (cmd.hasOption('d')) {
            m_customEventsDir = new FileSystemResource(cmd.getOptionValue('d'));
        }
        if (cmd.hasOption('t')) {
            m_modelTypeAssetField = cmd.getOptionValue('t');
        } else {
            m_modelTypeAssetField = "manufacturer";
        }
        if (cmd.hasOption('u')) {
            m_baseUei = cmd.getOptionValue('u');
        } else {
            m_baseUei = "uei.opennms.org/import/Spectrum";
        }
        if (cmd.hasOption('f')) {
            m_outputWriter = new PrintWriter(new FileSystemResource(cmd.getOptionValue('f')).getFile());
        } else {
            m_outputWriter = new PrintWriter(System.out);
        }
        if (cmd.hasOption('k')) {
            m_reductionKeyBody = cmd.getOptionValue('k');
        } else {
            m_reductionKeyBody = "%dpname%:%nodeid%:%interface%";
        }
    } catch (ParseException pe) {
        printHelp("Failed to parse command line options");
        System.exit(1);
    } catch (FileNotFoundException fnfe) {
        printHelp("Custom events input directory does not seem to exist");
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) FileNotFoundException(java.io.FileNotFoundException) CommandLineParser(org.apache.commons.cli.CommandLineParser) FileSystemResource(org.springframework.core.io.FileSystemResource) ParseException(org.apache.commons.cli.ParseException) PrintWriter(java.io.PrintWriter)

Example 87 with GnuParser

use of org.apache.commons.cli.GnuParser in project shifu by ShifuML.

the class ShifuCLI method main.

public static void main(String[] args) {
    // get -D parameters at first and set it in Environment then clean args
    List<String> cleanedArgsList = new ArrayList<String>();
    for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("-D")) {
            // remove '-D' at first
            String keyValue = args[i].substring(2, args[i].length());
            int index = keyValue.indexOf("=");
            String key = keyValue.substring(0, index);
            String value = "";
            if (keyValue.length() >= index + 1) {
                value = keyValue.substring(index + 1, keyValue.length());
            }
            // set to Environment for others to read
            Environment.setProperty(key.trim(), value.trim());
            // such parameter will also be set in system properties for later reference in correlation and others
            System.setProperty(key.trim(), value.trim());
            cleanedArgsList.add(args[i]);
        }
    }
    String[] cleanedArgs = cleanedArgsList.toArray(new String[0]);
    // invalid input and help options
    if (cleanedArgs.length < 1 || (isHelpOption(cleanedArgs[0]))) {
        printUsage();
        System.exit(cleanedArgs.length < 1 ? -1 : 0);
    }
    // process -v and -version conditions manually
    if (isVersionOption(cleanedArgs[0])) {
        printLogoAndVersion();
        System.exit(0);
    }
    CommandLineParser parser = new GnuParser();
    Options opts = buildModelSetOptions(cleanedArgs);
    CommandLine cmd = null;
    try {
        cmd = parser.parse(opts, cleanedArgs);
    } catch (ParseException e) {
        log.error("Invalid command options. Please check help message.");
        printUsage();
        System.exit(1);
    }
    int status = 0;
    try {
        if (cleanedArgs[0].equals(NEW) && cleanedArgs.length >= 2 && StringUtils.isNotEmpty(cleanedArgs[1])) {
            // modelset step
            String modelName = cleanedArgs[1];
            status = createNewModel(modelName, cmd.getOptionValue(MODELSET_CMD_TYPE), cmd.getOptionValue(MODELSET_CMD_M));
            if (status == 0) {
                printModelSetCreatedSuccessfulLog(modelName);
            } else {
                log.warn("Error in create new model set, please check your shifu config or report issue");
            }
            System.exit(status);
        // copyModel(manager, cmd.getOptionValues(MODELSET_CMD_CP));
        } else {
            if (cleanedArgs[0].equals(MODELSET_CMD_CP) && cleanedArgs.length >= 3 && StringUtils.isNotEmpty(cleanedArgs[1]) && StringUtils.isNotEmpty(cleanedArgs[2])) {
                String newModelSetName = cleanedArgs[2];
                // modelset step
                copyModel(new String[] { cleanedArgs[1], newModelSetName });
                printModelSetCopiedSuccessfulLog(newModelSetName);
            } else if (cleanedArgs[0].equals(INIT_CMD)) {
                // init step
                if (cmd.getOptions() == null || cmd.getOptions().length == 0) {
                    status = initializeModel();
                    if (status == 0) {
                        log.info("ModelSet initilization is successful. Please continue next step by using 'shifu stats'.");
                    } else {
                        log.warn("Error in ModelSet initilization, please check your shifu config or report issue");
                    }
                } else if (cmd.hasOption(INIT_CMD_MODEL)) {
                    initializeModelParam();
                } else {
                    log.error("Invalid command, please check help message.");
                    printUsage();
                }
            } else if (cleanedArgs[0].equals(STATS_CMD)) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put(StatsModelProcessor.IS_COMPUTE_CORR, cmd.hasOption(CORRELATION) || cmd.hasOption("c"));
                params.put(StatsModelProcessor.IS_REBIN, cmd.hasOption(REBIN));
                params.put(StatsModelProcessor.REQUEST_VARS, cmd.getOptionValue(VARS));
                params.put(StatsModelProcessor.EXPECTED_BIN_NUM, cmd.getOptionValue(N));
                params.put(StatsModelProcessor.IV_KEEP_RATIO, cmd.getOptionValue(IVR));
                params.put(StatsModelProcessor.MINIMUM_BIN_INST_CNT, cmd.getOptionValue(BIC));
                // stats step
                status = calModelStats(params);
                if (status == 0) {
                    if (cmd.hasOption(CORRELATION) || cmd.hasOption("c")) {
                        log.info("Do model set correlation computing successfully. Please continue next step by using 'shifu normalize or shifu norm'. For tree ensemble model, no need do norm, please continue next step by using 'shifu varsel'");
                    } else {
                        log.info("Do model set statistic successfully. Please continue next step by using 'shifu normalize or shifu norm'. For tree ensemble model, no need do norm, please continue next step by using 'shifu varsel'");
                    }
                } else {
                    log.warn("Error in model set stats computation, please report issue on http:/github.com/shifuml/shifu/issues.");
                }
            } else if (cleanedArgs[0].equals(NORMALIZE_CMD) || cleanedArgs[0].equals(NORM_CMD)) {
                // normalize step
                status = normalizeTrainData(cmd.hasOption(SHUFFLE));
                if (status == 0) {
                    log.info("Do model set normalization successfully. Please continue next step by using 'shifu varselect or shifu varsel'.");
                } else {
                    log.warn("Error in model set stats computation, please report issue on http:/github.com/shifuml/shifu/issues.");
                }
            } else if (cleanedArgs[0].equals(VARSELECT_CMD) || cleanedArgs[0].equals(VARSEL_CMD)) {
                // variable selected step
                status = selectModelVar(cmd.hasOption(RESET), cmd.hasOption(LIST));
                if (status == 0) {
                    log.info("Do model set variables selection successfully. Please continue next step by using 'shifu train'.");
                } else {
                    log.info("Do variable selection with error, please check error message or report issue.");
                }
            } else if (cleanedArgs[0].equals(TRAIN_CMD)) {
                // train step
                status = trainModel(cmd.hasOption(TRAIN_CMD_DRY), cmd.hasOption(TRAIN_CMD_DEBUG), cmd.hasOption(SHUFFLE));
                if (status == 0) {
                    log.info("Do model set training successfully. Please continue next step by using 'shifu posttrain' or if no need posttrain you can go through with 'shifu eval'.");
                } else {
                    log.info("Do model training with error, please check error message or report issue.");
                }
            } else if (cleanedArgs[0].equals(CMD_COMBO)) {
                if (cmd.hasOption(MODELSET_CMD_NEW)) {
                    log.info("Create new commbo models");
                    status = createNewCombo(cmd.getOptionValue(MODELSET_CMD_NEW));
                } else if (cmd.hasOption(INIT_CMD)) {
                    log.info("Init commbo models");
                    status = initComboModels();
                } else if (cmd.hasOption(EVAL_CMD_RUN)) {
                    log.info("Run combo model - with toShuffle: {}, with toResume: {}", opts.hasOption(SHUFFLE), opts.hasOption(RESUME));
                    status = runComboModels(cmd.hasOption(SHUFFLE), cmd.hasOption(RESUME));
                // train combo models
                } else if (cmd.hasOption(EVAL_CMD)) {
                    log.info("Eval combo model.");
                    // eval combo model performance
                    status = evalComboModels(cmd.hasOption(RESUME));
                } else {
                    log.error("Invalid command usage.");
                    printUsage();
                }
            } else if (cleanedArgs[0].equals(POSTTRAIN_CMD)) {
                // post train step
                status = postTrainModel();
                if (status == 0) {
                    log.info("Do model set post-training successfully. Please configurate your eval set in ModelConfig.json and continue next step by using 'shifu eval' or 'shifu eval -new <eval set>' to create a new eval set.");
                } else {
                    log.info("Do model post training with error, please check error message or report issue.");
                }
            } else if (cleanedArgs[0].equals(SAVE)) {
                String newModelSetName = cleanedArgs.length >= 2 ? cleanedArgs[1] : null;
                saveCurrentModel(newModelSetName);
            } else if (cleanedArgs[0].equals(SWITCH)) {
                String newModelSetName = cleanedArgs[1];
                switchCurrentModel(newModelSetName);
            } else if (cleanedArgs[0].equals(SHOW)) {
                ManageModelProcessor p = new ManageModelProcessor(ModelAction.SHOW, null);
                p.run();
            } else if (cleanedArgs[0].equals(EVAL_CMD)) {
                // eval step
                if (cleanedArgs.length == 1) {
                    // run everything
                    status = runEvalSet(cmd.hasOption(TRAIN_CMD_DRY));
                    if (status == 0) {
                        log.info("Run eval performance with all eval sets successfully.");
                    } else {
                        log.info("Do evaluation with error, please check error message or report issue.");
                    }
                } else if (cmd.getOptionValue(MODELSET_CMD_NEW) != null) {
                    // create new eval
                    createNewEvalSet(cmd.getOptionValue(MODELSET_CMD_NEW));
                    log.info("Create eval set successfully. You can configurate EvalConfig.json or directly run 'shifu eval -run <evalSetName>' to get performance info.");
                } else if (cmd.hasOption(EVAL_CMD_RUN)) {
                    runEvalSet(cmd.getOptionValue(EVAL_CMD_RUN), cmd.hasOption(TRAIN_CMD_DRY));
                    log.info("Finish run eval performance with eval set {}.", cmd.getOptionValue(EVAL_CMD_RUN));
                } else if (cmd.hasOption(SCORE)) {
                    // run score
                    runEvalScore(cmd.getOptionValue(SCORE));
                    log.info("Finish run score with eval set {}.", cmd.getOptionValue(SCORE));
                } else if (cmd.hasOption(CONFMAT)) {
                    // run confusion matrix
                    runEvalConfMat(cmd.getOptionValue(CONFMAT));
                    log.info("Finish run confusion matrix with eval set {}.", cmd.getOptionValue(CONFMAT));
                } else if (cmd.hasOption(PERF)) {
                    // run perfermance
                    runEvalPerf(cmd.getOptionValue(PERF));
                    log.info("Finish run performance maxtrix with eval set {}.", cmd.getOptionValue(PERF));
                } else if (cmd.hasOption(LIST)) {
                    // list all evaluation sets
                    listEvalSet();
                } else if (cmd.hasOption(DELETE)) {
                    // delete some evaluation set
                    deleteEvalSet(cmd.getOptionValue(DELETE));
                } else if (cmd.hasOption(NORM)) {
                    runEvalNorm(cmd.getOptionValue(NORM));
                } else {
                    log.error("Invalid command, please check help message.");
                    printUsage();
                }
            } else if (cleanedArgs[0].equals(CMD_EXPORT)) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put(ExportModelProcessor.IS_CONCISE, cmd.hasOption(EXPORT_CONCISE));
                params.put(ExportModelProcessor.REQUEST_VARS, cmd.getOptionValue(VARS));
                params.put(ExportModelProcessor.EXPECTED_BIN_NUM, cmd.getOptionValue(N));
                params.put(ExportModelProcessor.IV_KEEP_RATIO, cmd.getOptionValue(IVR));
                params.put(ExportModelProcessor.MINIMUM_BIN_INST_CNT, cmd.getOptionValue(BIC));
                status = exportModel(cmd.getOptionValue(MODELSET_CMD_TYPE), params);
                if (status == 0) {
                    log.info("Export models/columnstats to PMML/csv format successfully in current folder.");
                } else {
                    log.warn("Export models/columnstats to PMML/csv format with error, please check or report issue.");
                }
            } else {
                log.error("Invalid command, please check help message.");
                printUsage();
            }
        }
        // for some case jvm cannot stop
        System.exit(status);
    } catch (ShifuException e) {
        // need define error code in each step.
        log.error(e.getError().toString(), e.getCause());
        exceptionExit(e);
    } catch (Exception e) {
        exceptionExit(e);
    }
}
Also used : Options(org.apache.commons.cli.Options) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GnuParser(org.apache.commons.cli.GnuParser) IOException(java.io.IOException) ShifuException(ml.shifu.shifu.exception.ShifuException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) ManageModelProcessor(ml.shifu.shifu.core.processor.ManageModelProcessor) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ShifuException(ml.shifu.shifu.exception.ShifuException)

Example 88 with GnuParser

use of org.apache.commons.cli.GnuParser in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method parseArgs.

private static CommandLine parseArgs(String[] args) throws org.apache.commons.cli.ParseException {
    CommandLineParser parser = new GnuParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(sOptions, args);
    } catch (org.apache.commons.cli.ParseException pe) {
        usage(pe.getMessage());
        System.exit(1);
    }
    return cl;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 89 with GnuParser

use of org.apache.commons.cli.GnuParser in project zm-mailbox by Zimbra.

the class MimeDetect method main.

public static void main(String[] args) {
    int limit = -1;
    MimeDetect md = new MimeDetect();
    Options opts = new Options();
    CommandLineParser parser = new GnuParser();
    int ret = 1;
    opts.addOption("d", "data", false, "data only");
    opts.addOption("g", "globs", true, "globs file");
    opts.addOption("l", "limit", true, "size limit");
    opts.addOption("m", "magic", true, "magic file");
    opts.addOption("n", "name", false, "name only");
    opts.addOption("v", "validate", false, "validate extension and data");
    try {
        CommandLine cl = parser.parse(opts, args);
        String file;
        String globs = LC.shared_mime_info_globs.value();
        String magic = LC.shared_mime_info_magic.value();
        String type;
        if (cl.hasOption('g'))
            globs = cl.getOptionValue('g');
        if (cl.hasOption('l'))
            limit = Integer.parseInt(cl.getOptionValue('l'));
        if (cl.hasOption('m'))
            magic = cl.getOptionValue('m');
        if (cl.getArgs().length != 1)
            usage(opts);
        file = cl.getArgs()[0];
        md.parse(globs, magic);
        if (cl.hasOption('n')) {
            type = md.detect(file);
        } else if (file.equals("-")) {
            type = md.detect(System.in, limit);
        } else if (cl.hasOption('d')) {
            type = md.detect(new FileInputStream(file), limit);
        } else if (cl.hasOption('v')) {
            type = md.validate(new File(file), limit);
        } else {
            type = md.detect(new File(file), limit);
        }
        if (type == null) {
            System.out.println("unknown");
        } else {
            System.out.println(type);
            ret = 0;
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e instanceof UnrecognizedOptionException)
            usage(opts);
    }
    System.exit(ret);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

Example 90 with GnuParser

use of org.apache.commons.cli.GnuParser in project zm-mailbox by Zimbra.

the class RandomPassword method main.

public static void main(String[] args) {
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption("l", "localpart", false, "generated string does not contain dot(.)");
    CommandLine cl = null;
    boolean err = false;
    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        System.err.println("error: " + pe.getMessage());
        err = true;
    }
    if (err || cl.hasOption('h')) {
        usage();
    }
    boolean localpart = false;
    int minLength = DEFAULT_MIN_LENGTH;
    int maxLength = DEFAULT_MAX_LENGTH;
    if (cl.hasOption('l'))
        localpart = true;
    args = cl.getArgs();
    if (args.length != 0) {
        if (args.length != 2) {
            usage();
        }
        try {
            minLength = Integer.valueOf(args[0]).intValue();
            maxLength = Integer.valueOf(args[1]).intValue();
        } catch (Exception e) {
            System.err.println(e);
            e.printStackTrace();
        }
    }
    System.out.println(generate(minLength, maxLength, localpart));
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ParseException(org.apache.commons.cli.ParseException)

Aggregations

GnuParser (org.apache.commons.cli.GnuParser)208 CommandLine (org.apache.commons.cli.CommandLine)187 Options (org.apache.commons.cli.Options)165 CommandLineParser (org.apache.commons.cli.CommandLineParser)158 ParseException (org.apache.commons.cli.ParseException)139 HelpFormatter (org.apache.commons.cli.HelpFormatter)92 Path (org.apache.hadoop.fs.Path)40 Option (org.apache.commons.cli.Option)39 IOException (java.io.IOException)32 Job (org.apache.hadoop.mapreduce.Job)27 File (java.io.File)24 Configuration (org.apache.hadoop.conf.Configuration)19 FileInputStream (java.io.FileInputStream)14 ArrayList (java.util.ArrayList)14 Properties (java.util.Properties)13 FileSystem (org.apache.hadoop.fs.FileSystem)11 MissingArgumentException (org.apache.commons.cli.MissingArgumentException)9 FileNotFoundException (java.io.FileNotFoundException)7 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)6