Search in sources :

Example 1 with MissingOptionException

use of org.apache.commons.cli.MissingOptionException in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsTransformerMain method run.

/*****************************************************************************
   * {@link Runnable} Interface
   ****************************************************************************/
public void run(String[] args) throws IOException {
    if (needsHelp(args)) {
        printHelp();
        System.exit(0);
    }
    try {
        CommandLine cli = _parser.parse(_options, args, true);
        runApplication(cli, args);
    } catch (MissingOptionException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (MissingArgumentException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (UnrecognizedOptionException ex) {
        System.err.println("Unknown argument: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (AlreadySelectedException ex) {
        System.err.println("Argument already selected: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (TransformSpecificationException ex) {
        System.err.println("error with transform line: " + ex.getLine());
        System.err.println(ex.getMessage());
        System.exit(-1);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(-1);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException)

Example 2 with MissingOptionException

use of org.apache.commons.cli.MissingOptionException in project saga by timurstrekalov.

the class Main method main.

public static void main(final String[] args) throws IOException, ParseException {
    logger.debug("Starting...");
    final Option baseDirOpt = new Option("b", "base-dir", true, "Base directory for test search");
    final Option includeOpt = new Option("i", "include", true, "Comma-separated list of Ant-style paths to the tests to run");
    final Option excludeOpt = new Option("e", "exclude", true, "Comma-separated list of Ant-style paths to the tests to exclude from run");
    final Option outputDirOpt = new Option("o", "output-dir", true, "The output directory for coverage reports");
    final Option outputInstrumentedFilesOpt = new Option("f", "output-instrumented-files", false, "Whether to output instrumented files (default is false)");
    final Option noInstrumentPatternOpt = new Option("n", "no-instrument-pattern", true, "Regular expression patterns to match classes to exclude from instrumentation");
    noInstrumentPatternOpt.setArgs(Option.UNLIMITED_VALUES);
    final Option sourcesToPreloadOpt = new Option("p", "preload-sources", true, "Comma-separated list of Ant-style paths to files to preload");
    final Option sourcesToPreloadEncodingOpt = new Option(null, "preload-sources-encoding", true, "Encoding to use when preloading sources");
    final Option threadCountOpt = new Option("t", "thread-count", true, "The maximum number of threads to use (defaults to the number of cores)");
    final Option outputStrategyOpt = new Option("s", "output-strategy", true, "Coverage report output strategy. One of " + Arrays.toString(OutputStrategy.values()));
    final Option includeInlineScriptsOpt = new Option("d", "include-inline-scripts", false, "Whether to include inline scripts into instrumentation by default (default is false)");
    final Option backgroundJavaScriptTimeoutOpt = new Option("j", "background-javascript-timeout", true, "How long to wait for background JavaScript to finish running (in milliseconds, default is 5 minutes)");
    final Option browserVersionOpt = new Option("v", "browser-version", true, "Determines the browser and version profile that HtmlUnit will simulate");
    final Option reportFormatsOpt = new Option(null, "report-formats", true, "A comma-separated list of formats of the reports to be generated. Valid values are: HTML, RAW, CSV, PDF");
    final Option sortByOpt = new Option(null, "sort-by", true, "The column to sort by, one of 'file', 'statements', 'executed' or 'coverage' (default is 'coverage')");
    final Option orderOpt = new Option(null, "order", true, "The order of sorting, one of 'asc' or 'ascending', 'desc' or 'descending' (default is 'ascending')");
    final Option helpOpt = new Option("h", "help", false, "Print this message");
    final Options options = new Options();
    options.addOption(baseDirOpt);
    options.addOption(includeOpt);
    options.addOption(excludeOpt);
    options.addOption(outputDirOpt);
    options.addOption(outputInstrumentedFilesOpt);
    options.addOption(noInstrumentPatternOpt);
    options.addOption(threadCountOpt);
    options.addOption(outputStrategyOpt);
    options.addOption(includeInlineScriptsOpt);
    options.addOption(helpOpt);
    options.addOption(sourcesToPreloadOpt);
    options.addOption(sourcesToPreloadEncodingOpt);
    options.addOption(backgroundJavaScriptTimeoutOpt);
    options.addOption(browserVersionOpt);
    options.addOption(reportFormatsOpt);
    options.addOption(sortByOpt);
    options.addOption(orderOpt);
    logger.debug("Finished configuring options");
    try {
        CommandLineParser parser = new GnuParser();
        CommandLine line = parser.parse(options, args, false);
        logger.debug("Parsed the arguments, take 1");
        baseDirOpt.setRequired(true);
        outputDirOpt.setRequired(true);
        options.addOption(baseDirOpt);
        options.addOption(outputDirOpt);
        if (line.hasOption(helpOpt.getLongOpt())) {
            printHelpAndExit(options);
        }
        parser = new GnuParser();
        line = parser.parse(options, args);
        logger.debug("Parsed the arguments, take 2");
        final String baseDir = line.getOptionValue(baseDirOpt.getLongOpt());
        final String includes = line.getOptionValue(includeOpt.getLongOpt());
        final String excludes = line.getOptionValue(excludeOpt.getLongOpt());
        final File outputDir = new File(line.getOptionValue(outputDirOpt.getLongOpt()));
        final CoverageGenerator gen = CoverageGeneratorFactory.newInstance(baseDir, outputDir);
        final Config config = gen.getConfig();
        config.setIncludes(includes);
        config.setExcludes(excludes);
        if (line.hasOption(outputInstrumentedFilesOpt.getLongOpt())) {
            config.setOutputInstrumentedFiles(true);
        }
        config.setNoInstrumentPatterns(line.getOptionValues(noInstrumentPatternOpt.getLongOpt()));
        config.setSourcesToPreload(line.getOptionValue(sourcesToPreloadOpt.getLongOpt()));
        config.setOutputStrategy(line.getOptionValue(outputStrategyOpt.getLongOpt()));
        final String threadCount = line.getOptionValue(threadCountOpt.getLongOpt());
        if (threadCount != null) {
            try {
                config.setThreadCount(Integer.parseInt(threadCount));
            } catch (final Exception e) {
                System.err.println("Invalid thread count");
                printHelpAndExit(options);
            }
        }
        if (line.hasOption(includeInlineScriptsOpt.getLongOpt())) {
            config.setIncludeInlineScripts(true);
        }
        final String backgroundJavaScriptTimeout = line.getOptionValue(backgroundJavaScriptTimeoutOpt.getLongOpt());
        if (backgroundJavaScriptTimeout != null) {
            try {
                config.setBackgroundJavaScriptTimeout(Long.valueOf(backgroundJavaScriptTimeout));
            } catch (final Exception e) {
                System.err.println("Invalid timeout");
                printHelpAndExit(options);
            }
        }
        config.setBrowserVersion(line.getOptionValue(browserVersionOpt.getLongOpt()));
        config.setReportFormats(line.getOptionValue(reportFormatsOpt.getLongOpt()));
        config.setSortBy(line.getOptionValue(sortByOpt.getLongOpt()));
        config.setOrder(line.getOptionValue(orderOpt.getLongOpt()));
        logger.debug("Configured the coverage generator, running");
        gen.instrumentAndGenerateReports();
    } catch (final MissingOptionException e) {
        System.err.println(e.getMessage());
        printHelpAndExit(options);
    } catch (final UnrecognizedOptionException e) {
        System.err.println(e.getMessage());
        printHelpAndExit(options);
    } catch (final ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CoverageGenerator(com.github.timurstrekalov.saga.core.CoverageGenerator) Config(com.github.timurstrekalov.saga.core.cfg.Config) GnuParser(org.apache.commons.cli.GnuParser) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) ParseException(org.apache.commons.cli.ParseException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Example 3 with MissingOptionException

use of org.apache.commons.cli.MissingOptionException in project hbase by apache.

the class AbstractHBaseTool method run.

@Override
public int run(String[] args) throws IOException {
    cmdLineArgs = args;
    if (conf == null) {
        LOG.error("Tool configuration is not initialized");
        throw new NullPointerException("conf");
    }
    CommandLine cmd;
    List<String> argsList = new ArrayList<>(args.length);
    for (String arg : args) {
        argsList.add(arg);
    }
    // For backward compatibility of args which can't be parsed as Option. See javadoc for
    // processOldArgs(..)
    processOldArgs(argsList);
    try {
        addOptions();
        if (isHelpCommand(args)) {
            printUsage();
            return EXIT_SUCCESS;
        }
        String[] remainingArgs = new String[argsList.size()];
        argsList.toArray(remainingArgs);
        cmd = new DefaultParser().parse(options, remainingArgs);
    } catch (MissingOptionException e) {
        LOG.error(e.getMessage());
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    } catch (ParseException e) {
        LOG.error("Error when parsing command-line arguments", e);
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    }
    processOptions(cmd);
    int ret;
    try {
        ret = doWork();
    } catch (Exception e) {
        LOG.error("Error running command-line tool", e);
        return EXIT_FAILURE;
    }
    return ret;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 4 with MissingOptionException

use of org.apache.commons.cli.MissingOptionException in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMergerMain method run.

/*****************************************************************************
   * {@link Runnable} Interface
   ****************************************************************************/
public void run(String[] args) throws IOException {
    if (needsHelp(args)) {
        printHelp();
        System.exit(0);
    }
    try {
        CommandLine cli = _parser.parse(_options, args, true);
        runApplication(cli, args);
    } catch (MissingOptionException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
    } catch (MissingArgumentException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
    } catch (UnrecognizedOptionException ex) {
        System.err.println("Unknown argument: " + ex.getMessage());
        printHelp();
    } catch (AlreadySelectedException ex) {
        System.err.println("Argument already selected: " + ex.getMessage());
        printHelp();
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException)

Aggregations

IOException (java.io.IOException)4 CommandLine (org.apache.commons.cli.CommandLine)4 MissingOptionException (org.apache.commons.cli.MissingOptionException)4 ParseException (org.apache.commons.cli.ParseException)4 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)3 AlreadySelectedException (org.apache.commons.cli.AlreadySelectedException)2 MissingArgumentException (org.apache.commons.cli.MissingArgumentException)2 CoverageGenerator (com.github.timurstrekalov.saga.core.CoverageGenerator)1 Config (com.github.timurstrekalov.saga.core.cfg.Config)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 Option (org.apache.commons.cli.Option)1 Options (org.apache.commons.cli.Options)1