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
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down
29 changes: 27 additions & 2 deletions src/ArgsParser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ of this software and associated documentation files (the "Software"), to deal
* <li>Parameters can have a description</li>
* <li>Parameters can be of type String, Integer, Double, Boolean or Character</li>
* </ul>
* <li>After all parameters are added, the {@link #parseArgs()} method has to be called! (this is mandatory!)</li>
* <li>After all parameters are added, the {@link #parseUnchecked()} method has to be called! (this is mandatory!)</li>
* <li>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 </li>
* </ol>
Expand Down Expand Up @@ -401,6 +401,31 @@ public Parameter<Character> addCharacterParameter(String fullFlag, String shortF
return createParameter(fullFlag, shortFlag, null, Character.class, false, defaultValue);
}

/**
* <ul>
* <li>checks if args is Empty</li>
* <li>checks if --help or -h was called on the program</li>
* <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>
* <p>Directly handles any ArgsException by printing the message to the console than <strong>exiting the program!</strong></p>
*/
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);
}

}

/**
* <ul>
* <li>checks if args is Empty</li>
Expand All @@ -416,7 +441,7 @@ public Parameter<Character> addCharacterParameter(String fullFlag, String shortF
* @throws CalledForHelpNotification if --help or -h was called
* @throws InvalidArgTypeArgsException if the argument provided to a flag is not of the correct type
*/
public void parseArgs() throws NoArgumentsProvidedArgsException, UnknownFlagArgsException,
public void parseUnchecked() throws NoArgumentsProvidedArgsException, UnknownFlagArgsException,
TooManyArgumentsArgsException, MissingArgArgsException, MandatoryArgNotProvidedArgsException,
CalledForHelpNotification, InvalidArgTypeArgsException {

Expand Down
2 changes: 1 addition & 1 deletion src/ArgsParser/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected boolean hasArgument() {
/**
* getter method for the argument attribute
* @return argument as String
* @throws IllegalStateException if {@link ArgsParser#parseArgs()} was not called before trying to access this argument
* @throws IllegalStateException if {@link ArgsParser#parseUnchecked()} was not called before trying to access this argument
*/
public T getArgument() throws IllegalStateException {
if (!parser.parseArgsWasCalled) throw new IllegalStateException("parseArgs() was not called before trying to access the argument!");
Expand Down
Loading