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:name.livitski.databag.cli.Syntax.java

/**
 * Diagnostic entry point for the build process to check that all
 * usage strings that describe the application's syntax are present
 * in <code>usage.resources</code>.
 * Absent resources cause a <code>System.err</code> message
 * and a non-zero exit code./*  w ww .  ja va 2  s .  co m*/
 * @param args this method ignores its argument
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    PrintStream out = System.err;
    Resources resources = new Resources();
    final Class<Syntax> clazz = Syntax.class;
    String id = null;
    String legend = null;
    for (Option option : (Collection<Option>) OPTIONS.getOptions()) {
        try {
            id = getOptionId(option);
            legend = resources.getString(USAGE_BUNDLE, clazz, id).trim();
            if (legend.indexOf('.') < legend.length() - 1) {
                out.printf(
                        "Description of option %s must be a single sentence ending with a period. Got:%n\"%s\"%n",
                        id, legend);
                System.exit(5);
            }
            if (option.hasArg())
                resources.getString(USAGE_BUNDLE, clazz, "arg" + id);
            legend = null;
        } catch (MissingResourceException missing) {
            if (null == legend) {
                out.printf("Option \"%s\" does not have a description string in the resource bundle \"%s\"%n",
                        id, USAGE_BUNDLE);
                missing.printStackTrace(out);
                System.exit(1);
            }
            out.printf("Required argument spec \"%s\" is missing from the resource bundle \"%s\"%n",
                    getArgumentId(option), USAGE_BUNDLE);
            missing.printStackTrace(out);
            System.exit(2);
        } catch (Exception problem) {
            out.printf("Error while verifying option \"%s\" in the resource bundle \"%s\"%n", id, USAGE_BUNDLE);
            problem.printStackTrace(out);
            System.exit(-1);
        }
    }
}

From source file:be.dnsbelgium.rdap.client.ManGenerator.java

private static String getOptionString(Option option) {
    return String.format("%s%s%s", option.getOpt() == null ? "" : "-" + option.getOpt() + " ",
            option.getLongOpt() == null ? "" : "--" + option.getLongOpt() + " ",
            option.hasArg() ? "<" + option.getArgName() + ">" : ""

    ).trim();/*  w w  w  . j ava 2s.c o  m*/
}

From source file:com.datastax.brisk.demo.pricer.Pricer.java

/**
 * Printing out help message// w w w  .ja v a2s.c  om
 */
public static void printHelpMessage() {
    System.out.println("Usage: ./bin/pricer [options]\n\nOptions:");

    for (Object o : Session.availableOptions.getOptions()) {
        Option option = (Option) o;
        String upperCaseName = option.getLongOpt().toUpperCase();
        System.out.println(String.format("-%s%s, --%s%s%n\t\t%s%n", option.getOpt(),
                (option.hasArg()) ? " " + upperCaseName : "", option.getLongOpt(),
                (option.hasArg()) ? "=" + upperCaseName : "", option.getDescription()));
    }
}

From source file:dk.hippogrif.prettyxml.app.Main.java

private static void optionUsage(PrintStream ps) {
    for (Iterator iter = optionList.iterator(); iter.hasNext();) {
        Option option = (Option) iter.next();
        ps.print("  -" + option.getOpt());
        String spaces = "              ";
        int i = 0;
        if (option.hasArg()) {
            ps.print(" " + option.getArgName());
            i = option.getArgName().length() + 1;
        }//from  w  w w  .j a v a 2 s. c o m
        ps.print(spaces.substring(i));
        ps.println(option.getDescription());
    }
}

From source file:com.leshazlewood.scms.cli.Main.java

private static void printHelp(Options options, Exception e, boolean debug) {
    HelpFormatter help = new HelpFormatter();
    help.setWidth(80);//from w w  w. j  av  a  2s. c o  m
    String command = "scms [options] [src_dir] dest_dir";
    String header = "Injests content files in [src dir] and renders a static website into dest_dir.\n\n"
            + "  [src_dir] is optional and defaults to the current working directory.\n"
            + "  dest_dir is required and cannot be the same as src_dir.";
    /*String footer = "\n" +
        "Injests source content files and page templates in [src dir] and renders a\n" +
        "renders a static website into destination_directory.\n\n" +
        "If unspecified, [source directory] defaults to the current working\n" +
        "directory.  destination_directory is required and cannot be the same\n" +
        "as the source directory.";*/

    printException(e, debug);

    System.out.println();

    System.out.println("Usage:");
    System.out.print("  ");
    System.out.println(command);
    System.out.println();
    System.out.println("Description:");
    System.out.print("  ");
    System.out.println(header);
    System.out.println();
    System.out.println("Options:");

    StringBuilder sb = new StringBuilder();

    int columnWidth = calculateColumnWidth(options);

    for (Object o : options.getOptions()) {
        Option option = (Option) o;
        StringBuilder csb = new StringBuilder("  ");
        csb.append("-").append(option.getOpt()).append(",--").append(option.getLongOpt());
        if (option.hasArg()) {
            csb.append(" <arg>");
        }
        int csbLength = csb.length();
        for (int i = 0; i < (columnWidth - csbLength); i++) {
            csb.append(" ");
        }
        sb.append(csb.toString()).append("   ").append(option.getDescription()).append("\n");
    }
    System.out.println(sb);

    //help.printHelp("", "", options, null);
    //System.out.println(footer);
}

From source file:com.leshazlewood.scms.cli.Main.java

private static int calculateColumnWidth(Options options) {
    int max = 0;//from   ww  w  .  j  a  v a 2 s  .c  o  m
    for (Object o : options.getOptions()) {
        Option opt = (Option) o;
        int columnWidth = "-".length() + opt.getOpt().length();
        if (opt.hasLongOpt()) {
            columnWidth += ",--".length();
            columnWidth += opt.getLongOpt().length();
        }
        if (opt.hasArg()) {
            columnWidth += " <arg>".length();
        }
        columnWidth += 3; //buffer between description
        max = Math.max(max, columnWidth);
    }
    return max;
}

From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java

/**
 * Prints the help screen./*w w  w. jav a 2  s .co m*/
 *
 * @param options the expected command line parameters
 */
private static void printHelp(Options options) {
    // Automatically generate the help statement.
    HelpFormatter formatter = new HelpFormatter();
    formatter.setWidth(LINE_MAX_WIDTH);

    // Comparator to show non-argument options first.
    formatter.setOptionComparator(new Comparator<Option>() {
        @Override
        public int compare(Option o1, Option o2) {
            if (o1.hasArg() && !o2.hasArg()) {
                return 1;
            }
            if (!o1.hasArg() && o2.hasArg()) {
                return -1;
            }

            return o1.getOpt().compareTo(o2.getOpt());
        }
    });

    System.out.println("Keyword Optimizer - BETA");
    System.out.println("------------------------");
    System.out.println();
    System.out.println("This utility can be used creating a optimizing and finding a set of "
            + "'good' keywords. It uses the TargetingIdeaService / \n"
            + "TrafficEstimatorService of the AdWords API to first obtain a set "
            + "of seed keywords and then perform a round-based \nprocess for " + "optimizing them.");
    System.out.println();
    formatter.printHelp("keyword-optimizer", options);
    System.out.println();
}

From source file:eu.project.ttc.tools.cli.TermSuiteCLIUtils.java

/**
 * Displays all command line options in log messages.
 * @param line//from  w w  w. j  av  a  2  s.c  o  m
 */
public static void logCommandLineOptions(CommandLine line) {
    for (Option myOption : line.getOptions()) {
        String message;
        String opt = "";
        if (myOption.getOpt() != null) {
            opt += "-" + myOption.getOpt();
            if (myOption.getLongOpt() != null)
                opt += " (--" + myOption.getLongOpt() + ")";
        } else
            opt += "--" + myOption.getLongOpt() + "";

        if (myOption.hasArg())
            message = opt + " " + myOption.getValue();
        else
            message = opt;

        LOGGER.info("with option: " + message);
    }
}

From source file:com.marklogic.contentpump.utilities.CommandlineOption.java

public CommandlineOption(Option opt) throws IllegalArgumentException {
    super(opt.getOpt(), opt.hasArg(), opt.getDescription());

    this.setLongOpt(opt.getLongOpt());
    this.setRequired(opt.isRequired());
    this.setArgName(opt.getArgName());
    this.setArgs(opt.getArgs());
    this.setOptionalArg(opt.hasOptionalArg());
    this.setType(opt.getType());
    this.setValueSeparator(opt.getValueSeparator());
}

From source file:com.kylinolap.job.tools.OptionsHelper.java

public String getOptionsAsString() {
    StringBuilder buf = new StringBuilder();
    for (Option option : commandLine.getOptions()) {
        buf.append(" ");
        buf.append(option.getOpt());/*from  www. ja v a2 s.c  o  m*/
        if (option.hasArg()) {
            buf.append("=");
            buf.append(option.getValue());
        }
    }
    return buf.toString();
}