/** Sets a default value provider for the command and sub-commands * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * sub-commands and nested sub-subcommands <em>at the moment this method is called</em>. Sub-commands added * later will have the default setting. To ensure a setting is applied to all * sub-commands, call the setter last, after adding sub-commands.</p> * @param newValue the default value provider to use * @return this {@code CommandLine} object, to allow method chaining * @since 3.6 */ public CommandLine setDefaultValueProvider(IDefaultValueProvider newValue) { getCommandSpec().defaultValueProvider(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setDefaultValueProvider(newValue); } return this; }
@Test public void testDefaultValueInDescriptionAfterSetProvider() { String expected2 = String.format("" + "Usage: <main class> [OPTIONS] [<paramStringFieldWithoutDefaultNorInitialValue>] [<paramStringFieldWithAnnotatedDefault>] [<paramStringFieldWithInitDefault>]%n" + " [<paramStringFieldWithoutDefaultNorInitialValue>]%n" + " Default: XYZ%n" + " Default: XYZ%n" + " [<paramStringFieldWithAnnotatedDefault>]%n" + " Default: XYZ%n" + " [<paramStringFieldWithInitDefault>]%n" + " Default: XYZ%n" + " -a=<optionStringFieldWithoutDefaultNorInitialValue>%n" + " Default: XYZ%n" + " -b=<optionStringFieldWithAnnotatedDefault>%n" + " Default: XYZ%n" + " -c=<optionStringFieldWithInitDefault>%n" + " Default: XYZ%n" + " Default: XYZ%n" + " -d=<string> Default: XYZ%n"); CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new IDefaultValueProvider() { public String defaultValue(ArgSpec argSpec) throws Exception { return "XYZ"; } }); assertEquals(expected2, cmd.getUsageMessage(CommandLine.Help.Ansi.OFF)); }
@Test public void testDefaultValueInDescriptionWithErrorProvider() { String expected2 = String.format("" + "Usage: <main class> [OPTIONS] [<paramStringFieldWithoutDefaultNorInitialValue>] [<paramStringFieldWithAnnotatedDefault>] [<paramStringFieldWithInitDefault>]%n" + " [<paramStringFieldWithoutDefaultNorInitialValue>]%n" + " Default: null%n" + " Default: null%n" + " [<paramStringFieldWithAnnotatedDefault>]%n" + " Default: Annotated default value%n" + " [<paramStringFieldWithInitDefault>]%n" + " Default: Initial default value%n" + " -a=<optionStringFieldWithoutDefaultNorInitialValue>%n" + " Default: null%n" + " -b=<optionStringFieldWithAnnotatedDefault>%n" + " Default: Annotated default value%n" + " -c=<optionStringFieldWithInitDefault>%n" + " Default: Initial default value%n" + " Default: Initial default value%n" + " -d=<string> Default: Annotated setter default value%n"); CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new IDefaultValueProvider() { public String defaultValue(ArgSpec argSpec) throws Exception { throw new IllegalStateException("abc"); } }); assertEquals(expected2, cmd.getUsageMessage(CommandLine.Help.Ansi.OFF)); } }
@Test public void testDefaultProviderReturnsSetValue() { CommandLine cmd = new CommandLine(Sub.class); TestDefaultProvider provider = new TestDefaultProvider(); cmd.setDefaultValueProvider(provider); assertSame(provider, cmd.getDefaultValueProvider()); }
@Test public void testCommandDefaultProviderSetting() { CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new TestDefaultProvider()); cmd.parse(); App app = cmd.getCommand(); // if no default defined on the option, command default provider should be used assertEquals("Default provider string value", app.optionStringFieldWithoutDefaultNorInitialValue); }
@Test public void testDefaultProviderPropagatedToSubCommand() { CommandLine cmd = new CommandLine(App.class); cmd.addSubcommand("sub", new CommandLine(Sub.class)); CommandLine subCommandLine = cmd.getSubcommands().get("sub"); cmd.setDefaultValueProvider(new TestDefaultProvider()); assertNotNull(subCommandLine.getCommandSpec().defaultValueProvider()); assertEquals(TestDefaultProvider.class, subCommandLine.getCommandSpec().defaultValueProvider().getClass()); }
@Test public void testCommandDefaultProviderDoesntOverridesDefaultsIfValueIsNull() { CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new TestNullDefaultProvider()); cmd.parse(); App app = cmd.getCommand(); // if no default defined on the option, command default provider should be used assertNull(app.optionStringFieldWithoutDefaultNorInitialValue); assertNull(app.paramStringFieldWithoutDefaultNorInitialValue); // if a default is defined on the option either by annotation or by initial value, it must // override the default provider. assertEquals("Annotated default value", app.optionStringFieldWithAnnotatedDefault); assertEquals("Annotated default value", app.paramStringFieldWithAnnotatedDefault); assertEquals("Initial default value", app.optionStringFieldWithInitDefault); assertEquals("Initial default value", app.paramStringFieldWithInitDefault); assertEquals("Annotated setter default value", app.stringForSetterDefault); }
/** Sets a default value provider for the command and sub-commands * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * sub-commands and nested sub-subcommands <em>at the moment this method is called</em>. Sub-commands added * later will have the default setting. To ensure a setting is applied to all * sub-commands, call the setter last, after adding sub-commands.</p> * @param newValue the default value provider to use * @return this {@code CommandLine} object, to allow method chaining * @since 3.6 */ public CommandLine setDefaultValueProvider(IDefaultValueProvider newValue) { getCommandSpec().defaultValueProvider(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setDefaultValueProvider(newValue); } return this; }
@Override protected List<Object> handle(final ParseResult parseResult) throws ExecutionException { final CommandLine commandLine = parseResult.asCommandLineList().get(0); if (parseResult.hasMatchedOption(configFileOptionName)) { final OptionSpec configFileOption = parseResult.matchedOption(configFileOptionName); final File configFile; try { configFile = configFileOption.getter().get(); } catch (final Exception e) { throw new ExecutionException(commandLine, e.getMessage(), e); } final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } else if (isDocker) { final File configFile = new File(DOCKER_CONFIG_LOCATION); if (configFile.exists()) { final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } } commandLine.parseWithHandlers( resultHandler, exceptionHandler, parseResult.originalArgs().toArray(new String[0])); return new ArrayList<>(); }
@Test public void handle() throws Exception { when(mockConfigOptionGetter.get()).thenReturn(temp.newFile()); final List<Object> result = configParsingHandler.handle(mockParseResult); verify(mockCommandLine).setDefaultValueProvider(any(IDefaultValueProvider.class)); verify(mockCommandLine).parseWithHandlers(eq(resultHandler), eq(exceptionHandler), anyString()); assertThat(result, is(empty())); }
@Test public void testDefaultValueInDescriptionAfterSetProvider() { String expected2 = String.format("" + "Usage: <main class> [OPTIONS] [<paramStringFieldWithoutDefaultNorInitialValue>] [<paramStringFieldWithAnnotatedDefault>] [<paramStringFieldWithInitDefault>]%n" + " [<paramStringFieldWithoutDefaultNorInitialValue>]%n" + " Default: XYZ%n" + " Default: XYZ%n" + " [<paramStringFieldWithAnnotatedDefault>]%n" + " Default: XYZ%n" + " [<paramStringFieldWithInitDefault>]%n" + " Default: XYZ%n" + " -a=<optionStringFieldWithoutDefaultNorInitialValue>%n" + " Default: XYZ%n" + " -b=<optionStringFieldWithAnnotatedDefault>%n" + " Default: XYZ%n" + " -c=<optionStringFieldWithInitDefault>%n" + " Default: XYZ%n" + " Default: XYZ%n" + " -d=<string> Default: XYZ%n"); CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new IDefaultValueProvider() { public String defaultValue(ArgSpec argSpec) throws Exception { return "XYZ"; } }); assertEquals(expected2, cmd.getUsageMessage(CommandLine.Help.Ansi.OFF)); }
@Test public void testDefaultValueInDescriptionWithErrorProvider() { String expected2 = String.format("" + "Usage: <main class> [OPTIONS] [<paramStringFieldWithoutDefaultNorInitialValue>] [<paramStringFieldWithAnnotatedDefault>] [<paramStringFieldWithInitDefault>]%n" + " [<paramStringFieldWithoutDefaultNorInitialValue>]%n" + " Default: null%n" + " Default: null%n" + " [<paramStringFieldWithAnnotatedDefault>]%n" + " Default: Annotated default value%n" + " [<paramStringFieldWithInitDefault>]%n" + " Default: Initial default value%n" + " -a=<optionStringFieldWithoutDefaultNorInitialValue>%n" + " Default: null%n" + " -b=<optionStringFieldWithAnnotatedDefault>%n" + " Default: Annotated default value%n" + " -c=<optionStringFieldWithInitDefault>%n" + " Default: Initial default value%n" + " Default: Initial default value%n" + " -d=<string> Default: Annotated setter default value%n"); CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new IDefaultValueProvider() { public String defaultValue(ArgSpec argSpec) throws Exception { throw new IllegalStateException("abc"); } }); assertEquals(expected2, cmd.getUsageMessage(CommandLine.Help.Ansi.OFF)); } static class FooDefaultProvider implements IDefaultValueProvider {
@Test public void testDefaultProviderReturnsSetValue() { CommandLine cmd = new CommandLine(Sub.class); TestDefaultProvider provider = new TestDefaultProvider(); cmd.setDefaultValueProvider(provider); assertSame(provider, cmd.getDefaultValueProvider()); }
@Test public void testCommandDefaultProviderSetting() { CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new TestDefaultProvider()); cmd.parse(); App app = cmd.getCommand(); // if no default defined on the option, command default provider should be used assertEquals("Default provider string value", app.optionStringFieldWithoutDefaultNorInitialValue); }
@Test public void testDefaultProviderPropagatedToSubCommand() { CommandLine cmd = new CommandLine(App.class); cmd.addSubcommand("sub", new CommandLine(Sub.class)); CommandLine subCommandLine = cmd.getSubcommands().get("sub"); cmd.setDefaultValueProvider(new TestDefaultProvider()); assertNotNull(subCommandLine.getCommandSpec().defaultValueProvider()); assertEquals(TestDefaultProvider.class, subCommandLine.getCommandSpec().defaultValueProvider().getClass()); }
@Test public void testCommandDefaultProviderDoesntOverridesDefaultsIfValueIsNull() { CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new TestNullDefaultProvider()); cmd.parse(); App app = cmd.getCommand(); // if no default defined on the option, command default provider should be used assertNull(app.optionStringFieldWithoutDefaultNorInitialValue); assertNull(app.paramStringFieldWithoutDefaultNorInitialValue); // if a default is defined on the option either by annotation or by initial value, it must // override the default provider. assertEquals("Annotated default value", app.optionStringFieldWithAnnotatedDefault); assertEquals("Annotated default value", app.paramStringFieldWithAnnotatedDefault); assertEquals("Initial default value", app.optionStringFieldWithInitDefault); assertEquals("Initial default value", app.paramStringFieldWithInitDefault); assertEquals("Annotated setter default value", app.stringForSetterDefault); }