Exemplo n.º 1
0
 /**
  * Prints a usage message to the given stream.
  *
  * @param out The output stream to write to.
  */
 public void printUsage(OutputStream out) {
   Formatter formatter = new Formatter(out);
   Set<CommandLineOption> orderedOptions = new TreeSet<CommandLineOption>(new OptionComparator());
   orderedOptions.addAll(optionsByString.values());
   Map<String, String> lines = new LinkedHashMap<String, String>();
   for (CommandLineOption option : orderedOptions) {
     Set<String> orderedOptionStrings = new TreeSet<String>(new OptionStringComparator());
     orderedOptionStrings.addAll(option.getOptions());
     List<String> prefixedStrings = new ArrayList<String>();
     for (String optionString : orderedOptionStrings) {
       if (optionString.length() == 1) {
         prefixedStrings.add("-" + optionString);
       } else {
         prefixedStrings.add("--" + optionString);
       }
     }
     lines.put(GUtil.join(prefixedStrings, ", "), GUtil.elvis(option.getDescription(), ""));
   }
   int max = 0;
   for (String optionStr : lines.keySet()) {
     max = Math.max(max, optionStr.length());
   }
   for (Map.Entry<String, String> entry : lines.entrySet()) {
     if (entry.getValue().length() == 0) {
       formatter.format("%s%n", entry.getKey());
     } else {
       formatter.format("%-" + max + "s  %s%n", entry.getKey(), entry.getValue());
     }
   }
   formatter.flush();
 }
Exemplo n.º 2
0
 /**
  * Defines a new option. By default, the option takes no arguments and has no description.
  *
  * @param options The options values.
  * @return The option, which can be further configured.
  */
 public CommandLineOption option(String... options) {
   for (String option : options) {
     if (optionsByString.containsKey(option)) {
       throw new IllegalArgumentException(
           String.format("Option '%s' is already defined.", option));
     }
     if (option.startsWith("-")) {
       throw new IllegalArgumentException(
           String.format("Cannot add option '%s' as an option cannot start with '-'.", option));
     }
   }
   CommandLineOption option = new CommandLineOption(Arrays.asList(options));
   for (String optionStr : option.getOptions()) {
     this.optionsByString.put(optionStr, option);
   }
   return option;
 }
Exemplo n.º 3
0
 public int compare(CommandLineOption option1, CommandLineOption option2) {
   String min1 = Collections.min(option1.getOptions(), new OptionStringComparator());
   String min2 = Collections.min(option2.getOptions(), new OptionStringComparator());
   return new CaseInsensitiveStringComparator().compare(min1, min2);
 }