Example usage for org.apache.commons.cli Option hasArg

List of usage examples for org.apache.commons.cli Option hasArg

Introduction

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

Prototype

public boolean hasArg() 

Source Link

Document

Query to see if this Option requires an argument

Usage

From source file:org.pz.platypus.commandline.ClParser.java

/**
 * Return all possible options as a sorted TreeMap of option, description
 *///from   w ww.j  av  a  2 s. c  o m
public TreeMap<String, String> getAllOptions() {
    ClOptions clo = new ClOptions();
    TreeMap<String, String> optionsWithDesc = new TreeMap<String, String>();
    for (Option o : clo.getOptions()) {
        optionsWithDesc.put("-" + o.getOpt() + (o.hasArg() ? (" [" + o.getArgName() + "]") : ""),
                o.getDescription());
    }
    return (optionsWithDesc);
}

From source file:org.stathissideris.ascii2image.core.CommandLineConverter.java

private static void printRunInfo(CommandLine cmdLine) {
    System.out.println("\n" + notice + "\n");

    System.out.println("Running with options:");
    Option[] opts = cmdLine.getOptions();
    for (Option option : opts) {
        if (option.hasArgs()) {
            for (String value : option.getValues()) {
                System.out.println(option.getLongOpt() + " = " + value);
            }//  www . ja v  a2  s  . c  o  m
        } else if (option.hasArg()) {
            System.out.println(option.getLongOpt() + " = " + option.getValue());
        } else {
            System.out.println(option.getLongOpt());
        }
    }
}

From source file:org.vetmeduni.tools.AbstractTool.java

/**
 * Get the arguments with the string//from  w  w w.  j a v a 2  s.  c  o  m
 *
 * @param cmd the already parsed command line with the programParser
 *
 * @return the string with the arguments in the command line properly formatted
 */
protected String getCmdLineString(CommandLine cmd) {
    StringBuilder builder = new StringBuilder("");
    for (Option opt : cmd.getOptions()) {
        if (opt.hasArg()) {
            for (String val : opt.getValues()) {
                builder.append("--");
                builder.append(opt.getLongOpt());
                builder.append(" ");
                builder.append(val);
                builder.append(" ");
            }
        } else {
            builder.append("--");
            builder.append(opt.getLongOpt());
            builder.append(" ");
        }
    }
    return builder.toString();
}

From source file:org.voltdb.CLIConfig.java

public void parse(String cmdName, String[] args) {
    this.cmdName = cmdName;

    try {/*from   w  ww. j av  a 2s.  c  o m*/
        options.addOption("help", "h", false, "Print this message");
        // add all of the declared options to the cli
        for (Field field : getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(Option.class)) {
                Option option = field.getAnnotation(Option.class);

                String opt = option.opt();
                if ((opt == null) || (opt.trim().length() == 0)) {
                    opt = field.getName();
                }
                String shortopt = option.shortOpt();
                if ((shortopt == null) || (shortopt.trim().length() == 0)) {
                    options.addOption(null, opt, option.hasArg(), option.desc());
                    helpmsgs.addOption(null, opt, option.hasArg(), option.desc());
                } else {
                    options.addOption(shortopt, opt, option.hasArg(), option.desc());
                    helpmsgs.addOption(shortopt, opt, option.hasArg(), option.desc());
                }
            } else if (field.isAnnotationPresent(AdditionalArgs.class)) {
                AdditionalArgs params = field.getAnnotation(AdditionalArgs.class);
                String opt = params.opt();
                if ((opt == null) || (opt.trim().length() == 0)) {
                    opt = field.getName();
                }
                options.addOption(opt, params.hasArg(), params.desc());
            }
        }

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

        if (cmd.hasOption("help")) {
            printUsage();
            System.exit(0);
        }
        String[] leftargs = cmd.getArgs();
        int leftover = 0;
        // string key-value pairs
        Map<String, String> kvMap = new TreeMap<String, String>();

        for (Field field : getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(Option.class)) {
                Option option = field.getAnnotation(Option.class);
                String opt = option.opt();
                if ((opt == null) || (opt.trim().length() == 0)) {
                    opt = field.getName();
                }

                if (cmd.hasOption(opt)) {
                    if (option.hasArg()) {
                        assignValueToField(field, cmd.getOptionValue(opt));
                    } else {
                        if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
                            field.setAccessible(true);
                            try {
                                field.set(this, true);
                            } catch (Exception e) {
                                throw new IllegalArgumentException(e);
                            }
                        } else {
                            printUsage();
                        }
                    }
                } else {
                    if (option.required()) {
                        printUsage();
                    }
                }

                field.setAccessible(true);
                kvMap.put(opt, field.get(this).toString());
            } else if (field.isAnnotationPresent(AdditionalArgs.class)) {
                // Deal with --table=BLHA, offer nice error message later
                leftover++;
            }
        }
        if (leftargs != null) {
            if (leftargs.length <= leftover) {
                Field[] fields = getClass().getDeclaredFields();
                for (int i = 0, j = 0; i < leftargs.length; i++) {
                    for (; j < fields.length; j++) {
                        if (fields[j].isAnnotationPresent(AdditionalArgs.class))
                            break;
                    }
                    fields[j].setAccessible(true);
                    fields[j].set(this, leftargs[i]);
                }
            } else {
                System.err.println("Expected " + leftover + " args, but receive " + leftargs.length + " args");
                printUsage();
                System.exit(-1);
            }
        }

        // check that the values read are valid
        // this code is specific to your app
        validate();

        // build a debug string
        StringBuilder sb = new StringBuilder();
        for (Entry<String, String> e : kvMap.entrySet()) {
            sb.append(e.getKey()).append(" = ").append(e.getValue()).append("\n");
        }
        configDump = sb.toString();
    }

    catch (Exception e) {
        System.err.println("Parsing failed. Reason: " + e.getMessage());
        printUsage();
        System.exit(-1);
    }
}

From source file:ro.cs.products.Executor.java

private static void printCommandLine(CommandLine cmd) {
    Logger.getRootLogger().debug("Executing with the following arguments:");
    for (Option option : cmd.getOptions()) {
        if (option.hasArgs()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + String.join(" ", option.getValues()));
        } else if (option.hasArg()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + option.getValue());
        } else {//www  .  j  a v a2s  .  c o m
            Logger.getRootLogger().debug(option.getOpt());
        }
    }
}

From source file:wvw.utils.cmd.CmdUtils.java

public String toString() {
    StringBuffer ret = new StringBuffer();
    ret.append("- parameters:\n");

    int cnt = 0;/*from www.j  a  v  a2 s  .  co m*/
    for (Object obj : processedOptions) {

        if (cnt++ > 0)
            ret.append("\n");

        if (obj instanceof Option) {
            Option option = (Option) obj;

            if (option.hasArg())
                ret.append(option.getOpt() + " : " + option.getValue());
            else
                ret.append(option.getOpt());

        } else if (obj instanceof CmdOptionVal) {
            CmdOptionVal option = (CmdOptionVal) obj;

            if (option.getOption().hasArg())
                ret.append(option.getName() + " : " + option.getValue());
            else
                ret.append(option.getName());
        }
    }

    ret.append("\n");

    return ret.toString();
}