private StringBuilder usage(StringBuilder sb, Help help) { for (String key : getHelpSectionKeys()) { IHelpSectionRenderer renderer = getHelpSectionMap().get(key); if (renderer != null) { sb.append(renderer.render(help)); } } return sb; }
public static void main(String[] args) { CommandLine cmd = new CommandLine(new ShowAll()); cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new MyCommandListRenderer()); cmd.usage(System.out); } }
@Test public void testNullSectionRenderer() { CommandLine cmd = new CommandLine(new UsageDemo()); cmd.getHelpSectionMap().clear(); cmd.getHelpSectionMap().put(UsageMessageSpec.SECTION_KEY_HEADER, null); cmd.getHelpSectionMap().put(UsageMessageSpec.SECTION_KEY_DESCRIPTION, new IHelpSectionRenderer() { public String render(Help help) { return "abc"; } }); String actual = cmd.getUsageMessage(); String expected = "abc"; assertEquals(expected, actual); }
@Test public void testSetHelpSectionMap_AfterSubcommandsAdded() { @Command class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); commandLine.addSubcommand("main", createNestedCommand()); final Set<String> DEFAULT_KEYS = new HashSet<String>(Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis", "descriptionHeading", "description", "parameterListHeading", "parameterList", "optionListHeading", "optionList", "commandListHeading", "commandList", "footerHeading", "footer")); assertEquals(DEFAULT_KEYS, commandLine.getHelpSectionMap().keySet()); Map<String, CommandLine.IHelpSectionRenderer> NEW_MAP = new HashMap<String, CommandLine.IHelpSectionRenderer>(); NEW_MAP.put("a", null); NEW_MAP.put("b", null); NEW_MAP.put("c", null); commandLine.setHelpSectionMap(NEW_MAP); assertEquals(NEW_MAP.keySet(), commandLine.getHelpSectionMap().keySet()); int childCount = 0; int grandChildCount = 0; for (CommandLine sub : commandLine.getSubcommands().values()) { childCount++; assertEquals("subcommand added before IS impacted", NEW_MAP.keySet(), sub.getHelpSectionMap().keySet()); for (CommandLine subsub : sub.getSubcommands().values()) { grandChildCount++; assertEquals("subsubcommand added before IS impacted", NEW_MAP.keySet(), sub.getHelpSectionMap().keySet()); } } assertTrue(childCount > 0); assertTrue(grandChildCount > 0); }
@Test public void testSetHelpSectionMap_BeforeSubcommandsAdded() { @Command class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); final Set<String> DEFAULT_KEYS = new HashSet<String>(Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis", "descriptionHeading", "description", "parameterListHeading", "parameterList", "optionListHeading", "optionList", "commandListHeading", "commandList", "footerHeading", "footer")); assertEquals(DEFAULT_KEYS, commandLine.getHelpSectionMap().keySet()); Map<String, CommandLine.IHelpSectionRenderer> NEW_MAP = new HashMap<String, CommandLine.IHelpSectionRenderer>(); NEW_MAP.put("a", null); NEW_MAP.put("b", null); NEW_MAP.put("c", null); commandLine.setHelpSectionMap(NEW_MAP); assertEquals(NEW_MAP.keySet(), commandLine.getHelpSectionMap().keySet()); int childCount = 0; int grandChildCount = 0; commandLine.addSubcommand("main", createNestedCommand()); for (CommandLine sub : commandLine.getSubcommands().values()) { childCount++; assertEquals("subcommand added afterwards is not impacted", DEFAULT_KEYS, sub.getHelpSectionMap().keySet()); for (CommandLine subsub : sub.getSubcommands().values()) { grandChildCount++; assertEquals("subcommand added afterwards is not impacted", DEFAULT_KEYS, subsub.getHelpSectionMap().keySet()); } } assertTrue(childCount > 0); assertTrue(grandChildCount > 0); }
@Test public void testCustomizableHelpSections() { @Command(header="<header> (%s)", description="<description>") class TestCommand { } CommandLine commandLineWithCustomHelpSections = new CommandLine(new TestCommand()); IHelpSectionRenderer renderer = new IHelpSectionRenderer() { public String render(Help help) { return help.header("<custom header param>"); } }; commandLineWithCustomHelpSections.getHelpSectionMap().put("customSectionExtendsHeader", renderer); commandLineWithCustomHelpSections.setHelpSectionKeys(Arrays.asList( UsageMessageSpec.SECTION_KEY_DESCRIPTION, UsageMessageSpec.SECTION_KEY_SYNOPSIS_HEADING, "customSectionExtendsHeader")); String expected = String.format("" + "<description>%n" + "Usage: <header> (<custom header param>)%n"); assertEquals(expected, commandLineWithCustomHelpSections.getUsageMessage(Help.Ansi.OFF)); }
private StringBuilder usage(StringBuilder sb, Help help) { for (String key : getHelpSectionKeys()) { IHelpSectionRenderer renderer = getHelpSectionMap().get(key); if (renderer != null) { sb.append(renderer.render(help)); } } return sb; }
@Test public void testNullSectionRenderer() { CommandLine cmd = new CommandLine(new UsageDemo()); cmd.getHelpSectionMap().clear(); cmd.getHelpSectionMap().put(UsageMessageSpec.SECTION_KEY_HEADER, null); cmd.getHelpSectionMap().put(UsageMessageSpec.SECTION_KEY_DESCRIPTION, new IHelpSectionRenderer() { public String render(Help help) { return "abc"; } }); String actual = cmd.getUsageMessage(); String expected = "abc"; assertEquals(expected, actual); }
@Test public void testSetHelpSectionMap_AfterSubcommandsAdded() { @Command class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); commandLine.addSubcommand("main", createNestedCommand()); final Set<String> DEFAULT_KEYS = new HashSet<String>(Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis", "descriptionHeading", "description", "parameterListHeading", "parameterList", "optionListHeading", "optionList", "commandListHeading", "commandList", "footerHeading", "footer")); assertEquals(DEFAULT_KEYS, commandLine.getHelpSectionMap().keySet()); Map<String, CommandLine.IHelpSectionRenderer> NEW_MAP = new HashMap<String, CommandLine.IHelpSectionRenderer>(); NEW_MAP.put("a", null); NEW_MAP.put("b", null); NEW_MAP.put("c", null); commandLine.setHelpSectionMap(NEW_MAP); assertEquals(NEW_MAP.keySet(), commandLine.getHelpSectionMap().keySet()); int childCount = 0; int grandChildCount = 0; for (CommandLine sub : commandLine.getSubcommands().values()) { childCount++; assertEquals("subcommand added before IS impacted", NEW_MAP.keySet(), sub.getHelpSectionMap().keySet()); for (CommandLine subsub : sub.getSubcommands().values()) { grandChildCount++; assertEquals("subsubcommand added before IS impacted", NEW_MAP.keySet(), sub.getHelpSectionMap().keySet()); } } assertTrue(childCount > 0); assertTrue(grandChildCount > 0); }
@Test public void testSetHelpSectionMap_BeforeSubcommandsAdded() { @Command class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); final Set<String> DEFAULT_KEYS = new HashSet<String>(Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis", "descriptionHeading", "description", "parameterListHeading", "parameterList", "optionListHeading", "optionList", "commandListHeading", "commandList", "footerHeading", "footer")); assertEquals(DEFAULT_KEYS, commandLine.getHelpSectionMap().keySet()); Map<String, CommandLine.IHelpSectionRenderer> NEW_MAP = new HashMap<String, CommandLine.IHelpSectionRenderer>(); NEW_MAP.put("a", null); NEW_MAP.put("b", null); NEW_MAP.put("c", null); commandLine.setHelpSectionMap(NEW_MAP); assertEquals(NEW_MAP.keySet(), commandLine.getHelpSectionMap().keySet()); int childCount = 0; int grandChildCount = 0; commandLine.addSubcommand("main", createNestedCommand()); for (CommandLine sub : commandLine.getSubcommands().values()) { childCount++; assertEquals("subcommand added afterwards is not impacted", DEFAULT_KEYS, sub.getHelpSectionMap().keySet()); for (CommandLine subsub : sub.getSubcommands().values()) { grandChildCount++; assertEquals("subcommand added afterwards is not impacted", DEFAULT_KEYS, subsub.getHelpSectionMap().keySet()); } } assertTrue(childCount > 0); assertTrue(grandChildCount > 0); }
@Test public void testCustomizableHelpSections() { @Command(header="<header> (%s)", description="<description>") class TestCommand { } CommandLine commandLineWithCustomHelpSections = new CommandLine(new TestCommand()); IHelpSectionRenderer renderer = new IHelpSectionRenderer() { public String render(Help help) { return help.header("<custom header param>"); } }; commandLineWithCustomHelpSections.getHelpSectionMap().put("customSectionExtendsHeader", renderer); commandLineWithCustomHelpSections.setHelpSectionKeys(Arrays.asList( UsageMessageSpec.SECTION_KEY_DESCRIPTION, UsageMessageSpec.SECTION_KEY_SYNOPSIS_HEADING, "customSectionExtendsHeader")); String expected = String.format("" + "<description>%n" + "Usage: <header> (<custom header param>)%n"); assertEquals(expected, commandLineWithCustomHelpSections.getUsageMessage(Help.Ansi.OFF)); }