/** Generates a generic synopsis like {@code <command name> [OPTIONS] [PARAM1 [PARAM2]...]}, omitting parts * that don't apply to the command (e.g., does not show [OPTIONS] if the command has no options). * @return a generic synopsis */ public String abbreviatedSynopsis() { StringBuilder sb = new StringBuilder(); if (!commandSpec.optionsMap().isEmpty()) { // only show if annotated object actually has options sb.append(" [OPTIONS]"); } // sb.append(" [--] "); // implied for (PositionalParamSpec positionalParam : commandSpec.positionalParameters()) { if (!positionalParam.hidden()) { sb.append(' ').append(parameterLabelRenderer().renderParameterLabel(positionalParam, ansi(), colorScheme.parameterStyles)); } } // only show if object has subcommands if (!commandSpec.subcommands().isEmpty()) { sb.append(" [COMMAND]"); } return colorScheme.commandText(commandSpec.qualifiedName()).toString() + (sb.toString()) + System.getProperty("line.separator"); } /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
/** Returns a {@code Text} object containing the command name and all aliases, separated with the specified separator. * Command names will use the {@link ColorScheme#commandText(String) command style} for the color scheme of this Help. * @since 3.9 */ public Text commandNamesText(String separator) { Text result = colorScheme().commandText(aliases().get(0)); for (int i = 1; i < aliases().size(); i++) { result = result.concat(separator).concat(colorScheme().commandText(aliases().get(i))); } return result; } private static String join(String[] names, int offset, int length, String separator) {
/** * Returns the detailed synopsis text by inserting the command name before the specified text with options and positional parameters details. * @param synopsisHeadingLength length of the synopsis heading string to be displayed on the same line as the first synopsis line. * For example, if the synopsis heading is {@code "Usage: "}, this value is 7. * @param optionsAndPositionalsAndCommandsDetails formatted string with options, positional parameters and subcommands. * Follows the unix convention of showing optional options and parameters in square brackets ({@code [ ]}). * @return the detailed synopsis text, in multiple lines if the length exceeds the usage width */ protected String insertSynopsisCommandName(int synopsisHeadingLength, Text optionsAndPositionalsAndCommandsDetails) { // Fix for #142: first line of synopsis overshoots max. characters String commandName = commandSpec.qualifiedName(); int firstColumnLength = commandName.length() + synopsisHeadingLength; // synopsis heading ("Usage: ") may be on the same line, so adjust column width TextTable textTable = TextTable.forColumnWidths(ansi(), firstColumnLength, width() - firstColumnLength); textTable.indentWrappedLines = 1; // don't worry about first line: options (2nd column) always start with a space // right-adjust the command name by length of synopsis heading Text PADDING = Ansi.OFF.new Text(stringOf('X', synopsisHeadingLength)); textTable.addRowValues(PADDING.concat(colorScheme.commandText(commandName)), optionsAndPositionalsAndCommandsDetails); return textTable.toString().substring(synopsisHeadingLength); // cut off leading synopsis heading spaces }
/** Generates a generic synopsis like {@code <command name> [OPTIONS] [PARAM1 [PARAM2]...]}, omitting parts * that don't apply to the command (e.g., does not show [OPTIONS] if the command has no options). * @return a generic synopsis */ public String abbreviatedSynopsis() { StringBuilder sb = new StringBuilder(); if (!commandSpec.optionsMap().isEmpty()) { // only show if annotated object actually has options sb.append(" [OPTIONS]"); } // sb.append(" [--] "); // implied for (PositionalParamSpec positionalParam : commandSpec.positionalParameters()) { if (!positionalParam.hidden()) { sb.append(' ').append(parameterLabelRenderer().renderParameterLabel(positionalParam, ansi(), colorScheme.parameterStyles)); } } // only show if object has subcommands if (!commandSpec.subcommands().isEmpty()) { sb.append(" [COMMAND]"); } return colorScheme.commandText(commandSpec.qualifiedName()).toString() + (sb.toString()) + System.getProperty("line.separator"); } /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
/** * Returns the detailed synopsis text by inserting the command name before the specified text with options and positional parameters details. * @param synopsisHeadingLength length of the synopsis heading string to be displayed on the same line as the first synopsis line. * For example, if the synopsis heading is {@code "Usage: "}, this value is 7. * @param optionsAndPositionalsAndCommandsDetails formatted string with options, positional parameters and subcommands. * Follows the unix convention of showing optional options and parameters in square brackets ({@code [ ]}). * @return the detailed synopsis text, in multiple lines if the length exceeds the usage width */ protected String insertSynopsisCommandName(int synopsisHeadingLength, Text optionsAndPositionalsAndCommandsDetails) { // Fix for #142: first line of synopsis overshoots max. characters String commandName = commandSpec.qualifiedName(); int firstColumnLength = commandName.length() + synopsisHeadingLength; // synopsis heading ("Usage: ") may be on the same line, so adjust column width TextTable textTable = TextTable.forColumnWidths(ansi(), firstColumnLength, width() - firstColumnLength); textTable.indentWrappedLines = 1; // don't worry about first line: options (2nd column) always start with a space // right-adjust the command name by length of synopsis heading Text PADDING = Ansi.OFF.new Text(stringOf('X', synopsisHeadingLength)); textTable.addRowValues(PADDING.concat(colorScheme.commandText(commandName)), optionsAndPositionalsAndCommandsDetails); return textTable.toString().substring(synopsisHeadingLength); // cut off leading synopsis heading spaces }
@Test public void testTextDefaultColorScheme() { Ansi ansi = Ansi.ON; CommandLine.Help.ColorScheme scheme = CommandLine.Help.defaultColorScheme(ansi); assertEquals(scheme.ansi().new Text("@|yellow -p|@"), scheme.optionText("-p")); assertEquals(scheme.ansi().new Text("@|bold command|@"), scheme.commandText("command")); assertEquals(scheme.ansi().new Text("@|yellow FILE|@"), scheme.parameterText("FILE")); assertEquals(scheme.ansi().new Text("@|italic NUMBER|@"), scheme.optionParamText("NUMBER")); }
/** Returns a {@code Text} object containing the command name and all aliases, separated with the specified separator. * Command names will use the {@link ColorScheme#commandText(String) command style} for the color scheme of this Help. * @since 3.9 */ public Text commandNamesText(String separator) { Text result = colorScheme().commandText(aliases().get(0)); for (int i = 1; i < aliases().size(); i++) { result = result.concat(separator).concat(colorScheme().commandText(aliases().get(i))); } return result; } private static String join(String[] names, int offset, int length, String separator) {
@Test public void testTextDefaultColorScheme() { Ansi ansi = Ansi.ON; CommandLine.Help.ColorScheme scheme = CommandLine.Help.defaultColorScheme(ansi); assertEquals(scheme.ansi().new Text("@|yellow -p|@"), scheme.optionText("-p")); assertEquals(scheme.ansi().new Text("@|bold command|@"), scheme.commandText("command")); assertEquals(scheme.ansi().new Text("@|yellow FILE|@"), scheme.parameterText("FILE")); assertEquals(scheme.ansi().new Text("@|italic NUMBER|@"), scheme.optionParamText("NUMBER")); }