Example usage for org.apache.commons.cli CommandLine getOptions

List of usage examples for org.apache.commons.cli CommandLine getOptions

Introduction

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

Prototype

public Option[] getOptions() 

Source Link

Document

Returns an array of the processed Option s.

Usage

From source file:com.comcast.cats.vision.panel.remote.RemoteApplication.java

public void parseCommandLineArgs(String[] args) {

    String arg;/* w  w w .j a v a  2  s .  c  o m*/

    Options options = new Options();

    options.addOption("s", "server", true, "CATS server URL");
    options.addOption("i", "irPath", true, "IR path off settop");
    options.addOption("k", "keyset", true, "key set");

    CommandLineParser parser = new PosixParser();
    try {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Command Line Arguments", options);

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

        for (Option opt : cmd.getOptions()) {

            switch (opt.getLongOpt()) {

            case "server":
                arg = opt.getValue();
                logger.debug("Found mac address: " + arg);
                server = arg;
                break;
            case "irPath":
                arg = opt.getValue();
                logger.debug("Found Server address: " + arg);
                irPath = arg;
                break;
            case "keyset":
                arg = opt.getValue();
                logger.debug("Found login endpoint address: " + arg);
                keySet = arg;
                break;
            default:
                arg = opt.getValue();
                logger.debug("Argument not found..." + arg);
            }

        }

    } catch (ParseException e) {
        logger.error("Command line argument parsing error");
        e.printStackTrace();
    }

}

From source file:com.sangachy.license.LicenseHelperCommand.java

public SingleTask parser(String[] args) {

    options.addOption("h", "help", false, "?");

    options.addOption("u", "upload", false, "License");
    options.addOption("a", "apply", false, "License");

    options.addOption("tp", "temp-path", true, "License ?");
    options.addOption("lp", "lic-path", true, "License ?");

    options.addOption("e", "esn-key", true, "ESN ?");
    options.addOption("ek", "env-key", true, "");

    PosixParser basicParser = new PosixParser();
    CommandLine commandLine;

    try {//from   ww w .ja  v a 2  s . c  o m
        commandLine = basicParser.parse(options, args);

        if (commandLine.getOptions().length <= 0) {
            System.out.println("??0?");
            printHelp();
            return null;
        }

        if (commandLine.getOptions().length > 0) {

            if (commandLine.hasOption("h")) {
                printHelp();
                return null;
            }

            if (!commandLine.hasOption("a") && !commandLine.hasOption("u")) {
                System.out.println("????");
                printHelp();
                return null;
            }

            if (commandLine.hasOption("a")) {
                return getApplyLicenseTask(commandLine);
            }

            if (commandLine.hasOption("u")) {
                return getUploadTask(commandLine);
            }

        }
        return null;
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        printHelp();
        return null;
    }
}

From source file:fastacreator.ParseCLI.java

/**
 * Rutger Ozinga. ParseCLI Parses the commandline input and is able to
 * return the wanted option and value./*w w  w  .j av a2 s . c  o m*/
 *
 * @param args are commandline arguments.
 * @throws org.apache.commons.cli.ParseException an exception
 */
public ParseCLI(final String[] args) throws org.apache.commons.cli.ParseException {
    HelpFormatter helpForm = new HelpFormatter();
    Options cliOpt = new Options();
    cliOpt.addOption("h", "help", false, "Displays help");
    cliOpt.addOption("p", true, "Expects a path to a protein fasta file.");
    cliOpt.addOption("n", true, "Expects a path to place the new tab separated transcript file at.");
    cliOpt.addOption("i", true, "The index of the sequence");
    if (args.length == 0) {
        helpForm.printHelp("Please enter all the " + "options below. ", cliOpt);
        System.exit(0);
    } else {
        BasicParser parser = new BasicParser();
        CommandLine cliParser = parser.parse(cliOpt, args);
        if (cliParser.getOptions().length < 2) {
            System.out.println(
                    "Error : " + "Please enter all options in" + " order for this program to work" + "!\n");
            helpForm.printHelp("Please enter all of the  " + "option ", cliOpt);
            System.exit(0);
        } else {
            if (cliParser.hasOption("h") && cliParser.hasOption("help")) {
                helpForm.printHelp("Command Line Help:\n", cliOpt);
                System.exit(0);
            } else {
                String snpFileString = cliParser.getOptionValue("p");
                Path snpPath = Paths.get(snpFileString);
                if (Files.exists(snpPath)) {
                    setCDSPath(snpPath);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -p followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
                String newFileString = cliParser.getOptionValue("n");
                Matcher match2 = re.matcher(newFileString);
                String editedTranscriptFileString = match2.replaceAll("");
                Path newFilePath = Paths.get(editedTranscriptFileString);
                if (Files.exists(newFilePath)) {
                    setNewFilePath(newFileString);
                } else {
                    System.out.println("The entered Path does" + " not exits");
                    helpForm.printHelp("Please enter -nt followed by a valid" + " path ", cliOpt);
                    System.exit(0);
                }
                String indexString = cliParser.getOptionValue("i");
                int index = Integer.parseInt(indexString);
                setIndex(index);
            }
        }
    }
}

From source file:alluxio.shell.command.TestCommand.java

@Override
public int run(CommandLine cl) throws AlluxioException, IOException {
    if (cl.getOptions().length > 1) {
        return 1;
    }//from w w w  .ja  va2s  . c o m
    String[] args = cl.getArgs();
    AlluxioURI path = new AlluxioURI(args[0]);
    try {
        URIStatus status = mFileSystem.getStatus(path);
        boolean testResult = false;
        if (cl.hasOption("d")) {
            if (isDir(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("f")) {
            if (isFile(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("e")) {
            testResult = true;
        } else if (cl.hasOption("s")) {
            if (isNonEmptyDir(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("z")) {
            if (isZeroLengthFile(status)) {
                testResult = true;
            }
        }
        return testResult ? 0 : 1;
    } catch (AlluxioException | IOException e) {
        return 1;
    }
}

From source file:alluxio.cli.fs.command.TestCommand.java

@Override
public int run(CommandLine cl) throws AlluxioException, IOException {
    if (cl.getOptions().length > 1) {
        return -1;
    }//from   w  w  w.j av a 2 s .  c  o m
    String[] args = cl.getArgs();
    AlluxioURI path = new AlluxioURI(args[0]);
    try {
        URIStatus status = mFileSystem.getStatus(path);
        boolean testResult = false;
        if (cl.hasOption("d")) {
            if (isDir(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("f")) {
            if (isFile(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("e")) {
            testResult = true;
        } else if (cl.hasOption("s")) {
            if (isNonEmptyDir(status)) {
                testResult = true;
            }
        } else if (cl.hasOption("z")) {
            if (isZeroLengthFile(status)) {
                testResult = true;
            }
        } else {
            return -1;
        }
        return testResult ? 0 : 1;
    } catch (AlluxioException | IOException e) {
        return 1;
    }
}

From source file:info.fetter.rrdclient.GraphCommand.java

/**
 * Create a wrapper object for the RRD graph command.
 * /*  www  .  j a va2  s .com*/
 * @param args
 */
public GraphCommand(String... args) {
    try {
        this.args = args;
        CommandLineParser parser = new PosixParser();
        Options options = new Options();
        options.addOption("a", "imgformat", true, "image format");
        options.addOption("s", "start", true, "start (default end-1day)");
        options.addOption("e", "end", true, "end (default now)");
        options.addOption("t", "title", true, "graph title");
        options.addOption("n", "font", true, "font");
        options.addOption("w", "width", true, "image width");
        options.addOption("h", "height", true, "image height");
        options.addOption("b", "base", true, "base value");
        options.addOption("A", "alt-autoscale", false, "alternate scale algorithm");
        options.addOption("M", "alt-autoscale-max", false, "alternate scale algorithm (max value only)");
        options.addOption("J", "alt-autoscale-min", false, "alternate scale algorithm (min value only)");
        options.addOption("l", "lower-limit", true, "lower limit");
        options.addOption("u", "upper-limit", true, "upper limit");
        options.addOption("r", "rigid", false, "rigid boundaries");
        options.addOption("v", "vertical-label", true, "vertical label");
        options.addOption("E", "slope-mode", false, "enable slope mode");
        options.addOption("i", "interlaced", false, "interlaced image");
        options.addOption("o", "logarithmic", false, "logarithmic scale");
        options.addOption("z", "units", true, "use SI notation for logarithmic scale");
        options.addOption("y", "y-grid", true, "Y grid step");
        options.addOption("X", "units-exponent", true, "sets the 10**exponent scaling of the y-axis values");

        CommandLine cmd = parser.parse(options, args, false);
        for (Option option : cmd.getOptions()) {
            logger.trace("Parsed option : " + option.getLongOpt());
        }
        for (String arg : cmd.getArgs()) {
            logger.trace("Remaining arg : " + arg);
        }

    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:gobblin.runtime.cli.PublicMethodsGobblinCliFactory.java

/**
 * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains
 * that option, this method will automatically call the method on the input {@link EmbeddedGobblin} with the correct
 * arguments./*from ww w  .j a  v  a  2s  . c om*/
 */
public void applyCommandLineOptions(CommandLine cli, EmbeddedGobblin embeddedGobblin) {
    try {
        for (Option option : cli.getOptions()) {
            if (!this.methodsMap.containsKey(option.getOpt())) {
                // Option added by cli driver itself.
                continue;
            }
            if (option.hasArg()) {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue());
            } else {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin);
            }
        }
    } catch (IllegalAccessException | InvocationTargetException exc) {
        throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc);
    }
}

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

public boolean validate(final CommandLine input) {
    for (Option o : input.getOptions()) {
        if (Option.UNINITIALIZED == o.getArgs()) {
            continue;
        }//from  ww w  .  ja v a  2  s.c  om
        if (o.hasOptionalArg()) {
            continue;
        }
        if (o.getArgs() != o.getValuesList().size()) {
            console.printf("Missing argument for option %s%n", o.getLongOpt());
            return false;
        }
    }
    final TerminalAction action = TerminalActionFinder.get(input);
    if (null == action) {
        console.printf("%s%n", "Missing argument");
        return false;
    }
    if (input.hasOption(TerminalOptionsBuilder.Params.existing.name())) {
        final String arg = input.getOptionValue(TerminalOptionsBuilder.Params.existing.name());
        if (null == TransferAction.forName(arg)) {
            final Set<TransferAction> actions = new HashSet<TransferAction>(
                    TransferAction.forTransfer(Transfer.Type.download));
            actions.add(TransferAction.cancel);
            console.printf("Invalid argument '%s' for option %s. Must be one of %s%n", arg,
                    TerminalOptionsBuilder.Params.existing.name(), Arrays.toString(actions.toArray()));
            return false;
        }
        switch (action) {
        case download:
            if (!validate(arg, Transfer.Type.download)) {
                return false;
            }
            break;
        case upload:
            if (!validate(arg, Transfer.Type.upload)) {
                return false;
            }
            break;
        case synchronize:
            if (!validate(arg, Transfer.Type.sync)) {
                return false;
            }
            break;
        case copy:
            if (!validate(arg, Transfer.Type.copy)) {
                return false;
            }
            break;
        }
    }
    // Validate arguments
    switch (action) {
    case list:
    case download:
        if (!validate(input.getOptionValue(action.name()))) {
            return false;
        }
        break;
    case upload:
    case copy:
    case synchronize:
        if (!validate(input.getOptionValue(action.name()))) {
            return false;
        }
        break;
    }
    return true;
}

From source file:com.lcdfx.pipoint.PiPoint.java

public PiPoint(String[] args) {

    this.addWindowListener(new WindowAdapter() {
        @Override/* w w  w  .  j  av  a  2  s.c  om*/
        public void windowClosing(WindowEvent ev) {
            shutDown();
        }
    });

    // add logging
    logger = Logger.getLogger(this.getClass().getName());
    logger.log(Level.INFO,
            "PiPoint version " + PiPoint.class.getPackage().getImplementationVersion() + " running under "
                    + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version"));

    // get command line options
    CommandLineParser parser = new BasicParser();
    Map<String, String> cmdOptions = new HashMap<String, String>();
    Options options = new Options();
    options.addOption(new Option("f", "fullscreen", false, "fullscreen mode (no cursor)"));
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
        for (Option option : cmd.getOptions()) {
            cmdOptions.put(option.getOpt(), option.getValue());
        }
    } catch (ParseException e) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar pipoint.jar", options);
        System.exit(0);
    }

    if (cmd.hasOption("f")) {
        setUndecorated(true);
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0),
                "blank cursor");
        getContentPane().setCursor(blankCursor);
    }

    // instantiate the RendererManager
    mgr = new DlnaRendererManager(this);
    mgr.refreshDevices();

    nowPlayingPanel = new NowPlayingPanel(this);
    mgr.getRenderer().addListener(nowPlayingPanel);
    devicePanel = new DevicePanel(this);

    this.getContentPane().setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
    this.getContentPane().add(devicePanel);
}

From source file:gobblin.runtime.cli.ConstructorAndPublicMethodsCliObjectFactory.java

/**
 * Builds an instance of T using the selected constructor getting the constructor
 * parameters from the {@link CommandLine}.
 *
 * Note: this method will also automatically call {@link #applyCommandLineOptions(CommandLine, T)} on
 * the constructed object.//  w  ww .  j ava 2 s. co m
 */
private T buildInstance(CommandLine cli) {
    String[] constructorArgs = new String[this.constructor.getParameterTypes().length];
    for (Option option : cli.getOptions()) {
        if (this.constructoArgumentsMap.containsKey(option.getOpt())) {
            int idx = this.constructoArgumentsMap.get(option.getOpt());
            constructorArgs[idx] = option.getValue();
        }
    }

    T embeddedGobblin;
    try {
        embeddedGobblin = this.constructor.newInstance((Object[]) constructorArgs);
        return embeddedGobblin;
    } catch (IllegalAccessException | InvocationTargetException | InstantiationException exc) {
        throw new RuntimeException("Could not instantiate " + this.klazz.getName(), exc);
    }
}