Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:fr.ortolang.diffusion.client.cmd.ReindexAllRootCollectionCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;/* w w  w.  ja v a  2s.c om*/
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }

        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        boolean fakeMode = cmd.hasOption("F");

        OrtolangClient client = OrtolangClient.getInstance();
        if (username.length() > 0) {
            client.getAccountManager().setCredentials(username, password);
            client.login(username);
        }
        System.out.println("Connected as user: " + client.connectedProfile());
        System.out.println("Looking for root collection ...");

        // Looking for root collection
        List<String> rootCollectionKeys = new ArrayList<>();

        int offset = 0;
        int limit = 100;
        JsonObject listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
        JsonArray keys = listOfObjects.getJsonArray("entries");

        while (!keys.isEmpty()) {
            for (JsonString objectKey : keys.getValuesAs(JsonString.class)) {
                JsonObject objectRepresentation = client.getObject(objectKey.getString());
                JsonObject objectProperty = objectRepresentation.getJsonObject("object");
                boolean isRoot = objectProperty.getBoolean("root");
                if (isRoot) {
                    rootCollectionKeys.add(objectKey.getString());
                }
            }
            offset += limit;
            listOfObjects = client.listObjects("core", "collection", "PUBLISHED", offset, limit);
            keys = listOfObjects.getJsonArray("entries");
        }

        System.out.println("Reindex keys : " + rootCollectionKeys);
        if (!fakeMode) {
            for (String key : rootCollectionKeys) {
                client.reindex(key);
            }
        }

        client.logout();
        client.close();

    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:com.booleanworks.peacockmantisshrimp.CommandLineTool.java

private void execute(String[] args) {

    Options options = new Options();

    options.addOption(Option.builder().argName("createDefaultConfig").longOpt("createDefaultConfig").desc(
            "Create a default configuration file at the given location. (--createDefaultConfig <location>)")
            .hasArg(true).type(String.class).required(false).build());

    DefaultParser defaultParser = new DefaultParser();
    try {/*  w  w w.ja  v  a2s  .com*/
        CommandLine commandLine = defaultParser.parse(options, args);

        if (commandLine.hasOption("createDefaultConfig")) {
            this.createDefaultConfig(commandLine.getOptionValue("createDefaultConfig", "config.xml"));
        }

    } catch (ParseException ex) {
        Logger.getLogger(CommandLineTool.class.getName()).log(Level.SEVERE, null, ex);

        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java -jar hibernate-ogm-helper.jar [options]", options);
    }

}

From source file:alluxio.cli.MiniBenchmark.java

/**
 * Parses the input args with a command line format, using
 * {@link org.apache.commons.cli.CommandLineParser}.
 *
 * @param args the input args// w  w w.  j a  v  a 2  s.co  m
 * @return true if parsing succeeded
 */
private static boolean parseInputArgs(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(OPTIONS, args);
    } catch (ParseException e) {
        System.out.println("Failed to parse input args: " + e);
        return false;
    }
    sHelp = cmd.hasOption("help");
    sType = OperationType.valueOf(cmd.getOptionValue("type", "READ"));
    sFileSize = FormatUtils.parseSpaceSize(cmd.getOptionValue("fileSize", "1KB"));
    sIterations = Integer.parseInt(cmd.getOptionValue("iterations", "1"));
    sConcurrency = Integer.parseInt(cmd.getOptionValue("concurrency", "1"));

    return true;
}

From source file:fr.ortolang.diffusion.client.cmd.IndexAllCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;//w w w .j a  v  a 2s. c om
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }

        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        Map<String, String> params = new HashMap<>();

        if (!cmd.hasOption("t")) {
            System.out.println("Types must be defined");
            help();
        }
        String types = cmd.getOptionValue("t");
        String phase = cmd.getOptionValue("p");
        if (types.contains("all")) {
            if (phase == null) {
                System.out.println("When indexing all a phase must be given");
                help();
            } else if (!phase.equals("1") && !phase.equals("2")) {
                System.out.println("Phase number indexing all a phase must be given");
                help();
            }
        }

        params.put("indexingPhase", phase);
        params.put("indexingTypes", types);

        OrtolangClient client = OrtolangClient.getInstance();
        if (username.length() > 0) {
            client.getAccountManager().setCredentials(username, password);
            client.login(username);
        }
        System.out.println("Connected as user: " + client.connectedProfile());
        String pkey = client.createProcess("index-all",
                "Index all " + (types != null ? "(" + types + ")" : "(all)"), params, Collections.emptyMap());
        System.out.println("Index-All process created with key : " + pkey);

        client.logout();
        client.close();

    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:com.github.pjungermann.config.ApplicationCLI.java

public ApplicationCLI(@NotNull final String[] arguments) throws ParseException {
    final CommandLine cmd = new DefaultParser().parse(OPTIONS, arguments);

    profile = cmd.getOptionValue(PROFILE_OPTION.getLongOpt());
    configs = cmd.getOptionValues(CONFIGS_OPTION.getLongOpt());
    specs = cmd.getOptionValues(SPECS_OPTION.getLongOpt());
    recursive = cmd.hasOption(RECURSIVE_OPTION.getLongOpt());
    strict = cmd.hasOption(STRICT_OPTION.getLongOpt());
}

From source file:luceneGazateer.EntryData.java

public static void main(String[] args) throws IOException {
    Option buildOpt = OptionBuilder.withArgName("gazetteer file").hasArg().withLongOpt("build")
            .withDescription("The Path to the Geonames allCountries.txt").create('b');

    Option searchOpt = OptionBuilder.withArgName("set of location names").withLongOpt("search").hasArgs()
            .withDescription("Location names to search the Gazetteer for").create('s');

    Option indexOpt = OptionBuilder.withArgName("directoryPath").withLongOpt("index").hasArgs()
            .withDescription("The path to the Lucene index directory to either create or read").create('i');

    Option helpOpt = OptionBuilder.withLongOpt("help").withDescription("Print this message.").create('h');

    String indexPath = null;/*from w  w w  .jav a  2s. com*/
    String gazetteerPath = null;
    ArrayList<String> geoTerms = null;
    Options options = new Options();
    options.addOption(buildOpt);
    options.addOption(searchOpt);
    options.addOption(indexOpt);
    options.addOption(helpOpt);

    // create the parser
    CommandLineParser parser = new DefaultParser();
    GeoNameResolver resolver = new GeoNameResolver();

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("index")) {
            indexPath = line.getOptionValue("index");
        }

        if (line.hasOption("build")) {
            gazetteerPath = line.getOptionValue("build");
        }

        if (line.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("lucene-geo-gazetteer", options);
            System.exit(1);
        }

        if (indexPath != null && gazetteerPath != null) {
            LOG.info("Building Lucene index at path: [" + indexPath + "] with geoNames.org file: ["
                    + gazetteerPath + "]");
            resolver.buildIndex(gazetteerPath, indexPath);
        }

        if (line.hasOption("search")) {
            String temp_s = "";
            for (String string : line.getOptionValues("search")) {
                temp_s += string;
            }

            produceCandidates(indexPath, temp_s, resolver);
        }

    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());
    }
}

From source file:grakn.core.console.GraknConsole.java

public GraknConsole(String[] args, PrintStream printOut, PrintStream printErr) throws ParseException {
    this.printOut = printOut;
    this.printErr = printErr;

    this.commandLine = new DefaultParser().parse(options, args);
    this.infer = !commandLine.hasOption(NO_INFER);

    String serverAddressArg = commandLine.getOptionValue(URI);
    this.serverAddress = serverAddressArg != null ? serverAddressArg : GraknClient.DEFAULT_URI;

    String keyspaceArg = commandLine.getOptionValue(KEYSPACE);
    this.keyspace = keyspaceArg != null ? keyspaceArg : DEFAULT_KEYSPACE;
}

From source file:fr.ortolang.diffusion.client.cmd.ImportProfilesCommand.java

@Override
public void execute(String[] args) {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;//  w ww  .j a  v a 2  s  . co  m
    Map<String, String> params = new HashMap<>();
    Map<String, File> files = new HashMap<>();
    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("h")) {
            help();
        }
        String[] credentials = getCredentials(cmd);
        String username = credentials[0];
        String password = credentials[1];

        if (cmd.hasOption("f")) {
            if (cmd.hasOption("upload")) {
                files.put("profilespath", new File(cmd.getOptionValue("f")));
            } else {
                params.put("profilespath", cmd.getOptionValue("f"));
            }
        } else {
            help();
        }

        if (cmd.hasOption("overwrite")) {
            params.put("profilesoverwrites", "true");
        }

        OrtolangClient client = OrtolangClient.getInstance();
        if (username.length() > 0) {
            client.getAccountManager().setCredentials(username, password);
            client.login(username);
        }
        System.out.println("Connected as user: " + client.connectedProfile());
        String pkey = client.createProcess("import-profiles", "Import Profiles", params, files);
        System.out.println("Import-Profiles process created with key : " + pkey);

        client.logout();
        client.close();

    } catch (ParseException e) {
        System.out.println("Failed to parse command line properties " + e.getMessage());
        help();
    } catch (OrtolangClientException | OrtolangClientAccountException e) {
        System.out.println("Unexpected error !!");
        e.printStackTrace();
    }
}

From source file:com.somerledsolutions.pa11y.client.Application.java

private boolean shouldPrintHelp(String[] strings) throws ParseException {
    CommandLineParser parser = new DefaultParser();
    Options options = OptionsBuilder.buildHelpOptions();

    CommandLine cl = parser.parse(options, strings, true);
    return cl.hasOption(OptionsBuilder.HELP_OPT);
}

From source file:io.atrato.pubsubserver.PubsubServer.java

void init(String[] args) throws ParseException {
    Options options = new Options();
    options.addOption(CMD_OPTION_LISTEN_ADDRESS, true,
            "Address to listen to. Default is " + DEFAULT_HOST + ":" + DEFAULT_PORT);
    options.addOption(CMD_OPTION_HTDOCS, true, "The local directory where static files should be served.");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    String listenAddress = cmd.getOptionValue(CMD_OPTION_LISTEN_ADDRESS);

    if (listenAddress != null) {
        Pattern pattern = Pattern.compile("(.+:)?(\\d+)");
        Matcher matcher = pattern.matcher(listenAddress);

        if (matcher.find()) {
            String hostString = matcher.group(1);
            if (hostString != null) {
                host = hostString.substring(0, hostString.length() - 1);
            }//from   w  w  w .  j  a  v a2s  . c o m
            port = Integer.valueOf(matcher.group(2));
        } else {
            throw new ParseException("listenAddress must be in this format: [host:]port");
        }
    }

    htdocsDir = cmd.getOptionValue(CMD_OPTION_HTDOCS, null);

}