diff --git a/airline-core/src/main/java/com/github/rvesse/airline/parser/ParseState.java b/airline-core/src/main/java/com/github/rvesse/airline/parser/ParseState.java index 4419ec98c..f09829209 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/parser/ParseState.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/parser/ParseState.java @@ -23,6 +23,7 @@ import com.github.rvesse.airline.model.GlobalMetadata; import com.github.rvesse.airline.model.OptionMetadata; import com.github.rvesse.airline.model.ParserMetadata; +import com.github.rvesse.airline.parser.errors.ParseException; import com.github.rvesse.airline.restrictions.ArgumentsRestriction; import com.github.rvesse.airline.restrictions.OptionRestriction; import com.github.rvesse.airline.utils.AirlineUtils; @@ -88,22 +89,41 @@ public ParseState popContext() { public ParseState withOptionValue(OptionMetadata option, String rawValue) { // Pre-validate for (OptionRestriction restriction : option.getRestrictions()) { - restriction.preValidate(this, option, rawValue); + try { + restriction.preValidate(this, option, rawValue); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + } } - // Convert value - Object value = this.parserConfig.getTypeConverter().convert(option.getTitle(), option.getJavaType(), rawValue); - - // Post-validate - for (OptionRestriction restriction : option.getRestrictions()) { - restriction.postValidate(this, option, value); + try { + // Convert value + Object value = this.parserConfig.getTypeConverter().convert(option.getTitle(), option.getJavaType(), + rawValue); + + // Post-validate + for (OptionRestriction restriction : option.getRestrictions()) { + try { + restriction.postValidate(this, option, value); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + } + } + + List> newOptions = AirlineUtils.listCopy(parsedOptions); + newOptions.add(Pair.of(option, value)); + + return new ParseState(global, parserConfig, group, command, newOptions, locationStack, parsedArguments, + currentOption, unparsedInput); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + + List newUnparsed = AirlineUtils.listCopy(unparsedInput); + newUnparsed.add(rawValue); + + return new ParseState(global, parserConfig, group, command, parsedOptions, locationStack, + parsedArguments, currentOption, newUnparsed); } - - List> newOptions = AirlineUtils.listCopy(parsedOptions); - newOptions.add(Pair.of(option, value)); - - return new ParseState(global, parserConfig, group, command, newOptions, locationStack, parsedArguments, - currentOption, unparsedInput); } public ParseState withGlobal(GlobalMetadata global) { @@ -134,23 +154,41 @@ public ParseState withOption(OptionMetadata option) { public ParseState withArgument(ArgumentsMetadata arguments, String rawValue) { // Pre-validate for (ArgumentsRestriction restriction : arguments.getRestrictions()) { - restriction.preValidate(this, arguments, rawValue); + try { + restriction.preValidate(this, arguments, rawValue); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + } } // Convert value - Object value = this.parserConfig.getTypeConverter().convert(arguments.getTitle().get(0), - arguments.getJavaType(), rawValue); - - // Post-validate - for (ArgumentsRestriction restriction : arguments.getRestrictions()) { - restriction.postValidate(this, arguments, value); + try { + Object value = this.parserConfig.getTypeConverter().convert(arguments.getTitle().get(0), + arguments.getJavaType(), rawValue); + + // Post-validate + for (ArgumentsRestriction restriction : arguments.getRestrictions()) { + try { + restriction.postValidate(this, arguments, value); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + } + } + + List newArguments = AirlineUtils.listCopy(parsedArguments); + newArguments.add(value); + + return new ParseState(global, parserConfig, group, command, parsedOptions, locationStack, newArguments, + currentOption, unparsedInput); + } catch (ParseException e) { + this.parserConfig.getErrorHandler().handleError(e); + + List newUnparsed = AirlineUtils.listCopy(unparsedInput); + newUnparsed.add(rawValue); + + return new ParseState(global, parserConfig, group, command, parsedOptions, locationStack, + parsedArguments, currentOption, newUnparsed); } - - List newArguments = AirlineUtils.listCopy(parsedArguments); - newArguments.add(value); - - return new ParseState(global, parserConfig, group, command, parsedOptions, locationStack, newArguments, - currentOption, unparsedInput); } public ParseState withUnparsedInput(String input) { diff --git a/airline-core/src/main/java/com/github/rvesse/airline/parser/command/SingleCommandParser.java b/airline-core/src/main/java/com/github/rvesse/airline/parser/command/SingleCommandParser.java index 3990eb329..6d4ae3b07 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/parser/command/SingleCommandParser.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/parser/command/SingleCommandParser.java @@ -27,7 +27,9 @@ import com.github.rvesse.airline.model.OptionMetadata; import com.github.rvesse.airline.model.ParserMetadata; import com.github.rvesse.airline.parser.AbstractCommandParser; +import com.github.rvesse.airline.parser.ParseResult; import com.github.rvesse.airline.parser.ParseState; +import com.github.rvesse.airline.parser.errors.ParseException; import com.github.rvesse.airline.restrictions.ArgumentsRestriction; import com.github.rvesse.airline.restrictions.GlobalRestriction; import com.github.rvesse.airline.restrictions.OptionRestriction; @@ -35,7 +37,7 @@ public class SingleCommandParser extends AbstractCommandParser { - public T parse(ParserMetadata parserConfig, CommandMetadata commandMetadata, + public ParseResult parseWithResult(ParserMetadata parserConfig, CommandMetadata commandMetadata, Iterable restrictions, Iterable args) { if (args == null) throw new NullPointerException("args is null"); @@ -43,6 +45,15 @@ public T parse(ParserMetadata parserConfig, CommandMetadata commandMetadata, ParseState state = tryParse(parserConfig, commandMetadata, args); validate(state, IteratorUtils.toList(restrictions.iterator())); + return state.getParserConfiguration().getErrorHandler().finished(state); + + } + + public T parse(ParserMetadata parserConfig, CommandMetadata commandMetadata, + Iterable restrictions, Iterable args) { + ParseResult result = parseWithResult(parserConfig, commandMetadata, restrictions, args); + + ParseState state = result.getState(); CommandMetadata command = state.getCommand(); //@formatter:off @@ -72,7 +83,11 @@ protected void validate(ParseState state, List restriction for (GlobalRestriction restriction : restrictions) { if (restriction == null) continue; - restriction.validate(state); + try { + restriction.validate(state); + } catch (ParseException e) { + state.getParserConfiguration().getErrorHandler().handleError(e); + } } CommandMetadata command = state.getCommand(); if (command != null) { @@ -82,7 +97,11 @@ protected void validate(ParseState state, List restriction for (ArgumentsRestriction restriction : arguments.getRestrictions()) { if (restriction == null) continue; - restriction.finalValidate(state, arguments); + try { + restriction.finalValidate(state, arguments); + } catch (ParseException e) { + state.getParserConfiguration().getErrorHandler().handleError(e); + } } } @@ -93,7 +112,11 @@ protected void validate(ParseState state, List restriction for (OptionRestriction restriction : option.getRestrictions()) { if (restriction == null) continue; - restriction.finalValidate(state, option); + try { + restriction.finalValidate(state, option); + } catch (ParseException e) { + state.getParserConfiguration().getErrorHandler().handleError(e); + } } } }