/** * Gets a detailed description for a given argument group. * * @param argumentDefinitionGroup The group of argument definitions to render. * @return A string giving detailed info about the contents of this group. */ private String getDetailForGroup(ArgumentDefinitionGroup argumentDefinitionGroup) { if (argumentDefinitionGroup.allHidden()) return ""; StringBuilder builder = new StringBuilder(); Formatter formatter = new Formatter(builder); if (argumentDefinitionGroup.groupName != null && argumentDefinitionGroup.argumentDefinitions.size() != 0) builder.append(String.format("%nArguments for %s:%n", argumentDefinitionGroup.groupName)); List<ArgumentDefinition> argumentDefinitions = new ArrayList<ArgumentDefinition>(); for (ArgumentDefinition argumentDefinition : argumentDefinitionGroup.argumentDefinitions) { if (!argumentDefinition.isHidden) argumentDefinitions.add(argumentDefinition); } // Try to fit the entire argument definition across the screen, but impose an arbitrary cap of // 3/4 * // LINE_WIDTH in case the length of the arguments gets out of control. int argWidth = Math.min( findLongestArgumentCallingInfo(argumentDefinitions), (TextFormattingUtils.DEFAULT_LINE_WIDTH * 3) / 4 - FIELD_SEPARATION_WIDTH); int docWidth = TextFormattingUtils.DEFAULT_LINE_WIDTH - argWidth - FIELD_SEPARATION_WIDTH; for (ArgumentDefinition argumentDefinition : argumentDefinitions) { Iterator<String> wordWrappedArgs = TextFormattingUtils.wordWrap(getArgumentCallingInfo(argumentDefinition), argWidth) .iterator(); Iterator<String> wordWrappedDoc = TextFormattingUtils.wordWrap(getArgumentDoc(argumentDefinition), docWidth).iterator(); while (wordWrappedArgs.hasNext() || wordWrappedDoc.hasNext()) { String arg = wordWrappedArgs.hasNext() ? wordWrappedArgs.next() : ""; String doc = wordWrappedDoc.hasNext() ? wordWrappedDoc.next() : ""; String formatString = "%-" + argWidth + "s%" + FIELD_SEPARATION_WIDTH + "s%s%n"; formatter.format(formatString, arg, "", doc); } } return builder.toString(); }
/** * Gets the synopsis: the actual command to run. * * @param runningInstructions Instructions on how to run hte application. * @param argumentGroups Program arguments sorted in order of definition group displays. * @return A synopsis line. */ private String getSynopsis( String runningInstructions, List<ArgumentDefinitionGroup> argumentGroups) { // Build out the synopsis all as one long line. StringBuilder lineBuilder = new StringBuilder(); Formatter lineFormatter = new Formatter(lineBuilder); lineFormatter.format("java %s", runningInstructions); for (ArgumentDefinitionGroup argumentGroup : argumentGroups) { for (ArgumentDefinition argumentDefinition : argumentGroup.argumentDefinitions) { if (argumentDefinition.isHidden) continue; lineFormatter.format(" "); if (!argumentDefinition.required) lineFormatter.format("["); if (argumentDefinition.shortName != null) lineFormatter.format("-%s", argumentDefinition.shortName); else lineFormatter.format("--%s", argumentDefinition.fullName); if (!argumentDefinition.isFlag) lineFormatter.format(" <%s>", argumentDefinition.fullName); if (!argumentDefinition.required) lineFormatter.format("]"); } } // Word wrap the synopsis. List<String> wrappedSynopsis = TextFormattingUtils.wordWrap( lineBuilder.toString(), TextFormattingUtils.DEFAULT_LINE_WIDTH); String header = "usage: "; int headerLength = header.length(); StringBuilder synopsisBuilder = new StringBuilder(); Formatter synopsisFormatter = new Formatter(synopsisBuilder); for (String synopsisLine : wrappedSynopsis) { synopsisFormatter.format("%" + headerLength + "s%s%n", header, synopsisLine); header = ""; } return synopsisBuilder.toString(); }