Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
!**/src/scribble/**/out/
*.iml
/.idea/
/bin/
Expand All @@ -16,7 +16,7 @@ out/
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
!**/src/scribble/**/bin/

### NetBeans ###
/nbproject/private/
Expand All @@ -30,6 +30,6 @@ bin/

### Mac OS ###
.DS_Store
/src/test/TestPrintout.java
/src/scribble/TestPrintout.java
/doc/
/lib/
90 changes: 89 additions & 1 deletion src/ArgsParser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ArgsParser {
private String[] args;
private final Map<String, Parameter<?>> parameterMap = new HashMap<>();
private final Set<Parameter<?>> mandatoryParameters = new HashSet<>();
private final Set<String> arrayParameters = new HashSet<>();
private final Set<String> fullFlags = new HashSet<>();
private final Set<String> shortFlags = new HashSet<>();
protected boolean parseArgsWasCalled = false;
Expand Down Expand Up @@ -191,6 +192,21 @@ public Parameter<String> addStringParameter(String fullFlag, String shortFlag, S
return createParameter(fullFlag, shortFlag, null, String.class, false, defaultValue);
}

/**
* Adds a new parameter that will be checked in args and assigned to the Parameter instance
* Array Parameters can take multiple arguments behind their flag
* @param fullFlag name of the parameter (-- will automatically be added)
* @param shortFlag short version of the parameter (- will automatically be added)
* @param description description of the parameter
* @param isMandatory true if parameter is mandatory, false if optional
* @return the created Parameter instance of type String[]
*/
public Parameter<String[]> addStringArrayParameter(String fullFlag, String shortFlag, String description, boolean isMandatory) {
arrayParameters.add(makeFlag(fullFlag, false));
arrayParameters.add(makeFlag(shortFlag, true));
return createParameter(fullFlag, shortFlag, description, String[].class, isMandatory, null);
}


// Integer Parameter constructors

Expand Down Expand Up @@ -241,6 +257,21 @@ public Parameter<Integer> addIntegerParameter(String fullFlag, String shortFlag,
return createParameter(fullFlag, shortFlag, null, Integer.class, false, defaultValue);
}

/**
* Adds a new parameter that will be checked in args and assigned to the Parameter instance
* Array Parameters can take multiple arguments behind their flag
* @param fullFlag name of the parameter (-- will automatically be added)
* @param shortFlag short version of the parameter (- will automatically be added)
* @param description description of the parameter
* @param isMandatory true if parameter is mandatory, false if optional
* @return the created Parameter instance of type String[]
*/
public Parameter<int[]> addIntegerArrayParameter(String fullFlag, String shortFlag, String description, boolean isMandatory) {
arrayParameters.add(makeFlag(fullFlag, false));
arrayParameters.add(makeFlag(shortFlag, true));
return createParameter(fullFlag, shortFlag, description, int[].class, isMandatory, null);
}


// Double Parameter constructors

Expand Down Expand Up @@ -291,6 +322,21 @@ public Parameter<Double> addDoubleParameter(String fullFlag, String shortFlag, D
return createParameter(fullFlag, shortFlag, null, Double.class, false, defaultValue);
}

/**
* Adds a new parameter that will be checked in args and assigned to the Parameter instance
* Array Parameters can take multiple arguments behind their flag
* @param fullFlag name of the parameter (-- will automatically be added)
* @param shortFlag short version of the parameter (- will automatically be added)
* @param description description of the parameter
* @param isMandatory true if parameter is mandatory, false if optional
* @return the created Parameter instance of type String[]
*/
public Parameter<double[]> addDoubleArrayParameter(String fullFlag, String shortFlag, String description, boolean isMandatory) {
arrayParameters.add(makeFlag(fullFlag, false));
arrayParameters.add(makeFlag(shortFlag, true));
return createParameter(fullFlag, shortFlag, description, double[].class, isMandatory, null);
}


// Boolean Parameter constructors

Expand Down Expand Up @@ -341,6 +387,21 @@ public Parameter<Boolean> addBooleanParameter(Boolean defaultValue, String fullF
return createParameter(fullFlag, shortFlag, null, Boolean.class, false, defaultValue);
}

/**
* Adds a new parameter that will be checked in args and assigned to the Parameter instance
* Array Parameters can take multiple arguments behind their flag
* @param fullFlag name of the parameter (-- will automatically be added)
* @param shortFlag short version of the parameter (- will automatically be added)
* @param description description of the parameter
* @param isMandatory true if parameter is mandatory, false if optional
* @return the created Parameter instance of type String[]
*/
public Parameter<Boolean[]> addBooleanArrayParameter(String fullFlag, String shortFlag, String description, boolean isMandatory) {
arrayParameters.add(makeFlag(fullFlag, false));
arrayParameters.add(makeFlag(shortFlag, true));
return createParameter(fullFlag, shortFlag, description, Boolean[].class, isMandatory, null);
}


// Character Parameter constructors

Expand Down Expand Up @@ -391,6 +452,21 @@ public Parameter<Character> addCharacterParameter(String fullFlag, String shortF
return createParameter(fullFlag, shortFlag, null, Character.class, false, defaultValue);
}

/**
* Adds a new parameter that will be checked in args and assigned to the Parameter instance
* Array Parameters can take multiple arguments behind their flag
* @param fullFlag name of the parameter (-- will automatically be added)
* @param shortFlag short version of the parameter (- will automatically be added)
* @param description description of the parameter
* @param isMandatory true if parameter is mandatory, false if optional
* @return the created Parameter instance of type String[]
*/
public Parameter<char[]> addCharacterArrayParameter(String fullFlag, String shortFlag, String description, boolean isMandatory) {
arrayParameters.add(makeFlag(fullFlag, false));
arrayParameters.add(makeFlag(shortFlag, true));
return createParameter(fullFlag, shortFlag, description, char[].class, isMandatory, null);
}

/**
* <ul>
* <li>checks if args is Empty</li>
Expand Down Expand Up @@ -522,9 +598,21 @@ private Set<Parameter<?>> parseArguments() throws UnknownFlagArgsException, TooM
throw new MissingArgArgsException(args[i]);

} else if (lastPositionWasFlag && currentParameterNotNull) { // if the current position is an argument

boolean isArrayParam = arrayParameters.contains(args[i - 1]);
if (isArrayParam) { // we "collect" all following arguments after an array parameter in a StringBuilder
StringBuilder arguments = new StringBuilder();
arguments.append(args[i]).append("==="); // every entry in the array gets seperated by ===
while(i + 1 < args.length && !args[i + 1].startsWith("-")) { // loop through all arguments
arguments.append(args[++i]).append("===");
}
currentParameter.setArgument(arguments.toString());

} else {
currentParameter.setArgument(args[i]);
givenParameters.add(currentParameter);

}
givenParameters.add(currentParameter); // add parameter to the given Parameter Set
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/ArgsParser/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class Parameter<T> {
converters.put(Integer.class, Integer::valueOf);
converters.put(Double.class, Double::valueOf);
converters.put(Boolean.class, Boolean::valueOf);
converters.put(String[].class, s -> s.split("==="));
converters.put(int[].class, s -> Arrays.stream(s.split("===")).mapToInt(Integer::parseInt).toArray());
converters.put(double[].class, s -> Arrays.stream(s.split("===")).mapToDouble(Double::parseDouble).toArray());
converters.put(Boolean[].class, s -> Arrays.stream(s.split("===")).map(Boolean::parseBoolean).toArray(Boolean[]::new));
converters.put(char[].class, s -> Arrays.stream(s.split("===")).map(c -> c.charAt(0))
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString().toCharArray());
converters.put(Character.class, s -> {
if (s.length() != 1) {
throw new IllegalArgumentException("Argument must be a single character!");
Expand Down
117 changes: 117 additions & 0 deletions src/test/TestArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,123 @@ public void testDirectParse() {
assertEquals("file.txt", file.getArgument());
}

@Test
public void testStringArrayParameter() {
String[] args = {"--file", "file1.txt", "file2.txt", "file3.txt"};
ArgsParser parser = new ArgsParser(args);

Parameter<String[]> files = parser.addStringArrayParameter("file", "f", "descr", true);
try {
parser.parseUnchecked();
} catch (Exception e) {
e.printStackTrace();
}

String[] result = files.getArgument();
String[] expected = new String[] {"file1.txt", "file2.txt", "file3.txt"};

assertArrayEquals(expected, result);
}

@Test
public void testIntegerArrayParameter() {
String[] args = {"--integer", "1", "2", "3"};
ArgsParser parser = new ArgsParser(args);

Parameter<int[]> integers = parser.addIntegerArrayParameter("integer", "f", "descr", true);
try {
parser.parse();
} catch (Exception e) {
e.printStackTrace();
}

int[] result = integers.getArgument();
int[] expected = new int[] {1, 2, 3};

assertArrayEquals(expected, result);
}

@Test
public void testDoubleArrayParameter() {
String[] args = {"--double", "1.34", "2.45", "3.56"};
ArgsParser parser = new ArgsParser(args);

Parameter<double[]> doubles = parser.addDoubleArrayParameter("double", "f", "descr", true);
try {
parser.parse();
} catch (Exception e) {
e.printStackTrace();
}

double[] result = doubles.getArgument();
double[] expected = new double[] {1.34, 2.45, 3.56};
assertArrayEquals(expected, result);
}

@Test
public void testBooleanArrayParameter() {
String[] args = {"--boolean", "true", "false"};
ArgsParser parser = new ArgsParser(args);

Parameter<Boolean[]> booleans = parser.addBooleanArrayParameter("boolean", "f", "descr", true);
try {
parser.parseUnchecked();
} catch (Exception e) {
e.printStackTrace();
}

Boolean[] result = booleans.getArgument();
Boolean[] expected = new Boolean[] {true, false};
assertArrayEquals(expected, result);
}

@Test
public void testCharacterArrayParameter() {
String[] args = {"--character", "a", "b", "c"};
ArgsParser parser = new ArgsParser(args);

Parameter<char[]> characters = parser.addCharacterArrayParameter("character", "f", "descr", true);
try {
parser.parse();
} catch (Exception e) {
e.printStackTrace();
}

char[] result = characters.getArgument();
char[] expected = new char[] {'a', 'b', 'c'};
assertArrayEquals(expected, result);
}

@Test
public void testArrayParameterWithMultipleFlags() {
String[] args = {"--file", "file1.txt", "file2.txt", "file3.txt", "-str", "vierzig"};
ArgsParser parser = new ArgsParser(args);

Parameter<String[]> files = parser.addStringArrayParameter("file", "f", "descr", true);
Parameter<String> stringNumber = parser.addStringParameter("string", "str", "descr", true);
parser.parse();

String[] result = files.getArgument();
String[] expected = new String[] {"file1.txt", "file2.txt", "file3.txt"};
assertArrayEquals(expected, result);
}

@Test
public void testTwoArrayParameterBetweenNormalParameters() {
String[] args = {"--text", "hello friend", "--file", "file1.txt", "file2.txt", "file3.txt", "-n", "12.4", "13.5", "--end", "testEnd"};
ArgsParser parser = new ArgsParser(args);

Parameter<String> text = parser.addStringParameter("text", "t", "descr", true);
Parameter<String[]> files = parser.addStringArrayParameter("file", "f", "descr", true);
Parameter<double[]> doubles = parser.addDoubleArrayParameter("double", "n", "descr", true);
Parameter<String> end = parser.addStringParameter("end", "e", "descr", true);
parser.parse();

double[] result = doubles.getArgument();
double[] expected = new double[] {12.4, 13.5};
assertArrayEquals(expected, result);
}

// tests for the checked parse() method

// @Test
Expand Down