Example usage for org.apache.commons.cli PosixParser parse

List of usage examples for org.apache.commons.cli PosixParser parse

Introduction

In this page you can find the example usage for org.apache.commons.cli PosixParser parse.

Prototype

public CommandLine parse(Options options, String[] arguments) throws ParseException 

Source Link

Document

Parses the specified arguments based on the specifed Options .

Usage

From source file:org.multicore_association.measure.cycle.writeback.ParameterParser.java

/**
 * Check and Parse the command line parameters.
 * @param args command line parameters//w  ww  .  j ava  2s .c  om
 * @return Flag for success judgements
 * @throws ParseException
 */
public boolean parseParam(String[] args) {
    boolean ch = true;

    PosixParser parser = new PosixParser();
    org.apache.commons.cli.Options opts = generateOpts();

    try {
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        //         e.printStackTrace();
        System.err.println(e.getMessage());
        //         printHelp();
        return false;
    }

    if (cl.hasOption(PRINTHELP_LOPT)) {
        //         printHelp();
        return false;
    }

    System.out.println();

    if (cl.hasOption(INPUT_SHIM_LOPT)) {
        inShimPath = cl.getOptionValue(INPUT_SHIM_LOPT);
        System.out.println("Input SHIM: " + inShimPath + "");
    } else {
        System.out.println("Input SHIM: None specified");
    }

    outShimPath = cl.getOptionValue(OUTPUT_SHIM_LOPT);
    System.out.println("Output SHIM:" + outShimPath + "");

    if (cl.hasOption(CSV_DIR_LOPT)) {
        csvDir = cl.getOptionValue(CSV_DIR_LOPT);
        System.out.println("Result CSV directory: " + csvDir + "");
    } else {
        csvDir = null;
        csvFileList = new ArrayList<String>();
        String[] files = cl.getArgs();
        System.out.println("Result CSV file(s):");
        for (int i = 0; i < files.length; i++) {
            System.out.println("    " + files[i]);
            csvFileList.add(files[i]);
        }
    }

    if (cl.hasOption(ARCH_NAME_LOPT)) {
        architectureName = cl.getOptionValue(ARCH_NAME_LOPT);
        System.out.println("Architecture: " + architectureName + "");
    } else {
        System.out.println("Architecture: None specified");
    }

    if (cl.hasOption(INSTSET_NAME_LOPT)) {
        instructionSetName = cl.getOptionValue(INSTSET_NAME_LOPT);
        System.out.println("CommonInstructionSet name: " + instructionSetName + "");
    } else {
        System.out.println("CommonInstructionSet name: None specified");
    }

    if (cl.hasOption(SHIM_SCHEMA_LOPT)) {
        shimSchemaPath = cl.getOptionValue(SHIM_SCHEMA_LOPT);
        System.out.println("SHIM Schema: " + shimSchemaPath + "");
    } else {
        System.out.println("SHIM Schema: None specified");
    }

    if (cl.hasOption(RECURSIVE_LOPT)) {
        isRecursive = true;
    }
    System.out.println();

    return ch;
}

From source file:org.multicore_association.measure.mem.generate.MemCodeGen.java

/**
 * Check the command line parameters./*from w w w .  j  a va2  s . c om*/
 * @param args command line parameters
 * @return Flag for success judgements
 */
@SuppressWarnings("static-access")
private boolean checkParam(String[] args) {
    boolean ch = true;

    Option optShimSchema = OptionBuilder.withLongOpt("shim-schema").withDescription("SHIM XML schema file path")
            .hasArg().withArgName("path").create();

    Options opts = new Options();
    opts.addOption(optShimSchema);

    PosixParser parser = new PosixParser();
    CommandLine cl = null;

    try {
        cl = parser.parse(opts, args);

        if (cl.hasOption("shim-schema")) {
            shimSchemaPath = cl.getOptionValue("shim-schema");
        }

        if (cl.getArgs().length != 2) {
            System.err.println("java -jar MemCodeGen.jar [opts] config_file_path shim_config_file_path");
            ch = false;
        } else {
            conff = new File(cl.getArgs()[0]);
            if (!conff.exists()) {
                System.err.println("Error: Config file(.cfg) does not exist");
                ch = false;
            }

            shimf = new File(cl.getArgs()[1]);
            if (!shimf.exists()) {
                System.err.println("Error: SHIM file(.xml) does not exist");
                ch = false;
            }
        }

        if (!shimSchemaPath.equals("")) {
            File temp = new File(shimSchemaPath);
            if (!temp.exists()) {
                System.err.println("Error: SHIM schema file(.xsd) does not exist");
                ch = false;
            }
        }

        if (!ch) {
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp("java -jar MeasurementMemoryCodegen.jar [opts] config_file_path shim_file_path", opts);
        }
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar MeasurementMemoryCodegen.jar [opts] config_file_path shim_file_path", opts);
        ch = false;
    }

    return ch;
}

From source file:org.multicore_association.measure.mem.writeback.SetResultToShim.java

/**
 * Check the command line parameters.//from  ww w  .j a  v  a  2 s  . c  om
 * @param args command line parameters
 */
@SuppressWarnings("static-access")
private static boolean checkParam(String[] args) {
    boolean ch = true;

    Option optShimSchema = OptionBuilder.withLongOpt("shim-schema").withDescription("SHIM XML schema file path")
            .hasArg().withArgName("path").create();

    Options opts = new Options();
    opts.addOption(optShimSchema);

    PosixParser parser = new PosixParser();
    CommandLine cl = null;

    try {
        cl = parser.parse(opts, args);

        if (cl.hasOption("shim-schema")) {
            shimSchemaPath = cl.getOptionValue("shim-schema");
        }

        if (cl.getArgs().length == 2) {
            csvf = new File(cl.getArgs()[0]);
            if (!csvf.exists()) {
                System.err.println("Error: measurements file(.csv) does not exist");
                ch = false;
            }

            shimf = new File(cl.getArgs()[1]);
            if (!shimf.exists()) {
                System.err.println("Error: SHIM file(.xml) does not exist");
                ch = false;
            }
        } else if (cl.getArgs().length == 1) {
            csvf = new File(cl.getArgs()[0]);
            if (!csvf.exists()) {
                System.err.println("Error: measurements file(.csv) does not exist");
                ch = false;
            }
        } else {
            System.err.println("Error: too few arguments");
            ch = false;
        }

        if (!shimSchemaPath.equals("")) {
            File temp = new File(shimSchemaPath);
            if (!temp.exists()) {
                System.err.println("Error: SHIM schema file(.xsd) does not exist");
                ch = false;
            }
        }

        if (!ch) {
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp(
                    "java -jar MeasurementMemoryWriteback.jar [opts] measurements_file_path shim_file_path",
                    opts);
        }

    } catch (ParseException e) {
        System.err.println(e.getMessage());
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar MeasurementMemoryWriteback.jar [opts] measurements_file_path shim_file_path",
                opts);
        ch = false;
    }

    return ch;
}

From source file:org.onebusaway.siri.client.cli.SiriClientMain.java

public void run(String[] args) throws Exception {

    if (SiriLibrary.needsHelp(args)) {
        printUsage();/*from  w  w  w. j a va  2 s  .  c  o m*/
        System.exit(0);
    }

    Options options = new Options();
    buildOptions(options);

    Daemonizer.buildOptions(options);

    PosixParser parser = new PosixParser();
    CommandLine cli = parser.parse(options, args);

    Daemonizer.handleDaemonization(cli);

    args = cli.getArgs();

    Set<Module> modules = new HashSet<Module>();
    SiriCoreModule.addModuleAndDependencies(modules);
    SiriJettyModule.addModuleAndDependencies(modules);
    Injector injector = Guice.createInjector(modules);

    injector.injectMembers(this);

    if (cli.hasOption(ARG_ID))
        _client.setIdentity(cli.getOptionValue(ARG_ID));

    if (cli.hasOption(ARG_CLIENT_URL))
        _client.setUrl(cli.getOptionValue(ARG_CLIENT_URL));

    if (cli.hasOption(ARG_PRIVATE_CLIENT_URL))
        _client.setPrivateUrl(cli.getOptionValue(ARG_PRIVATE_CLIENT_URL));

    if (cli.hasOption(ARG_LOG_RAW_XML)) {
        String value = cli.getOptionValue(ARG_LOG_RAW_XML);
        ELogRawXmlType type = ELogRawXmlType.valueOf(value.toUpperCase());
        _client.setLogRawXmlType(type);
    }

    if (cli.hasOption(ARG_OUTPUT)) {
        _outputFormat = cli.getOptionValue(ARG_OUTPUT);
        _log.info("setting output=" + _outputFormat);
    } else {
        _output = new PrintWriter(System.out);
    }

    if (cli.hasOption(ARG_RESPONSE_TIMEOUT)) {
        int responseTimeout = Integer.parseInt(cli.getOptionValue(ARG_RESPONSE_TIMEOUT));
        _schedulingService.setResponseTimeout(responseTimeout);
    }
    if (cli.hasOption(ARG_CONNECTION_TIMEOUT)) {
        int connectionTimeout = Integer.parseInt(cli.getOptionValue(ARG_CONNECTION_TIMEOUT));
        _client.setConnectionTimeout(connectionTimeout);
    }

    _client.setFormatOutputXmlByDefault(cli.hasOption(ARG_FORMAT_OUTPUT_XML));

    _client.addServiceDeliveryHandler(new ServiceDeliveryHandlerImpl());

    if (args.length == 0 && !cli.hasOption(ARG_NO_SUBSCRIPTIONS)) {
        printUsage();
        System.exit(-1);
    }

    /**
     * Need to make sure that we fire off all the @PostConstruct service
     * annotations. The @PreDestroy annotations are automatically included in a
     * shutdown hook.
     */
    _lifecycleService.start();

    ERequestType requestType = getRequestType(cli);
    SiriClientRequestFactory factory = new SiriClientRequestFactory();

    for (String arg : args) {
        SiriClientRequest request = getLineAsRequest(requestType, factory, arg);
        Siri delivery = _client.handleRequestWithResponse(request);
        if (delivery != null)
            printAsXml(delivery);
    }
}

From source file:org.onebusaway.siri.client.cli.SiriReplayMain.java

public void run(String[] args) throws Exception {

    if (SiriLibrary.needsHelp(args)) {
        printUsage();//w ww . ja  v  a  2  s  .c  om
        System.exit(0);
    }

    Options options = new Options();
    buildOptions(options);

    PosixParser parser = new PosixParser();
    CommandLine cli = parser.parse(options, args);

    args = cli.getArgs();

    if (args.length != 1) {
        printUsage();
        System.exit(-1);
    }

    Set<Module> modules = new HashSet<Module>();
    SiriCoreModule.addModuleAndDependencies(modules);
    SiriJettyModule.addModuleAndDependencies(modules);
    Injector injector = Guice.createInjector(modules);

    injector.injectMembers(this);

    if (cli.hasOption(ARG_ID))
        _server.setIdentity(cli.getOptionValue(ARG_ID));

    if (cli.hasOption(ARG_SERVER_URL))
        _server.setUrl(cli.getOptionValue(ARG_SERVER_URL));

    if (cli.hasOption(ARG_PRIVATE_SERVER_URL))
        _server.setPrivateUrl(cli.getOptionValue(ARG_PRIVATE_SERVER_URL));

    _inRealtime = cli.hasOption(ARG_IN_REALTIME);
    _noWaitForSubscription = cli.hasOption(ARG_NO_WAIT_FOR_SUBSCRIPTION);

    if (cli.hasOption(ARG_DEFAULT_DELAY)) {
        _defaultDelay = Long.parseLong(cli.getOptionValue(ARG_DEFAULT_DELAY));
    }

    _subscriptionManager.addListener(new SiriSubscriptionManagerListenerImpl());

    File dataDir = new File(args[0]);
    for (File file : dataDir.listFiles()) {
        if (!file.getName().endsWith(".xml"))
            continue;
        _dataFiles.add(file);
    }

    /**
     * Need to make sure that we fire off all the @PostConstruct service
     * annotations. The @PreDestroy annotations are automatically included in a
     * shutdown hook.
     */
    _lifecycleService.start();

    checkExecutor();
}

From source file:timemlvalidator.Main.java

/**
 * @param args the command line arguments
 *//*from  w w w.  j  ava  2  s .  c  o  m*/
public static void main(String[] args) {
    String type = "tml"; // normal
    String input_files[];
    int incorrect_files = 0;

    try {

        Options opt = new Options();
        //addOption(String opt, boolean hasArg, String description)
        opt.addOption("h", "help", false, "Print this help");
        opt.addOption("t", "type", true, "Type of validation: normal (default) or minimum");
        opt.addOption("d", "debug", false, "Debug mode: Output errors stack trace (default: disabled)");

        PosixParser parser = new PosixParser();
        CommandLine cl_options = parser.parse(opt, args);
        HelpFormatter hf = new HelpFormatter();
        if (cl_options.hasOption('h')) {
            hf.printHelp("TimeML-validator", opt);
            System.exit(0);
        } else {
            if (cl_options.hasOption('d')) {
                System.setProperty("DEBUG", "true");
            }

            if (cl_options.hasOption('t')) {
                type = cl_options.getOptionValue("t");
                if (!type.equalsIgnoreCase("normal") && !type.equalsIgnoreCase("minimum")
                        && !type.equalsIgnoreCase("just-ids")) {
                    throw new Exception("Type (-t) must be either 'normal' or 'minimum' or 'just-ids'.");
                }
            }

            if (type.equalsIgnoreCase("minimum")) {
                type = "tml-min-consistency";
            } else {
                if (type.equalsIgnoreCase("just-ids")) {
                    type = "tml-just-ids";
                } else {
                    type = "tml";
                }
            }

            input_files = cl_options.getArgs();

            for (int i = 0; i < input_files.length; i++) {
                File f = new File(input_files[i]);
                if (!f.exists()) {
                    throw new FileNotFoundException("File does not exist: " + f);
                }

                if (f.isFile()) {
                    File[] files = { f };
                    XMLFile xmlfile = new XMLFile();
                    xmlfile.loadFile(f);
                    xmlfile.overrideExtension(type);
                    if (!xmlfile.isWellFormed() || !validateTEXTDCT(f)) {
                        if (System.getProperty("DEBUG") != null
                                && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
                            throw new Exception("File: " + xmlfile.getFile().getCanonicalPath()
                                    + " is not a valid TimeML XML file.");
                        } else {
                            System.out.println("File: " + xmlfile.getFile().getCanonicalPath()
                                    + " is not a valid TimeML XML file.");
                            incorrect_files++;
                        }
                    }
                } else {
                    File[] files = f.listFiles(FileUtils.onlyFilesFilter);
                    if (files.length == 0) {
                        throw new Exception("Empty folder: " + f.getName());
                    }
                    for (int fn = 0; fn < files.length; fn++) {
                        XMLFile xmlfile = new XMLFile();
                        xmlfile.loadFile(files[fn]);
                        xmlfile.overrideExtension(type);
                        if (!xmlfile.isWellFormed() || !validateTEXTDCT(xmlfile.getFile())) {
                            if (System.getProperty("DEBUG") != null
                                    && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
                                throw new Exception("File: " + xmlfile.getFile().getCanonicalPath()
                                        + " is not a valid TimeML XML file.");
                            } else {
                                System.out.println("File: " + xmlfile.getFile().getCanonicalPath()
                                        + " is not a valid TimeML XML file.");
                                incorrect_files++;
                            }
                        }
                    }
                }

            }

        }
        if (incorrect_files == 0)
            System.out.println("\n\tAll the files are valid!\n\n");
        else
            System.out.println("\n\tERROR:" + incorrect_files + " files are INCORRECT.\n\n");

    } catch (Exception e) {
        System.err.println("Errors found:\n\t" + e.getMessage() + "\n");
        if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
            e.printStackTrace(System.err);
        }
        System.exit(1);
    }

}