Example #1
0
  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);
    }
  }
Example #2
0
 @Override
 public String toString() {
   return String.format("function %s(%s)", name, StringUtil.join(argumentList, ", "));
 }