public void renderOption(OptionItem option, PrintWriter writer, boolean enabled) { new TagWriter(writer) .start("option") .attribute("id", option.getId()) .attribute("class", option.getStyleClass()) .attribute("value", option.getIndex()) .attribute("selected", option.isSelected()) .body(FormatUtils.stripTags(option.getLabel())) .end(); }
/** * @param optionName * @return */ public OptionItem getOption(String optionName) { for (OptionItem eachOption : optionItemList) { Option opt = eachOption.getOption(); if (optionName.equals(opt.symbol())) return eachOption; String longName = opt.longName(); if (longName != null && optionName.equals(longName)) return eachOption; } return null; }
protected static String optionDescription(OptionItem optionItem) { StringBuilder line = new StringBuilder(); Option opt = optionItem.getOption(); if (optionItem.hasSymbol()) { line.append(String.format("-%s", opt.symbol())); if (optionItem.hasLongName()) { line.append(String.format(", --%s", opt.longName())); if (optionItem.needsArgument()) line.append(String.format("=%s", opt.varName())); } else { if (optionItem.needsArgument()) line.append(String.format(" ", opt.varName())); } } else if (optionItem.hasLongName()) { line.append(String.format(" --%s", opt.longName())); if (optionItem.needsArgument()) line.append(String.format("=%s", opt.varName())); } else { throw new XerialError(XerialErrorCode.NO_OPTION, optionItem.toString()); } return line.toString(); }
public void printUsage(Writer out) { Properties helpMessageTemplateValue = new Properties(); // argument list Collections.sort( argumentItemList, new Comparator<ArgumentItem>() { public int compare(ArgumentItem o1, ArgumentItem o2) { return o1.getRange().compareTo(o2.getRange()); } }); List<String> argExpressionList = Algorithm.map( argumentItemList, new Mapper<ArgumentItem, String>() { public String map(ArgumentItem input) { return argumentExpression(input); } }); // usage information if (usage != null) { helpMessageTemplateValue.put(TemplateVariable.COMMAND.name(), usage.command()); if (usage.description() != null && usage.description().length() > 0) helpMessageTemplateValue.put(TemplateVariable.DESCRIPTION.name(), usage.description()); } helpMessageTemplateValue.put( TemplateVariable.ARGUMENT_LIST.name(), StringUtil.join(argExpressionList, " ")); // option list Collections.sort( optionItemList, new Comparator<OptionItem>() { public int compare(OptionItem o1, OptionItem o2) { Option opt1 = o1.getOption(); Option opt2 = o2.getOption(); // prefer options that have a short name if (o1.hasSymbol()) { if (!o2.hasSymbol()) return -1; } else if (o2.hasSymbol()) return 1; int diff = opt1.symbol().compareTo(opt2.symbol()); if (diff == 0) return opt1.longName().compareTo(opt2.longName()); else return diff; } }); List<String> descriptionList = Algorithm.map( optionItemList, new Mapper<OptionItem, String>() { public String map(OptionItem input) { return optionDescription(input); } }); int maxDescriptionLength = 15; for (String each : descriptionList) if (each.length() > maxDescriptionLength) maxDescriptionLength = each.length(); String optionHelpFormat = String.format(" %%-%ds %%s", maxDescriptionLength); StringWriter optionListHelpWriter = new StringWriter(); for (int i = 0; i < optionItemList.size(); ++i) { OptionItem optionItem = optionItemList.get(i); String optionHelp = descriptionList.get(i); String line = String.format(optionHelpFormat, optionHelp, optionItem.getOption().description()); optionListHelpWriter.append(line); optionListHelpWriter.append(StringUtil.newline()); } helpMessageTemplateValue.put( TemplateVariable.OPTION_LIST.name(), optionListHelpWriter.toString()); // render help messages using template ResourcePath rp = new ResourcePath(usage != null ? usage.templatePath() : Usage.DEFAULT_TEMPLATE); try { Template helpMessageTemplate = new Template(rp.openBinaryStream()); String helpMessage = helpMessageTemplate.apply(helpMessageTemplateValue); out.append(helpMessage); out.flush(); } catch (IOException e) { throw new XerialError(XerialErrorCode.OUTPUT_ERROR, e); } catch (XerialException e) { throw new XerialError(e); } }
public boolean isSelected(OptionItem option) { return selectedValues != null && selectedValues.contains(option.getValue()); }