Search in sources :

Example 1 with MissingOptionException

use of org.apache.hbase.thirdparty.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;
    Objects.requireNonNull(conf, "Tool configuration is not initialized");
    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 = newParser().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.hbase.thirdparty.org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Example 2 with MissingOptionException

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

the class LoadTestTool method newParser.

@Override
protected CommandLineParser newParser() {
    // Validate in parse() to get helpful error messages instead of exploding in processOptions()
    return new DefaultParser() {

        @Override
        public CommandLine parse(Options opts, String[] args, Properties props, boolean stop) throws ParseException {
            CommandLine cl = super.parse(opts, args, props, stop);
            boolean isReadWriteUpdate = cmd.hasOption(OPT_READ) || cmd.hasOption(OPT_WRITE) || cmd.hasOption(OPT_UPDATE);
            boolean isInitOnly = cmd.hasOption(OPT_INIT_ONLY);
            if (!isInitOnly && !isReadWriteUpdate) {
                throw new MissingOptionException("Must specify either -" + OPT_INIT_ONLY + " or at least one of -" + OPT_READ + ", -" + OPT_WRITE + ", -" + OPT_UPDATE);
            }
            if (isInitOnly && isReadWriteUpdate) {
                throw new AlreadySelectedException(OPT_INIT_ONLY + " cannot be specified with any of -" + OPT_READ + ", -" + OPT_WRITE + ", -" + OPT_UPDATE);
            }
            if (isReadWriteUpdate && !cmd.hasOption(OPT_NUM_KEYS)) {
                throw new MissingOptionException(OPT_NUM_KEYS + " must be specified in read/write mode.");
            }
            return cl;
        }
    };
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) AlreadySelectedException(org.apache.hbase.thirdparty.org.apache.commons.cli.AlreadySelectedException) Properties(java.util.Properties) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)

Aggregations

CommandLine (org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine)2 MissingOptionException (org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 AlreadySelectedException (org.apache.hbase.thirdparty.org.apache.commons.cli.AlreadySelectedException)1 DefaultParser (org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)1 Options (org.apache.hbase.thirdparty.org.apache.commons.cli.Options)1 ParseException (org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)1