diff --git a/src/ArgsParser/ArgsParser.java b/src/ArgsParser/ArgsParser.java index b7ac35f..9caef4e 100644 --- a/src/ArgsParser/ArgsParser.java +++ b/src/ArgsParser/ArgsParser.java @@ -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; } @@ -420,7 +423,7 @@ public void parse() { *
  • goes through the args given to the ArgsParser and assigns each parameter its argument, making it callable via flags
  • *
  • checks if all mandatory parameters were given in the args * - * @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 @@ -433,23 +436,25 @@ public void parseUnchecked() throws NoArgumentsProvidedArgsException, UnknownFla CalledForHelpNotification, InvalidArgTypeArgsException { parseArgsWasCalled = true; - checkIfAnyArgumentsProvided(); - checkForHelpCall(); - Set> givenParameters = parseArguments(); - checkMandatoryArguments(givenParameters); + checkIfAnyArgumentsProvided(); + if (args.length > 0) { + checkForHelpCall(); + Set> 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 } /** @@ -555,20 +560,4 @@ private void checkMandatoryArguments(Set> givenParameters) throws M public 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; - } - } diff --git a/src/ArgsParser/Parameter.java b/src/ArgsParser/Parameter.java index d92de10..2a81adc 100644 --- a/src/ArgsParser/Parameter.java +++ b/src/ArgsParser/Parameter.java @@ -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; } diff --git a/src/test/TestArgsParser.java b/src/test/TestArgsParser.java index d0effd8..2145239 100644 --- a/src/test/TestArgsParser.java +++ b/src/test/TestArgsParser.java @@ -18,11 +18,10 @@ public void testNoArgumentsProvided() { Parameter 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(); } @@ -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(); @@ -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(); } @@ -123,7 +124,6 @@ public void testSuggestionForFullFlag() { "\n" + "> Use --help for more information.\n", e.getMessage()); } - String result = file.getArgument(); } @Test @@ -144,7 +144,6 @@ public void testMissspelledHelp() { "\n" + "> Use --help for more information.\n", e.getMessage()); } - String result = file.getArgument(); } @Test @@ -160,7 +159,6 @@ public void testMissingArgument() { System.out.println(e.getMessage()); assertEquals(new MissingArgArgsException("--file").getMessage(), e.getMessage()); } - String result = save.getArgument(); } @Test @@ -176,7 +174,6 @@ public void testMissingLastArgument() { System.out.println(e.getMessage()); assertEquals(new MissingArgArgsException("--save").getMessage(), e.getMessage()); } - String result = save.getArgument(); } @Test @@ -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); @@ -611,13 +609,14 @@ public void testAddCharacterParameterDefaultValue() { } @Test - public void testStringDeafult() { + public void testStringDefault() { ArgsParser parser = new ArgsParser(new String[] {}); Parameter string = parser.addStringParameter("string", "s", "default"); try { parser.parseUnchecked(); } catch (Exception e) { + System.out.print(e.getMessage()); } String result = string.getArgument(); assertEquals("default", result);