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); command.helpOption.runOrShowHelp(parseResult, command); ``` The call to runOrShowHelp will: * Run the command if there are no parse errors and help has not been requsted * Will display help if it is requested regardless of whether there are parse errors * Will display parse errors and help when there are parse errors
- Loading branch information
Showing
4 changed files
with
110 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
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,36 @@ | ||
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<ParseException>(); | ||
|
||
public void addError(ParseException error) { | ||
errors.add(error); | ||
} | ||
|
||
public boolean hasErrors() { | ||
return errors.size() != 0; | ||
} | ||
|
||
public List<ParseException> getErrors() { | ||
return ImmutableList.copyOf(errors); | ||
} | ||
|
||
public String getErrorMessage() { | ||
if (errors.size() >= 1) { | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append("ERROR: Encountered problems parsing arguments:\n"); | ||
for (ParseException error : errors) { | ||
sb.append(" - ").append(error.getMessage()).append("\n"); | ||
} | ||
sb.append("\n"); | ||
return sb.toString(); | ||
} else { | ||
throw new IllegalStateException("There are no errors to build a message for."); | ||
} | ||
} | ||
} |
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