Search in sources :

Example 21 with ParseException

use of org.apache.commons.cli.ParseException in project alluxio by Alluxio.

the class JournalTool method parseInputArgs.

/**
   * Parses the input args with a command line format, using
   * {@link org.apache.commons.cli.CommandLineParser}.
   *
   * @param args the input args
   * @return true if parsing succeeded
   */
private static boolean parseInputArgs(String[] args) {
    CommandLineParser parser = new BasicParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(OPTIONS, args);
    } catch (ParseException e) {
        System.out.println("Failed to parse input args: " + e);
        return false;
    }
    sNoTimeout = cmd.hasOption("noTimeout");
    sHelp = cmd.hasOption("help");
    return true;
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 22 with ParseException

use of org.apache.commons.cli.ParseException in project alluxio by Alluxio.

the class AlluxioFuse method parseOptions.

/**
   * Parses CLI options.
   *
   * @param args CLI args
   * @return Alluxio-FUSE configuration options
   */
private static AlluxioFuseOptions parseOptions(String[] args) {
    final Options opts = new Options();
    final Option mntPoint = Option.builder("m").hasArg().required(false).longOpt("mount-point").desc("Desired local mount point for alluxio-fuse.").build();
    final Option alluxioRoot = Option.builder("r").hasArg().required(false).longOpt("alluxio-root").desc("Path within alluxio that will be used as the root of the FUSE mount " + "(e.g., /users/foo; defaults to /)").build();
    final Option help = Option.builder("h").required(false).desc("Print this help").build();
    final Option fuseOption = Option.builder("o").valueSeparator(',').required(false).hasArgs().desc("FUSE mount options").build();
    opts.addOption(mntPoint);
    opts.addOption(alluxioRoot);
    opts.addOption(help);
    opts.addOption(fuseOption);
    final CommandLineParser parser = new DefaultParser();
    try {
        CommandLine cli = parser.parse(opts, args);
        if (cli.hasOption("h")) {
            final HelpFormatter fmt = new HelpFormatter();
            fmt.printHelp(AlluxioFuse.class.getName(), opts);
            return null;
        }
        String mntPointValue = cli.getOptionValue("m");
        String alluxioRootValue = cli.getOptionValue("r");
        List<String> fuseOpts = new ArrayList<>();
        boolean noUserMaxWrite = true;
        if (cli.hasOption("o")) {
            String[] fopts = cli.getOptionValues("o");
            // keep the -o
            for (final String fopt : fopts) {
                fuseOpts.add("-o" + fopt);
                if (noUserMaxWrite && fopt.startsWith("max_write")) {
                    noUserMaxWrite = false;
                }
            }
        }
        // from conf
        if (noUserMaxWrite) {
            final long maxWrite = Configuration.getLong(PropertyKey.FUSE_MAXWRITE_BYTES);
            fuseOpts.add(String.format("-omax_write=%d", maxWrite));
        }
        if (mntPointValue == null) {
            mntPointValue = Configuration.get(PropertyKey.FUSE_MOUNT_DEFAULT);
            LOG.info("Mounting on default {}", mntPointValue);
        }
        if (alluxioRootValue == null) {
            alluxioRootValue = Configuration.get(PropertyKey.FUSE_FS_ROOT);
            LOG.info("Using default alluxio root {}", alluxioRootValue);
        }
        final boolean fuseDebug = Configuration.getBoolean(PropertyKey.FUSE_DEBUG_ENABLED);
        return new AlluxioFuseOptions(mntPointValue, alluxioRootValue, fuseDebug, fuseOpts);
    } catch (ParseException e) {
        System.err.println("Error while parsing CLI: " + e.getMessage());
        final HelpFormatter fmt = new HelpFormatter();
        fmt.printHelp(AlluxioFuse.class.getName(), opts);
        return null;
    }
}
Also used : Options(org.apache.commons.cli.Options) ArrayList(java.util.ArrayList) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 23 with ParseException

use of org.apache.commons.cli.ParseException in project alluxio by Alluxio.

the class GetConf method getConf.

/**
   * Implements get configuration.
   *
   * @param args list of arguments
   * @return 0 on success, 1 on failures
   */
public static int getConf(String... args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(OPTIONS, args, true);
    } catch (ParseException e) {
        printHelp("Unable to parse input args: " + e.getMessage());
        return 1;
    }
    Preconditions.checkNotNull(cmd, "Unable to parse input args");
    args = cmd.getArgs();
    switch(args.length) {
        case 0:
            for (Entry<String, String> entry : Configuration.toMap().entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                System.out.println(String.format("%s=%s", key, value));
            }
            break;
        case 1:
            if (!PropertyKey.isValid(args[0])) {
                printHelp(String.format("%s is not a valid configuration key", args[0]));
                return 1;
            }
            PropertyKey key = PropertyKey.fromString(args[0]);
            if (!Configuration.containsKey(key)) {
                System.out.println("");
            } else {
                if (cmd.hasOption(UNIT_OPTION_NAME)) {
                    String arg = cmd.getOptionValue(UNIT_OPTION_NAME).toUpperCase();
                    Unit unit;
                    try {
                        unit = Unit.valueOf(arg);
                        System.out.println(Configuration.getBytes(key) / unit.getValue());
                    } catch (IllegalArgumentException e) {
                        printHelp(String.format("%s is not a valid unit", arg));
                        return 1;
                    }
                } else {
                    System.out.println(Configuration.get(key));
                }
            }
            break;
        default:
            printHelp("More arguments than expected");
            return 1;
    }
    return 0;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) PropertyKey(alluxio.PropertyKey) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 24 with ParseException

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

the class ConfigTester method main.

public static void main(String[] argv) {
    FilterDaoFactory.setInstance(new ConfigTesterFilterDao());
    DataSourceFactory.setInstance(new ConfigTesterDataSource());
    ConfigTester tester = BeanUtils.getBean("configTesterContext", "configTester", ConfigTester.class);
    final CommandLineParser parser = new PosixParser();
    final Options options = new Options();
    options.addOption("h", "help", false, "print this help and exit");
    options.addOption("a", "all", false, "check all supported configuration files");
    options.addOption("l", "list", false, "list supported configuration files and exit");
    options.addOption("v", "verbose", false, "list each configuration file as it is tested");
    options.addOption("i", "ignore-unknown", false, "ignore unknown configuration files and continue processing");
    final CommandLine line;
    try {
        line = parser.parse(options, argv, false);
    } catch (ParseException e) {
        System.err.println("Invalid usage: " + e.getMessage());
        System.err.println("Run 'config-tester -h' for help.");
        System.exit(1);
        // not reached; here to eliminate warning on line being uninitialized
        return;
    }
    final boolean ignoreUnknown = line.hasOption("i");
    if ((line.hasOption('l') || line.hasOption('h') || line.hasOption('a'))) {
        if (line.getArgList().size() > 0) {
            System.err.println("Invalid usage: No arguments allowed when using the '-a', '-h', or '-l' options.");
            System.err.println("Run 'config-tester -h' for help.");
            System.exit(1);
        }
    } else {
        if (line.getArgs().length == 0) {
            System.err.println("Invalid usage: too few arguments.  Use the '-h' option for help.");
            System.exit(1);
        }
    }
    boolean verbose = line.hasOption('v');
    DataSourceFactory.setInstance(new ConfigTesterDataSource());
    if (line.hasOption('l')) {
        System.out.println("Supported configuration files: ");
        for (String configFile : tester.getConfigs().keySet()) {
            System.out.println("    " + configFile);
        }
        System.out.println("Note: not all OpenNMS configuration files are currently supported.");
    } else if (line.hasOption('h')) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("config-tester -a\nOR: config-tester [config files]\nOR: config-tester -l\nOR: config-tester -h", options);
    } else if (line.hasOption('a')) {
        for (String configFile : tester.getConfigs().keySet()) {
            tester.testConfig(configFile, verbose, ignoreUnknown);
        }
    } else {
        for (String configFile : line.getArgs()) {
            tester.testConfig(configFile, verbose, ignoreUnknown);
        }
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 25 with ParseException

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

the class Mib2Events method parseCli.

public void parseCli(String[] argv) {
    Options opts = new Options();
    opts.addOption("m", "mib", true, "Pathname or URL of MIB file to scan for traps");
    opts.addOption("b", "ueibase", true, "Base UEI for resulting events");
    opts.addOption("c", "compat", false, "Turn on compatibility mode to create output as similar to mib2opennms as possible");
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(opts, argv);
        if (cmd.hasOption('m')) {
            m_mibLocation = cmd.getOptionValue('m');
        } else {
            printHelp("You must specify a MIB file pathname or URL");
            System.exit(1);
        }
        if (cmd.hasOption("b")) {
            m_ueiBase = cmd.getOptionValue('b');
        }
        if (cmd.hasOption("c")) {
            m_compat = true;
        }
    } catch (ParseException e) {
        printHelp("Failed to parse command line options");
        System.exit(1);
    }
}
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)

Aggregations

ParseException (org.apache.commons.cli.ParseException)587 CommandLine (org.apache.commons.cli.CommandLine)489 CommandLineParser (org.apache.commons.cli.CommandLineParser)381 Options (org.apache.commons.cli.Options)370 DefaultParser (org.apache.commons.cli.DefaultParser)220 HelpFormatter (org.apache.commons.cli.HelpFormatter)205 GnuParser (org.apache.commons.cli.GnuParser)173 IOException (java.io.IOException)124 Option (org.apache.commons.cli.Option)109 File (java.io.File)90 PosixParser (org.apache.commons.cli.PosixParser)65 Path (org.apache.hadoop.fs.Path)50 ArrayList (java.util.ArrayList)42 Properties (java.util.Properties)35 BasicParser (org.apache.commons.cli.BasicParser)32 FileInputStream (java.io.FileInputStream)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)26 List (java.util.List)25 URI (java.net.URI)21