Search in sources :

Example 56 with CommandLine

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

the class ApplicationMaster method main.

/**
   * @param args Command line arguments to launch application master
   */
public static void main(String[] args) {
    Options options = new Options();
    options.addOption("num_workers", true, "Number of Alluxio workers to launch. Default 1");
    options.addOption("master_address", true, "(Required) Address to run Alluxio master");
    options.addOption("resource_path", true, "(Required) HDFS path containing the Application Master");
    try {
        LOG.info("Starting Application Master with args {}", Arrays.toString(args));
        final CommandLine cliParser = new GnuParser().parse(options, args);
        YarnConfiguration conf = new YarnConfiguration();
        UserGroupInformation.setConfiguration(conf);
        if (UserGroupInformation.isSecurityEnabled()) {
            String user = System.getenv("ALLUXIO_USER");
            UserGroupInformation ugi = UserGroupInformation.createRemoteUser(user);
            for (Token token : UserGroupInformation.getCurrentUser().getTokens()) {
                ugi.addToken(token);
            }
            LOG.info("UserGroupInformation: " + ugi);
            ugi.doAs(new PrivilegedExceptionAction<Void>() {

                @Override
                public Void run() throws Exception {
                    runApplicationMaster(cliParser);
                    return null;
                }
            });
        } else {
            runApplicationMaster(cliParser);
        }
    } catch (Exception e) {
        LOG.error("Error running Application Master", e);
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) GnuParser(org.apache.commons.cli.GnuParser) Token(org.apache.hadoop.security.token.Token) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 57 with CommandLine

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

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

use of org.apache.commons.cli.CommandLine in project GNS by MobilityFirst.

the class BatchCreateTest method main.

/**
   * The main routine run from the command line.
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    CommandLine parser = initializeOptions(args);
    if (parser.hasOption("help")) {
        printUsage();
        System.exit(1);
    }
    String alias = parser.getOptionValue("alias");
    int guidCnt = Integer.parseInt(parser.getOptionValue("guidCnt", "10000"));
    int writeTo = Integer.parseInt(parser.getOptionValue("writeTo", "0"));
    new BatchCreateTest(alias != null ? alias : DEFAULT_ACCOUNT_ALIAS, guidCnt, writeTo);
    System.exit(0);
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) RandomString(edu.umass.cs.gnscommon.utils.RandomString)

Example 60 with CommandLine

use of org.apache.commons.cli.CommandLine 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)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33