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

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

Introduction

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

Prototype

PosixParser

Source Link

Usage

From source file:com.bigdata.dastor.tools.SSTableExport.java

/**
 * Given arguments specifying an SSTable, and optionally an output file,
 * export the contents of the SSTable to JSON.
 *  //  w  w  w.  java  2s .c o  m
 * @param args command lines arguments
 * @throws IOException on failure to open/read/write files or output streams
 */
public static void main(String[] args) throws IOException {
    String usage = String.format("Usage: %s <sstable> [-k key [-k key [...]] -x key [-x key [...]]]%n",
            SSTableExport.class.getName());

    CommandLineParser parser = new PosixParser();
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e1) {
        System.err.println(e1.getMessage());
        System.err.println(usage);
        System.exit(1);
    }

    if (cmd.getArgs().length != 1) {
        System.err.println("You must supply exactly one sstable");
        System.err.println(usage);
        System.exit(1);
    }

    String[] keys = cmd.getOptionValues(KEY_OPTION);
    String[] excludes = cmd.getOptionValues(EXCLUDEKEY_OPTION);
    String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();

    if (cmd.hasOption(ENUMERATEKEYS_OPTION))
        enumeratekeys(ssTableFileName, System.out);
    else {
        if ((keys != null) && (keys.length > 0))
            export(ssTableFileName, System.out, keys, excludes);
        else
            export(ssTableFileName, excludes);
    }
    System.exit(0);
}

From source file:edu.usc.corral.cli.CommandLine.java

public static CommandLine parse(List<Option> options, String[] args) throws CommandException {
    CommandLine cmdln = null;/*from   w ww . j  a va 2 s .c  o m*/

    // Parse the command line args
    try {
        Options ops = new Options();
        for (Option option : options) {
            ops.addOption(option.buildOption());
        }
        CommandLineParser parser = new PosixParser();
        cmdln = new CommandLine(parser.parse(ops, args));
    } catch (ParseException pe) {
        throw new CommandException("Invalid argument: " + pe.getMessage());
    }

    // If the user specified an arg file, parse that too
    if (cmdln.hasOption("af")) {
        String argFileName = cmdln.getOptionValue("af");
        String[] fargs = null;
        try {
            List<String> argList = new LinkedList<String>();
            String contents = IOUtil.read(new File(argFileName));
            String[] lines = contents.split("[\r\n]+");
            for (String line : lines) {
                if (!line.startsWith("#")) {
                    ArgumentParser p = new ArgumentParser(line);
                    for (String next = p.nextArgument(); next != null; next = p.nextArgument()) {
                        argList.add(next);
                    }
                }
            }
            fargs = argList.toArray(new String[0]);
        } catch (IOException ioe) {
            throw new CommandException("Unable to read argument file: " + ioe.getMessage(), ioe);
        }

        try {
            Options ops = new Options();
            for (Option option : options) {
                ops.addOption(option.buildOption());
            }
            CommandLineParser parser = new PosixParser();
            cmdln.setFileArgs(parser.parse(ops, fargs));
        } catch (ParseException pe) {
            throw new CommandException("Invalid argument: " + pe.getMessage());
        }
    }

    return cmdln;
}

From source file:com.galenframework.actions.GalenActionDumpArguments.java

public static GalenActionDumpArguments parse(String[] args) {
    args = ArgumentsUtils.processSystemProperties(args);

    Options options = new Options();
    options.addOption("u", "url", true, "Initial test url");
    options.addOption("s", "size", true, "Browser window size");
    options.addOption("W", "max-width", true, "Maximum width of element area image");
    options.addOption("H", "max-height", true, "Maximum height of element area image");
    options.addOption("E", "export", true, "Export path for page dump");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;/* w  w  w  .  ja  va 2s .  co  m*/

    try {
        cmd = parser.parse(options, args);
    } catch (MissingArgumentException e) {
        throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    GalenActionDumpArguments arguments = new GalenActionDumpArguments();
    arguments.setUrl(cmd.getOptionValue("u"));
    arguments.setScreenSize(convertScreenSize(cmd.getOptionValue("s")));
    arguments.setMaxWidth(parseOptionalInt(cmd.getOptionValue("W")));
    arguments.setMaxHeight(parseOptionalInt(cmd.getOptionValue("H")));
    arguments.setExport(cmd.getOptionValue("E"));

    String[] leftovers = cmd.getArgs();
    List<String> paths = new LinkedList<String>();
    if (leftovers.length > 0) {
        for (int i = 0; i < leftovers.length; i++) {
            paths.add(leftovers[i]);
        }
    }
    arguments.setPaths(paths);
    return arguments;
}

From source file:com.enremmeta.otter.spark.ServiceRunner.java

protected void parseCommandLineArgs(String[] argv) throws Exception {
    CommandLineParser parser = new PosixParser();
    try {//from  w  ww.  j a  v a 2 s .c o  m
        cl = parser.parse(opts, argv);
    } catch (ParseException e) {
        usage();
    }
}

From source file:com.galenframework.ide.model.settings.IdeArguments.java

public static IdeArguments parse(String[] args) {
    Options options = new Options();
    options.addOption("h", "help", false, "Print this message");
    options.addOption("p", "port", true, "Port for a server");
    options.addOption("P", "profile", true, "Profile that will be loaded on start");
    options.addOption("s", "storage", true,
            "Path to static file storage that will be used for storing reports");
    options.addOption("k", "keep-results", true, "Amount of last results to be kept");
    options.addOption("c", "cleanup-period", true, "Period in minutes in which it should run task cleanups");
    options.addOption("z", "zombie-results-timeout", true,
            "Minutes for how long it should keep unfinished results");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd;/*  w ww  . j a v a  2  s .  c om*/

    try {
        cmd = parser.parse(options, args);
    } catch (MissingArgumentException e) {
        throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (cmd.hasOption("h")) {
        printHelp(options);
        System.exit(0);
    }

    IdeArguments ideArguments = new IdeArguments();
    ideArguments.setProfile(cmd.getOptionValue("P"));
    ideArguments.setPort(Integer.parseInt(cmd.getOptionValue("p", "4567")));
    ideArguments.setFileStorage(cmd.getOptionValue("s"));
    ideArguments.setKeepLastResults(Integer.parseInt(cmd.getOptionValue("k", "30")));
    ideArguments.setCleanupPeriodInMinutes(Integer.parseInt(cmd.getOptionValue("c", "1")));

    return ideArguments;
}

From source file:ch.cyberduck.cli.DeletePathFinderTest.java

@Test
public void testFindDirectory() throws Exception {
    final CommandLineParser parser = new PosixParser();
    final CommandLine input = parser.parse(TerminalOptionsBuilder.options(),
            new String[] { "--delete", "rackspace://cdn.cyberduck.ch/remote" });

    assertTrue(new DeletePathFinder()
            .find(input, TerminalAction.delete, new Path("/remote", EnumSet.of(Path.Type.directory)))
            .contains(new TransferItem(new Path("/remote", EnumSet.of(Path.Type.directory)))));
}

From source file:net.orpiske.mdm.broker.main.actions.RunAction.java

@Override
protected void processCommand(String[] args) {
    CommandLineParser parser = new PosixParser();

    options = new Options();

    options.addOption("h", "help", false, "prints the help");
    options.addOption(null, "log-level", true,
            "sets log level (should be one of [ 'trace', 'debug', 'verbose'])");

    try {/*  w w  w .  j  av  a 2 s  .  co  m*/
        cmdLine = parser.parse(options, args);
    } catch (ParseException e) {
        help(options, -1);
    }

    isHelp = cmdLine.hasOption("help");
    configureOutput(cmdLine.getOptionValue("log-level"));
}

From source file:com.opengamma.integration.server.copier.CommandLineOption.java

public CommandLineOption(String[] args, Class<?> entryPointClazz) {
    ArgumentChecker.notNull(args, "args");
    ArgumentChecker.notNull(entryPointClazz, "entryPointClazz");

    Options options = getCommandLineOption();

    final CommandLineParser parser = new PosixParser();
    CommandLine line = null;//from   www . j  a va  2 s .  c o m
    try {
        line = parser.parse(options, args);
    } catch (final ParseException e) {
        usage(options, entryPointClazz);
    }
    if (line.hasOption(HELP_OPTION)) {
        usage(options, entryPointClazz);
    } else {
        _configFile = line.getOptionValue(TOOLCONTEXT_CONFIG);
        _serverUrl = line.getOptionValue(SERVER);
    }
}

From source file:net.orpiske.dcd.actions.ParseAction.java

@Override
protected void processCommand(String[] args) {
    CommandLineParser parser = new PosixParser();

    options = new Options();

    options.addOption("h", "help", false, "prints the help");
    options.addOption("f", "file", true, "the file to parse");
    options.addOption(null, "dry-run", false, "does not dispatch the data");
    options.addOption(null, "log-level", true,
            "sets log level (should be one of [ 'trace', 'debug', 'verbose'])");

    try {//  w  ww . j a v a  2  s. c o m
        cmdLine = parser.parse(options, args);
    } catch (ParseException e) {
        help(options, -1);
    }

    isHelp = cmdLine.hasOption("help");

    String fileName = cmdLine.getOptionValue('f');
    if (fileName == null) {
        help(options, -1);
    }
    runner.setFile(fileName);

    boolean isDryRun = cmdLine.hasOption("dry-run");
    if (isDryRun) {
        runner.setDispatcher(new SimpleDispatcher());
    } else {
        runner.setDispatcher(new WebServicesDispatcher());
    }

    configureOutput(cmdLine.getOptionValue("log-level"));
}

From source file:ch.cyberduck.cli.GlobTransferItemFinderTest.java

@Test
public void testNoLocalInOptionsDownload() throws Exception {
    final CommandLineParser parser = new PosixParser();
    final CommandLine input = parser.parse(TerminalOptionsBuilder.options(),
            new String[] { "--download", "rackspace://cdn.cyberduck.ch/remote" });

    final Set<TransferItem> found = new GlobTransferItemFinder().find(input, TerminalAction.download,
            new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)));
    assertFalse(found.isEmpty());/*ww  w. ja v  a 2 s  .c  o m*/
    assertEquals(new TransferItem(new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)),
            LocalFactory.get(System.getProperty("user.dir") + "/remote")), found.iterator().next());

}