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
41 changes: 15 additions & 26 deletions src/ArgsParser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ public class ArgsParser {

/**
* Constructor
* @param args arguments given to the program
* @throws IllegalArgumentException if args is null
*/
public ArgsParser(String[] args) {
public ArgsParser(String[] args) throws IllegalArgumentException {
if (args == null) throw new IllegalArgumentException("Args cannot be null!");
this.args = args;
}

Expand Down Expand Up @@ -420,7 +423,7 @@ public void parse() {
* <li>goes through the args given to the ArgsParser and assigns each parameter its argument, making it callable via flags</li>
* <li>checks if all mandatory parameters were given in the args
* </ul>
* @throws NoArgumentsProvidedArgsException if no arguments were provided in args
* @throws NoArgumentsProvidedArgsException if no arguments were provided in args and mandatory params were defined
* @throws UnknownFlagArgsException if an unknown flag was provided in args
* @throws TooManyArgumentsArgsException if more than one argument was provided to a single flag
* @throws MissingArgArgsException if a flag was provided without an argument
Expand All @@ -433,23 +436,25 @@ public void parseUnchecked() throws NoArgumentsProvidedArgsException, UnknownFla
CalledForHelpNotification, InvalidArgTypeArgsException {

parseArgsWasCalled = true;
checkIfAnyArgumentsProvided();
checkForHelpCall();
Set<Parameter<?>> givenParameters = parseArguments();
checkMandatoryArguments(givenParameters);

checkIfAnyArgumentsProvided();
if (args.length > 0) {
checkForHelpCall();
Set<Parameter<?>> givenParameters = parseArguments();
checkMandatoryArguments(givenParameters);
}
}

/**
* checks if any arguments were provided to the program
* Checks if any arguments were provided to the program.
* It allows the script
* to run without any arguments as long as no mandatory parameters were defined on this ArgsParser.
* @throws NoArgumentsProvidedArgsException if no arguments were provided in args
*/
private void checkIfAnyArgumentsProvided() throws NoArgumentsProvidedArgsException {
if (args == null || args.length == 0) {
if (args.length == 0 & !mandatoryParameters.isEmpty()) {
throw new NoArgumentsProvidedArgsException();
}
// allow run the script with no arguments if all mandatory parameters have a default which is then used
// if there is no mandatory parameter set, the program will run without any arguments
}

/**
Expand Down Expand Up @@ -555,20 +560,4 @@ private void checkMandatoryArguments(Set<Parameter<?>> givenParameters) throws M
public <T> T getArgumentOf(String fullFlag) throws ClassCastException {
return (T) parameterMap.get(makeFlag(fullFlag, false)).getArgument();
}

/**
* resets all fields of the ArgsParser class for testing purposes only!
*/
private void reset() {
// reset all fields
args = null;
parameterMap.clear();
mandatoryParameters.clear();
fullFlags.clear();
shortFlags.clear();
parseArgsWasCalled = false;
longestFlagSize = 0;
longestShortFlag = 0;
}

}
2 changes: 1 addition & 1 deletion src/ArgsParser/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected boolean hasArgument() {
* @throws IllegalStateException if {@link ArgsParser#parse()} was not called before trying to access this argument
*/
public T getArgument() throws IllegalStateException {
if (!argsParser.parseArgsWasCalled()) throw new IllegalStateException("parseArgs() was not called before trying to access the argument!");
if (!argsParser.parseArgsWasCalled()) throw new IllegalStateException("parse() was not called before trying to access the argument!");
if (!hasArgument && !hasDefault) return null;
return argument;
}
Expand Down
15 changes: 7 additions & 8 deletions src/test/TestArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public void testNoArgumentsProvided() {
Parameter<String> file = parser.addStringParameter("file", "f", " ",true);
try {
parser.parseUnchecked();
} catch (ArgsException e) {
} catch (Exception e) {
System.out.println(e.getMessage());
assertEquals(new NoArgumentsProvidedArgsException().getMessage(), e.getMessage());
} catch (CalledForHelpNotification e) {
System.out.println(e.getMessage());
return;
}
String result = file.getArgument();
}
Expand Down Expand Up @@ -78,6 +77,7 @@ public void testGetArgumentWithMultipleFlagsAndWrongInput() {
"> did you mean: --save ?\n" +
"\n" +
"> Use --help for more information.\n", e.getMessage());
return;
}
String result = file.getArgument();

Expand All @@ -101,6 +101,7 @@ public void testMissingShorts() {
"> did you mean: --file ?\n" +
"\n" +
"> Use --help for more information.\n", e.getMessage());
return;
}
String result = file.getArgument();
}
Expand All @@ -123,7 +124,6 @@ public void testSuggestionForFullFlag() {
"\n" +
"> Use --help for more information.\n", e.getMessage());
}
String result = file.getArgument();
}

@Test
Expand All @@ -144,7 +144,6 @@ public void testMissspelledHelp() {
"\n" +
"> Use --help for more information.\n", e.getMessage());
}
String result = file.getArgument();
}

@Test
Expand All @@ -160,7 +159,6 @@ public void testMissingArgument() {
System.out.println(e.getMessage());
assertEquals(new MissingArgArgsException("--file").getMessage(), e.getMessage());
}
String result = save.getArgument();
}

@Test
Expand All @@ -176,7 +174,6 @@ public void testMissingLastArgument() {
System.out.println(e.getMessage());
assertEquals(new MissingArgArgsException("--save").getMessage(), e.getMessage());
}
String result = save.getArgument();
}

@Test
Expand Down Expand Up @@ -481,6 +478,7 @@ public void testAddDoubleParameterDefaultValue() {
try {
parser.parseUnchecked();
} catch (Exception e) {
e.printStackTrace();
}
Double result = number.getArgument();
assertEquals(Double.valueOf(42.5), result);
Expand Down Expand Up @@ -611,13 +609,14 @@ public void testAddCharacterParameterDefaultValue() {
}

@Test
public void testStringDeafult() {
public void testStringDefault() {
ArgsParser parser = new ArgsParser(new String[] {});

Parameter<String> string = parser.addStringParameter("string", "s", "default");
try {
parser.parseUnchecked();
} catch (Exception e) {
System.out.print(e.getMessage());
}
String result = string.getArgument();
assertEquals("default", result);
Expand Down