Search in sources :

Example 86 with PosixParser

use of org.apache.commons.cli.PosixParser in project apex-core by apache.

the class ApexCli method getShowLogicalPlanCommandLineInfo.

private static ShowLogicalPlanCommandLineInfo getShowLogicalPlanCommandLineInfo(String[] args) throws ParseException {
    CommandLineParser parser = new PosixParser();
    ShowLogicalPlanCommandLineInfo result = new ShowLogicalPlanCommandLineInfo();
    CommandLine line = parser.parse(getShowLogicalPlanCommandLineOptions(), args);
    result.libjars = line.getOptionValue("libjars");
    result.ignorePom = line.hasOption("ignorepom");
    result.args = line.getArgs();
    result.exactMatch = line.hasOption("exactMatch");
    return result;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 87 with PosixParser

use of org.apache.commons.cli.PosixParser in project apex-core by apache.

the class ApexCli method getLaunchCommandLineInfo.

static LaunchCommandLineInfo getLaunchCommandLineInfo(String[] args) throws ParseException {
    CommandLineParser parser = new PosixParser();
    LaunchCommandLineInfo result = new LaunchCommandLineInfo();
    CommandLine line = parser.parse(LAUNCH_OPTIONS.options, args);
    result.localMode = line.hasOption(LAUNCH_OPTIONS.local.getOpt());
    result.configFile = line.getOptionValue(LAUNCH_OPTIONS.configFile.getOpt());
    result.apConfigFile = line.getOptionValue(LAUNCH_OPTIONS.apConfigFile.getOpt());
    result.ignorePom = line.hasOption(LAUNCH_OPTIONS.ignorePom.getOpt());
    String[] defs = line.getOptionValues(LAUNCH_OPTIONS.defProperty.getOpt());
    if (defs != null) {
        result.overrideProperties = new HashMap<>();
        for (String def : defs) {
            int equal = def.indexOf('=');
            if (equal < 0) {
                result.overrideProperties.put(def, null);
            } else {
                result.overrideProperties.put(def.substring(0, equal), def.substring(equal + 1));
            }
        }
    }
    result.libjars = line.getOptionValue(LAUNCH_OPTIONS.libjars.getOpt());
    result.archives = line.getOptionValue(LAUNCH_OPTIONS.archives.getOpt());
    result.files = line.getOptionValue(LAUNCH_OPTIONS.files.getOpt());
    result.queue = line.getOptionValue(LAUNCH_OPTIONS.queue.getOpt());
    result.tags = line.getOptionValue(LAUNCH_OPTIONS.tags.getOpt());
    result.args = line.getArgs();
    result.origAppId = line.getOptionValue(LAUNCH_OPTIONS.originalAppID.getOpt());
    result.exactMatch = line.hasOption("exactMatch");
    result.force = line.hasOption("force");
    result.useConfigApps = line.getOptionValue(LAUNCH_OPTIONS.useConfigApps.getOpt());
    return result;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 88 with PosixParser

use of org.apache.commons.cli.PosixParser in project phoenix by apache.

the class AbstractBulkLoadTool method parseOptions.

/**
     * Parses the commandline arguments, throws IllegalStateException if mandatory arguments are
     * missing.
     *
     * @param args supplied command line arguments
     * @return the parsed command line
     */
protected CommandLine parseOptions(String[] args) {
    Options options = getOptions();
    CommandLineParser parser = new PosixParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = parser.parse(options, args);
    } catch (ParseException e) {
        printHelpAndExit("Error parsing command line options: " + e.getMessage(), options);
    }
    if (cmdLine.hasOption(HELP_OPT.getOpt())) {
        printHelpAndExit(options, 0);
    }
    if (!cmdLine.hasOption(TABLE_NAME_OPT.getOpt())) {
        throw new IllegalStateException(TABLE_NAME_OPT.getLongOpt() + " is a mandatory " + "parameter");
    }
    if (!cmdLine.getArgList().isEmpty()) {
        throw new IllegalStateException("Got unexpected extra parameters: " + cmdLine.getArgList());
    }
    if (!cmdLine.hasOption(INPUT_PATH_OPT.getOpt())) {
        throw new IllegalStateException(INPUT_PATH_OPT.getLongOpt() + " is a mandatory " + "parameter");
    }
    return cmdLine;
}
Also used : 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 89 with PosixParser

use of org.apache.commons.cli.PosixParser in project lucene-solr by apache.

the class ZkCLI method main.

/**
   * Allows you to perform a variety of zookeeper related tasks, such as:
   * 
   * Bootstrap the current configs for all collections in solr.xml.
   * 
   * Upload a named config set from a given directory.
   * 
   * Link a named config set explicity to a collection.
   * 
   * Clear ZooKeeper info.
   * 
   * If you also pass a solrPort, it will be used to start an embedded zk useful
   * for single machine, multi node tests.
   */
public static void main(String[] args) throws InterruptedException, TimeoutException, IOException, ParserConfigurationException, SAXException, KeeperException {
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption(OptionBuilder.hasArg(true).withDescription("cmd to run: " + BOOTSTRAP + ", " + UPCONFIG + ", " + DOWNCONFIG + ", " + LINKCONFIG + ", " + MAKEPATH + ", " + PUT + ", " + PUT_FILE + "," + GET + "," + GET_FILE + ", " + LIST + ", " + CLEAR + ", " + UPDATEACLS + ", " + LS).create(CMD));
    Option zkHostOption = new Option("z", ZKHOST, true, "ZooKeeper host address");
    options.addOption(zkHostOption);
    Option solrHomeOption = new Option("s", SOLRHOME, true, "for " + BOOTSTRAP + ", " + RUNZK + ": solrhome location");
    options.addOption(solrHomeOption);
    options.addOption("d", CONFDIR, true, "for " + UPCONFIG + ": a directory of configuration files");
    options.addOption("n", CONFNAME, true, "for " + UPCONFIG + ", " + LINKCONFIG + ": name of the config set");
    options.addOption("c", COLLECTION, true, "for " + LINKCONFIG + ": name of the collection");
    options.addOption(EXCLUDE_REGEX_SHORT, EXCLUDE_REGEX, true, "for " + UPCONFIG + ": files matching this regular expression won't be uploaded");
    options.addOption("r", RUNZK, true, "run zk internally by passing the solr run port - only for clusters on one machine (tests, dev)");
    options.addOption("h", HELP, false, "bring up this help page");
    options.addOption(NAME, true, "name of the cluster property to set");
    options.addOption(VALUE_LONG, true, "value of the cluster to set");
    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        if (line.hasOption(HELP) || !line.hasOption(ZKHOST) || !line.hasOption(CMD)) {
            // automatically generate the help statement
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ZK_CLI_NAME, options);
            stdout.println("Examples:");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION + " collection1" + " -" + CONFNAME + " myconf");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE + " /solr.xml /User/myuser/solr/solr.xml");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST);
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LS + " /solr/live_nodes");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME + " urlScheme -" + VALUE_LONG + " https");
            stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr");
            return;
        }
        // start up a tmp zk server first
        String zkServerAddress = line.getOptionValue(ZKHOST);
        String solrHome = line.getOptionValue(SOLRHOME);
        String solrPort = null;
        if (line.hasOption(RUNZK)) {
            if (!line.hasOption(SOLRHOME)) {
                stdout.println("-" + SOLRHOME + " is required for " + RUNZK);
                System.exit(1);
            }
            solrPort = line.getOptionValue(RUNZK);
        }
        SolrZkServer zkServer = null;
        if (solrPort != null) {
            zkServer = new SolrZkServer("true", null, solrHome + "/zoo_data", solrHome, Integer.parseInt(solrPort));
            zkServer.parseConfig();
            zkServer.start();
        }
        SolrZkClient zkClient = null;
        try {
            zkClient = new SolrZkClient(zkServerAddress, 30000, 30000, () -> {
            });
            if (line.getOptionValue(CMD).equalsIgnoreCase(BOOTSTRAP)) {
                if (!line.hasOption(SOLRHOME)) {
                    stdout.println("-" + SOLRHOME + " is required for " + BOOTSTRAP);
                    System.exit(1);
                }
                CoreContainer cc = new CoreContainer(solrHome);
                if (!ZkController.checkChrootPath(zkServerAddress, true)) {
                    stdout.println("A chroot was specified in zkHost but the znode doesn't exist. ");
                    System.exit(1);
                }
                ZkController.bootstrapConf(zkClient, cc, solrHome);
            // No need to close the CoreContainer, as it wasn't started
            // up in the first place...
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(UPCONFIG)) {
                if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {
                    stdout.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + UPCONFIG);
                    System.exit(1);
                }
                String confDir = line.getOptionValue(CONFDIR);
                String confName = line.getOptionValue(CONFNAME);
                final String excludeExpr = line.getOptionValue(EXCLUDE_REGEX, EXCLUDE_REGEX_DEFAULT);
                if (!ZkController.checkChrootPath(zkServerAddress, true)) {
                    stdout.println("A chroot was specified in zkHost but the znode doesn't exist. ");
                    System.exit(1);
                }
                ZkConfigManager configManager = new ZkConfigManager(zkClient);
                final Pattern excludePattern = Pattern.compile(excludeExpr);
                configManager.uploadConfigDir(Paths.get(confDir), confName, excludePattern);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(DOWNCONFIG)) {
                if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {
                    stdout.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + DOWNCONFIG);
                    System.exit(1);
                }
                String confDir = line.getOptionValue(CONFDIR);
                String confName = line.getOptionValue(CONFNAME);
                ZkConfigManager configManager = new ZkConfigManager(zkClient);
                configManager.downloadConfigDir(confName, Paths.get(confDir));
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(LINKCONFIG)) {
                if (!line.hasOption(COLLECTION) || !line.hasOption(CONFNAME)) {
                    stdout.println("-" + COLLECTION + " and -" + CONFNAME + " are required for " + LINKCONFIG);
                    System.exit(1);
                }
                String collection = line.getOptionValue(COLLECTION);
                String confName = line.getOptionValue(CONFNAME);
                ZkController.linkConfSet(zkClient, collection, confName);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(LIST)) {
                zkClient.printLayoutToStream(stdout);
            } else if (line.getOptionValue(CMD).equals(LS)) {
                List argList = line.getArgList();
                if (argList.size() != 1) {
                    stdout.println("-" + LS + " requires one arg - the path to list");
                    System.exit(1);
                }
                StringBuilder sb = new StringBuilder();
                String path = argList.get(0).toString();
                zkClient.printLayout(path == null ? "/" : path, 0, sb);
                stdout.println(sb.toString());
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLEAR)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    stdout.println("-" + CLEAR + " requires one arg - the path to clear");
                    System.exit(1);
                }
                zkClient.clean(arglist.get(0).toString());
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(MAKEPATH)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    stdout.println("-" + MAKEPATH + " requires one arg - the path to make");
                    System.exit(1);
                }
                zkClient.makePath(arglist.get(0).toString(), true);
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    stdout.println("-" + PUT + " requires two args - the path to create and the data string");
                    System.exit(1);
                }
                String path = arglist.get(0).toString();
                if (zkClient.exists(path, true)) {
                    zkClient.setData(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), true);
                } else {
                    zkClient.create(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), CreateMode.PERSISTENT, true);
                }
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT_FILE)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    stdout.println("-" + PUT_FILE + " requires two args - the path to create in ZK and the path to the local file");
                    System.exit(1);
                }
                String path = arglist.get(0).toString();
                InputStream is = new FileInputStream(arglist.get(1).toString());
                try {
                    if (zkClient.exists(path, true)) {
                        zkClient.setData(path, IOUtils.toByteArray(is), true);
                    } else {
                        zkClient.create(path, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true);
                    }
                } finally {
                    IOUtils.closeQuietly(is);
                }
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    stdout.println("-" + GET + " requires one arg - the path to get");
                    System.exit(1);
                }
                byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true);
                stdout.println(new String(data, StandardCharsets.UTF_8));
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET_FILE)) {
                List arglist = line.getArgList();
                if (arglist.size() != 2) {
                    stdout.println("-" + GET_FILE + "requires two args - the path to get and the file to save it to");
                    System.exit(1);
                }
                byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true);
                FileUtils.writeByteArrayToFile(new File(arglist.get(1).toString()), data);
            } else if (line.getOptionValue(CMD).equals(UPDATEACLS)) {
                List arglist = line.getArgList();
                if (arglist.size() != 1) {
                    stdout.println("-" + UPDATEACLS + " requires one arg - the path to update");
                    System.exit(1);
                }
                zkClient.updateACLs(arglist.get(0).toString());
            } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLUSTERPROP)) {
                if (!line.hasOption(NAME)) {
                    stdout.println("-" + NAME + " is required for " + CLUSTERPROP);
                }
                String propertyName = line.getOptionValue(NAME);
                //If -val option is missing, we will use the null value. This is required to maintain
                //compatibility with Collections API.
                String propertyValue = line.getOptionValue(VALUE_LONG);
                ClusterProperties props = new ClusterProperties(zkClient);
                try {
                    props.setClusterProperty(propertyName, propertyValue);
                } catch (IOException ex) {
                    stdout.println("Unable to set the cluster property due to following error : " + ex.getLocalizedMessage());
                    System.exit(1);
                }
            } else {
                // If not cmd matches
                stdout.println("Unknown command " + line.getOptionValue(CMD) + ". Use -h to get help.");
                System.exit(1);
            }
        } finally {
            if (solrPort != null) {
                zkServer.stop();
            }
            if (zkClient != null) {
                zkClient.close();
            }
        }
    } catch (ParseException exp) {
        stdout.println("Unexpected exception:" + exp.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) Pattern(java.util.regex.Pattern) ZkConfigManager(org.apache.solr.common.cloud.ZkConfigManager) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) FileInputStream(java.io.FileInputStream) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CoreContainer(org.apache.solr.core.CoreContainer) ClusterProperties(org.apache.solr.common.cloud.ClusterProperties) Option(org.apache.commons.cli.Option) List(java.util.List) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 90 with PosixParser

use of org.apache.commons.cli.PosixParser in project compiler by boalang.

the class BoaEvaluator method main.

public static void main(final String[] args) {
    final Options options = new Options();
    options.addOption("i", "input", true, "input Boa source file (*.boa)");
    options.addOption("d", "data", true, "path to local data directory");
    options.addOption("o", "output", true, "output directory");
    options.getOption("i").setRequired(true);
    options.getOption("d").setRequired(true);
    try {
        if (args.length == 0) {
            printHelp(options, null);
            return;
        } else {
            final CommandLine cl = new PosixParser().parse(options, args);
            if (cl.hasOption('i') && cl.hasOption('d')) {
                final BoaEvaluator evaluator;
                try {
                    if (cl.hasOption('o')) {
                        evaluator = new BoaEvaluator(cl.getOptionValue('i'), cl.getOptionValue('d'), cl.getOptionValue('o'));
                    } else {
                        evaluator = new BoaEvaluator(cl.getOptionValue('i'), cl.getOptionValue('d'));
                    }
                } catch (final IOException e) {
                    System.err.print(e);
                    return;
                }
                if (!evaluator.compile()) {
                    System.err.println("Compilation Failed");
                    return;
                }
                final long start = System.currentTimeMillis();
                evaluator.evaluate();
                final long end = System.currentTimeMillis();
                System.out.println("Total Time Taken: " + (end - start));
                System.out.println(evaluator.getResults());
            } else {
                printHelp(options, "missing required options: -i <arg> and -d <arg>");
                return;
            }
        }
    } catch (final org.apache.commons.cli.ParseException e) {
        printHelp(options, e.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException)

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)103 CommandLine (org.apache.commons.cli.CommandLine)97 CommandLineParser (org.apache.commons.cli.CommandLineParser)67 Options (org.apache.commons.cli.Options)56 ParseException (org.apache.commons.cli.ParseException)51 File (java.io.File)24 IOException (java.io.IOException)20 HelpFormatter (org.apache.commons.cli.HelpFormatter)16 Optional (java.util.Optional)10 Test (org.junit.Test)9 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 GenericOptionsParser (org.apache.hadoop.util.GenericOptionsParser)7 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Option (org.apache.commons.cli.Option)6 Parser (org.apache.commons.cli.Parser)6 Properties (java.util.Properties)5 Path (org.apache.hadoop.fs.Path)5 ArrayList (java.util.ArrayList)4 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)4