forked from airlift/airline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing required option doesn't throw
This is a proposed fix for: airlift#22 This approach introduces a ParseResult object which allows for the collection of multiple parse errors from the user input and then provides this back to the caller to decide what to do. E.G. send the user the to the help dialog. This keeps the existing API and fits with the desire to keep the parsing logic agnostic to the details of the HelpOption. The existing pattern still fails fast with an exception: ```java MyCommand command = SingleCommand.singleCommand(MyCommand.class).parse(args); ``` This fix introduces the following pattern from the caller's perspective: ```java ParseResult parseResult = new ParseResult(); MyCommand command = SingleCommand.singleCommand(MyCommand.class).parse(args, parseResult); if (parseResult.hasErrors()) { command.helpOption.showHelpWithErrors(parseResult.errors()); } ```
- Loading branch information
Showing
3 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.airlift.airline; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class ParseResult { | ||
private List<ParseException> errors = new LinkedList<>(); | ||
|
||
public void addError(ParseException error) { | ||
errors.add(error); | ||
} | ||
|
||
public boolean hasErrors() { | ||
return errors.size() != 0; | ||
} | ||
|
||
public List<ParseException> getErrors() { | ||
return ImmutableList.copyOf(errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters