Skip to content

Commit

Permalink
Allow resetting more argument collections (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshakir authored Nov 13, 2024
1 parent f14e9a2 commit d4e1c40
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void setCollectionValues(
"A \"null\" value was detected for an option after values for that option were already set. " +
"Clobbering previously set values for this option: %s.", getArgumentAliasDisplayString()));
}
if (!isOptional()) {
if (!argumentAnnotation.optional() && i == preprocessedValues.size() - 1) {
throw new CommandLineException(
String.format("Non \"null\" value must be provided for '%s'", getArgumentAliasDisplayString()));
}
Expand Down Expand Up @@ -704,4 +704,4 @@ public int hashCode() {
result = 31 * result + (getDefaultValueAsString() != null ? getDefaultValueAsString().hashCode() : 0);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.testng.annotations.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class NamedArgumentDefinitionUnitTest {

Expand Down Expand Up @@ -150,4 +152,123 @@ private Field getFieldForFieldName(final Object argContainer, final String field
throw new IllegalArgumentException("Can't find field");
}

public static class ArgumentLists {
@Argument
List<String> required;
@Argument(optional = true)
List<String> optional;
@Argument
List<String> defaultedRequired = new ArrayList<>(List.of("default"));
@Argument(optional = true)
List<String> defaultedOptional = new ArrayList<>(List.of("default"));
}

@DataProvider
public Object[][] setArgumentValuesCollectionsTests() {
return new Object[][]{
{"required", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedRequired", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedOptional", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(),
List.of("stuff", "more stuff", "null"), List.of()},
{"defaultedOptional", Set.of(),
List.of("stuff", "more stuff", "null"), List.of()},

{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null"), List.of()},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null"), List.of()},
};
}

@Test(dataProvider = "setArgumentValuesCollectionsTests")
public void testSetArgumentValuesCollections(
final String fieldName,
final Set<CommandLineParserOptions> parserOptions,
final List<String> newValues,
final List<String> expectedValues
) throws IllegalAccessException {
final ArgumentLists argLists = new ArgumentLists();
final Field field = getFieldForFieldName(argLists, fieldName);
final NamedArgumentDefinition argDef =
new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null);
final CommandLineArgumentParser clp =
new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions);
argDef.setArgumentValues(clp, System.out, newValues);
Assert.assertEquals(field.get(argLists), expectedValues);
}

@DataProvider
public Object[][] setArgumentValuesCollectionsFailures() {
return new Object[][]{
{"required", Set.of(),
List.of("stuff", "more stuff", "null")},
{"defaultedRequired", Set.of(),
List.of("stuff", "more stuff", "null")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null")},
};
}

@Test(dataProvider = "setArgumentValuesCollectionsFailures", expectedExceptions = CommandLineException.class)
public void testSetArgumentValuesCollectionsFailures(
final String fieldName,
final Set<CommandLineParserOptions> parserOptions,
final List<String> newValues
) {
final ArgumentLists argLists = new ArgumentLists();
final Field field = getFieldForFieldName(argLists, fieldName);
final NamedArgumentDefinition argDef =
new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null);
final CommandLineArgumentParser clp =
new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions);
argDef.setArgumentValues(clp, System.out, newValues);
}
}

0 comments on commit d4e1c40

Please sign in to comment.