Skip to content

Commit

Permalink
Use error handler in other appropriate places (#53)
Browse files Browse the repository at this point in the history
Adopts use of ParserErrorHandler in other appropriate places
  • Loading branch information
rvesse committed Dec 1, 2016
1 parent 659069a commit 4ba810e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static <T> T createInstance(Class<?> type, Iterable<OptionMetadata> optio
new DefaultCommandFactory<T>());
}

@SuppressWarnings("unchecked")
public static <T> T injectOptions(T commandInstance, Iterable<OptionMetadata> options,
List<Pair<OptionMetadata, Object>> parsedOptions, ArgumentsMetadata arguments,
Iterable<Object> parsedArguments, Iterable<Accessor> metadataInjection, Map<Class<?>, Object> bindings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ public PeekingIterator<String> resolveAliases(PeekingIterator<String> tokens, Pa

do {
// Try to find an alias
AliasMetadata alias = CollectionUtils.find(state.getParserConfiguration().getAliases(), new AliasFinder(tokens.peek()));
AliasMetadata alias = CollectionUtils.find(state.getParserConfiguration().getAliases(),
new AliasFinder(tokens.peek()));

// Nothing further to do if no aliases found
if (alias == null)
return tokens;

// Check for circular references
if (!aliasesSeen.add(alias.getName())) {
throw new ParseAliasCircularReferenceException(alias.getName(), aliasesSeen);
// Handle the error and exit alias resolution
state.getParserConfiguration().getErrorHandler()
.handleError(new ParseAliasCircularReferenceException(alias.getName(), aliasesSeen));
return tokens;
}

// Can we override built-ins?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ public ParseState<T> parseOptions(PeekingIterator<String> tokens, ParseState<T>
// option parser
if (first)
return null;
throw new ParseOptionUnexpectedException("Short options style can not be used with option %s", option);

// Produce an error, can't use short style options with an option
// with an arity greater than one
// Return the modified state anyway as we don't want to retry
// processing this option in that case
state.getParserConfiguration().getErrorHandler().handleError(new ParseOptionUnexpectedException(
"Short options style can not be used with option %s as the arity was not 0 or 1", option));
return nextState;
}

// consume the current token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,19 @@ public ParseState<T> parseOptions(PeekingIterator<String> tokens, ParseState<T>

// Parse value as a list
List<String> listValues = getValues(list);
if (listValues.size() < option.getArity())
throw new ParseOptionMissingValueException(
if (listValues.size() < option.getArity()) {
state.getParserConfiguration().getErrorHandler().handleError(new ParseOptionMissingValueException(
"Too few option values received for option %s in list value '%s' (%d values expected but only found %d)",
option.getTitle(), option.getOptions().iterator().next(), list, option.getArity(),
listValues.size());
if (listValues.size() > option.getArity())
throw new ParseOptionUnexpectedException(
listValues.size()));
return state;
}
if (listValues.size() > option.getArity()) {
state.getParserConfiguration().getErrorHandler().handleError(new ParseOptionUnexpectedException(
"Too many option values received for option %s in list value '%s' (%d values expected but found %d)",
option.getOptions().iterator().next(), list, option.getArity(), listValues.size());
option.getOptions().iterator().next(), list, option.getArity(), listValues.size()));
return state;
}

// Parse individual values and assign to option
for (String value : listValues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ public ParseState<T> parseOptions(PeekingIterator<String> tokens, ParseState<T>
}

if (count != option.getArity()) {
throw new ParseOptionMissingValueException(
"Too few option values received for option %s (%d values expected but only found %d)",
option.getTitle(), option.getOptions().iterator().next(), option.getArity(), count);
state.getParserConfiguration().getErrorHandler()
.handleError(new ParseOptionMissingValueException(
"Too few option values received for option %s (%d values expected but only found %d)",
option.getTitle(), option.getOptions().iterator().next(), option.getArity(), count));
return state;

}
state = state.popContext();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2010-16 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rvesse.airline.parser.errors.handlers;

import org.testng.Assert;
Expand All @@ -9,6 +24,7 @@
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.ParseResult;
import com.github.rvesse.airline.parser.errors.ParseException;
import com.github.rvesse.airline.restrictions.Strings;

public class TestErrorHandlers {

Expand All @@ -31,4 +47,15 @@ public void errorHandlerCollectAll() {
Assert.assertFalse(result.wasSuccessful());
Assert.assertEquals(result.getErrors().size(), 1);
}

@Test
public void errorHandlerFailAll() {
try {
ParseResult<Strings> result = SingleCommand
.<Strings> singleCommand(Strings.class, this.<Strings> prepareParser(new FailAll()))
.parseWithResult("--not-empty", "", "--not-blank", " ");
} catch (ParseException e) {
Assert.assertEquals(e.getSuppressed().length, 2);
}
}
}

0 comments on commit 4ba810e

Please sign in to comment.