diff --git a/README.md b/README.md index d7583e6..43b63b6 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,13 @@ You can specify several fields for each parameter: ``` ### 3. Parse the Arguments -Call the `parseArgs()` method after adding all parameters. -Catch possible `ArgsException` errors for common parsing +Call the `parse()` or `parseUnchecked()` method after adding all parameters. + +- The `parse()` method directly handles all ArgsExceptions and uses `System.exit()` if any invalid argument is +provided to the console or `--help` / `-h` was used. +- The `parseUnchecked()` method throws the exceptions, which you can catch and handle manually. + +The ArgsParser catches possible `ArgsException` errors for common parsing issues such as: - No arguments provided @@ -65,9 +70,15 @@ issues such as: A `CalledForHelpNotification` can also be thrown if the user requests the help message. Exit with status code 0 for help requests and 1 for errors is recommended. +The Code example look like this: +```Java + parser.parse(); +``` + +or like this for manually handling the ArgsExceptions: ```java try { - parser.parseArgs(); + parser.parseUnchecked(); } catch (CalledForHelpNotification help) { System.out.println(help.getMessage()); diff --git a/src/ArgsParser/ArgsParser.java b/src/ArgsParser/ArgsParser.java index 1bca2e2..da0a20f 100644 --- a/src/ArgsParser/ArgsParser.java +++ b/src/ArgsParser/ArgsParser.java @@ -49,7 +49,7 @@ of this software and associated documentation files (the "Software"), to deal *
  • Parameters can have a description
  • *
  • Parameters can be of type String, Integer, Double, Boolean or Character
  • * - *
  • After all parameters are added, the {@link #parseArgs()} method has to be called! (this is mandatory!)
  • + *
  • After all parameters are added, the {@link #parseUnchecked()} method has to be called! (this is mandatory!)
  • *
  • Then the arguments can be accessed by using {@link Parameter#getArgument()} on the specific Parameter variable * which will return the parsed argument of that parameter as the specified type
  • * @@ -401,6 +401,31 @@ public Parameter addCharacterParameter(String fullFlag, String shortF return createParameter(fullFlag, shortFlag, null, Character.class, false, defaultValue); } + /** + * + *

    Directly handles any ArgsException by printing the message to the console than exiting the program!

    + */ + public void parse() { + + try { + parseUnchecked(); + + } catch (CalledForHelpNotification help) { + System.out.println(help.getMessage()); + System.exit(0); + + } catch (ArgsException e) { + System.out.println(e.getMessage()); + System.exit(1); + } + + } + /** *