Search in sources :

Example 36 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project flink by apache.

the class CliFrontendParser method parseCancelCommand.

public static CancelOptions parseCancelCommand(String[] args) throws CliArgsException {
    try {
        DefaultParser parser = new DefaultParser();
        CommandLine line = parser.parse(CANCEL_OPTIONS, args, false);
        return new CancelOptions(line);
    } catch (ParseException e) {
        throw new CliArgsException(e.getMessage());
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 37 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project flink by apache.

the class CliFrontendParser method parseInfoCommand.

public static InfoOptions parseInfoCommand(String[] args) throws CliArgsException {
    try {
        DefaultParser parser = new DefaultParser();
        CommandLine line = parser.parse(INFO_OPTIONS, args, true);
        return new InfoOptions(line);
    } catch (ParseException e) {
        throw new CliArgsException(e.getMessage());
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 38 with DefaultParser

use of org.apache.commons.cli.DefaultParser 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 39 with DefaultParser

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

the class AbstractHBaseTool method isHelpCommand.

private boolean isHelpCommand(String[] args) throws ParseException {
    Options helpOption = new Options().addOption(HELP_OPTION);
    // this parses the command line but doesn't throw an exception on unknown options
    CommandLine cl = new DefaultParser().parse(helpOption, args, true);
    return cl.getOptions().length != 0;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 40 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project heron by twitter.

the class JavaCheckstyle method main.

public static void main(String[] args) throws IOException {
    CommandLineParser parser = new DefaultParser();
    // create the Options
    Options options = new Options();
    options.addOption(Option.builder("f").required(true).hasArg().longOpt("extra_action_file").desc("bazel extra action protobuf file").build());
    options.addOption(Option.builder("hc").required(true).hasArg().longOpt("heron_checkstyle_config_file").desc("checkstyle config file").build());
    options.addOption(Option.builder("ac").required(true).hasArg().longOpt("apache_checkstyle_config_file").desc("checkstyle config file for imported source files").build());
    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        String extraActionFile = line.getOptionValue("f");
        String configFile = line.getOptionValue("hc");
        String apacheConfigFile = line.getOptionValue("ac");
        // check heron source file style
        String[] heronSourceFiles = getHeronSourceFiles(extraActionFile);
        checkStyle(heronSourceFiles, configFile);
        // check other apache source file style
        String[] apacheSourceFiles = getApacheSourceFiles(extraActionFile);
        checkStyle(apacheSourceFiles, apacheConfigFile);
    } catch (ParseException exp) {
        LOG.severe(String.format("Invalid input to %s: %s", CLASSNAME, exp.getMessage()));
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java " + CLASSNAME, options);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

DefaultParser (org.apache.commons.cli.DefaultParser)65 CommandLine (org.apache.commons.cli.CommandLine)63 Options (org.apache.commons.cli.Options)49 CommandLineParser (org.apache.commons.cli.CommandLineParser)45 ParseException (org.apache.commons.cli.ParseException)43 HelpFormatter (org.apache.commons.cli.HelpFormatter)30 Option (org.apache.commons.cli.Option)20 IOException (java.io.IOException)8 ToolOptions (com.google.api.tools.framework.tools.ToolOptions)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 File (java.io.File)5 Config (com.twitter.heron.spi.common.Config)3 PrintStream (java.io.PrintStream)3 Topology (com.ibm.streamsx.topology.Topology)2 PackingException (com.twitter.heron.spi.packing.PackingException)2 FileNotFoundException (java.io.FileNotFoundException)2 PrintWriter (java.io.PrintWriter)2 Path (java.nio.file.Path)2 Properties (java.util.Properties)2