Skip to content

Commit

Permalink
Start adding restriction support for positional arguments (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvesse committed Apr 16, 2019
1 parent 96cd226 commit 7476930
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package com.github.rvesse.airline.parser.errors;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.github.rvesse.airline.model.PositionalArgumentMetadata;

/**
* Exception thrown when required arguments are missing
*
Expand All @@ -27,6 +30,11 @@ public class ParseArgumentsMissingException extends ParseRestrictionViolatedExce
private static final long serialVersionUID = 6220909299960264997L;

private final List<String> argumentTitles;

public ParseArgumentsMissingException(PositionalArgumentMetadata posArg) {
super("Required positional argument %d ('%s') is missing", posArg.getZeroBasedPosition(), posArg.getTitle());
this.argumentTitles = Collections.singletonList(posArg.getTitle());
}

public ParseArgumentsMissingException(List<String> argumentTitles) {
super("Required arguments are missing: '%s'", StringUtils.join(argumentTitles, ','));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
Expand All @@ -41,6 +42,12 @@ public final <T> void preValidate(ParseState<T> state, ArgumentsMetadata argumen
throw violated(state, arguments, value);
}

@Override
public final <T> void preValidate(ParseState<T> state, PositionalArgumentMetadata arguments, String value) {
if (!isValid(value))
throw violated(state, arguments, value);
}

/**
* Method that derived classes must implement to check whether a value is
* valid
Expand All @@ -59,7 +66,7 @@ public final <T> void preValidate(ParseState<T> state, ArgumentsMetadata argumen
* @param state
* Parser state
* @param option
* Option metadata for the option whose value is being checked
* Option meta-data for the option whose value is being checked
* @param value
* Value which has been deemed invalid
* @return Exception
Expand All @@ -75,11 +82,27 @@ protected abstract <T> ParseRestrictionViolatedException violated(ParseState<T>
* @param state
* Parser state
* @param arguments
* Arguments metadata
* Arguments meta-data
* @param value
* Value which has been deemed invalid
* @return Exception
*/
protected abstract <T> ParseRestrictionViolatedException violated(ParseState<T> state, ArgumentsMetadata arguments,
String value);

/**
* Method that derived classes must implement to provide an exception for
* the case of an invalid argument value, this will be called if
* {@link #isValid(String)} returns {@code false}
*
* @param state
* Parser state
* @param arguments
* Argument meta-data
* @param value
* Value which has been deemed invalid
* @return Exception
*/
protected abstract <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseArgumentsIllegalValueException;
import com.github.rvesse.airline.parser.errors.ParseOptionIllegalValueException;
Expand Down Expand Up @@ -66,4 +67,16 @@ public <T> void preValidate(ParseState<T> state, ArgumentsMetadata arguments, St
throw new ParseArgumentsIllegalValueException(AbstractCommonRestriction.getArgumentTitle(state, arguments), value, asObjects(rawValues));
}
}

@Override
public <T> void preValidate(ParseState<T> state, PositionalArgumentMetadata arguments, String value) {
// Not enforced if no values specified
if (rawValues.isEmpty())
return;

// Check in list of values
if (!this.rawValues.contains(value)) {
throw new ParseArgumentsIllegalValueException(arguments.getTitle(), value, asObjects(rawValues));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseArgumentsIllegalValueException;
import com.github.rvesse.airline.parser.errors.ParseOptionIllegalValueException;
Expand Down Expand Up @@ -65,4 +66,16 @@ public <T> void preValidate(ParseState<T> state, ArgumentsMetadata arguments, St
throw new ParseArgumentsIllegalValueException(AbstractCommonRestriction.getArgumentTitle(state, arguments), value, asObjects(rawValues));
}
}

@Override
public <T> void preValidate(ParseState<T> state, PositionalArgumentMetadata arguments, String value) {
// Not enforced if no values specified
if (rawValues.isEmpty())
return;

// Check in list of values
if (!this.rawValues.contains(value)) {
throw new ParseArgumentsIllegalValueException(arguments.getTitle(), value, asObjects(rawValues));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseArgumentsIllegalValueException;
import com.github.rvesse.airline.parser.errors.ParseInvalidRestrictionException;
Expand Down Expand Up @@ -92,4 +93,18 @@ public <T> void postValidate(ParseState<T> state, ArgumentsMetadata arguments, O
}
}

@Override
public <T> void postValidate(ParseState<T> state, PositionalArgumentMetadata arguments, Object value) {
// Not enforced if no values specified
if (this.rawValues.isEmpty())
return;

String title = arguments.getTitle();
Set<Object> allowedValues = createAllowedValues(state, title, arguments.getJavaType(),
arguments.getTypeConverterProvider().getTypeConverter(arguments, state));
if (!allowedValues.contains(value)) {
throw new ParseArgumentsIllegalValueException(title, value, allowedValues);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
Expand Down Expand Up @@ -66,6 +67,14 @@ protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, Ar
StringUtils.join(this.suffixes, ", "));
}

@Override
protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value) {
throw new ParseRestrictionViolatedException(
"Positional argument %d ('%s') has value '%s' which does not end with one of the permitted suffixes: %s",
arguments.getZeroBasedPosition(), arguments.getTitle(), value, StringUtils.join(this.suffixes, ", "));
}

@Override
public String getPreamble() {
return String.format("This options value must end with one of the following %s suffixes:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseArgumentsMissingException;
import com.github.rvesse.airline.parser.errors.ParseOptionMissingException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
import com.github.rvesse.airline.utils.AirlineUtils;
import com.github.rvesse.airline.utils.predicates.parser.ParsedOptionFinder;
import com.github.rvesse.airline.utils.predicates.parser.ParsedPositionalArgumentFinder;

/**
* A restriction that options/arguments are required
Expand All @@ -43,4 +45,11 @@ public <T> void finalValidate(ParseState<T> state, ArgumentsMetadata arguments)
throw new ParseArgumentsMissingException(arguments.getTitle());
}

@Override
public <T> void finalValidate(ParseState<T> state, PositionalArgumentMetadata arguments) {
if (IterableUtils.find(state.getParsedPositionalArguments(),
new ParsedPositionalArgumentFinder(arguments)) == null)
throw new ParseArgumentsMissingException(arguments);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseInvalidRestrictionException;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
Expand Down Expand Up @@ -127,6 +128,31 @@ protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, Ar
}
}

@Override
protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value) {
if (this.maximum) {
return new ParseRestrictionViolatedException(
"Positional Argument %d ('%s') was given value '%s' that has length %d which exceeds the maximum permitted length of %d",
arguments.getZeroBasedPosition(), arguments.getTitle(), value, value.length(), this.max);
} else if (this.range) {
if (this.min == this.max) {
return new ParseRestrictionViolatedException(
"Positional Argument %d ('%s') was given value '%s' that has length %d which exceeds the maximum permitted length of %d",
arguments.getZeroBasedPosition(), arguments.getTitle(), value, value.length(), this.max);
} else {
return new ParseRestrictionViolatedException(
"Positional Argument %d ('%s') was given value '%s' that has length %d which is not in the accepted length range of %d to %d characters",
arguments.getZeroBasedPosition(), arguments.getTitle(), value, value.length(), this.min,
this.max);
}
} else {
return new ParseRestrictionViolatedException(
"Positional Argument %d ('%s') was given value '%s' that has length %d which is below the minimum required length of %d",
arguments.getZeroBasedPosition(), arguments.getTitle(), value, value.length(), this.min);
}
}

@Override
public String getPreamble() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
Expand All @@ -44,6 +45,13 @@ protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, Ar
return new ParseRestrictionViolatedException("Arguments '%s' requires a non-blank value but got value '%s'",
AbstractCommonRestriction.getArgumentTitle(state, arguments), value);
}

@Override
protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value) {
return new ParseRestrictionViolatedException("Positional argument %d ('%s') requires a non-blank value but got value '%s'",
arguments.getZeroBasedPosition(), arguments.getTitle(), value);
}

@Override
public String getPreamble() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
Expand All @@ -43,6 +44,13 @@ protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, Ar
return new ParseRestrictionViolatedException("Arguments '%s' requires a non-empty value",
AbstractCommonRestriction.getArgumentTitle(state, arguments));
}

@Override
protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value) {
return new ParseRestrictionViolatedException("Positional argument %d ('%s') requires a non-empty value",
arguments.getZeroBasedPosition(), arguments.getTitle());
}

@Override
public String getPreamble() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseArgumentsMissingException;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
Expand Down Expand Up @@ -76,6 +77,23 @@ public <T> void finalValidate(ParseState<T> state, ArgumentsMetadata arguments)
}
}

@Override
public <T> void finalValidate(ParseState<T> state, PositionalArgumentMetadata arguments) {
if (occurrences <= 0)
return;

// NB - Since a positional argument can only ever receive a single value
// setting occurrences to any value other than 1 makes no sense

if (maximum && state.getParsedArguments().size() > this.occurrences) {
throw new ParseTooManyArgumentsException("At most %d positional arguments may be specified but %d were found",
occurrences, state.getParsedArguments().size());
} else if (!maximum && state.getParsedArguments().size() < this.occurrences) {
throw new ParseArgumentsMissingException("At least %d positional arguments must be specified but only %d were found",
Collections.singletonList(arguments.getTitle()), this.occurrences, state.getParsedArguments().size());
}
}

private <T> List<String> titles(ParseState<T> state, ArgumentsMetadata arguments) {
if (state.getParsedArguments().size() >= arguments.getTitle().size())
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.model.ArgumentsMetadata;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.errors.ParseRestrictionViolatedException;
import com.github.rvesse.airline.restrictions.AbstractCommonRestriction;
Expand Down Expand Up @@ -65,6 +66,15 @@ protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, Ar
AbstractCommonRestriction.getArgumentTitle(state, arguments), value,
StringUtils.join(this.prefixes, ", "));
}

@Override
protected <T> ParseRestrictionViolatedException violated(ParseState<T> state, PositionalArgumentMetadata arguments,
String value) {
throw new ParseRestrictionViolatedException(
"Positional argument %d ('%s') has value '%s' which does not end with one of the permitted prefixes: %s",
arguments.getZeroBasedPosition(), arguments.getTitle(), value,
StringUtils.join(this.prefixes, ", "));
}

@Override
public String getPreamble() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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.utils.predicates.parser;

import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.tuple.Pair;

import com.github.rvesse.airline.model.PositionalArgumentMetadata;

public class ParsedPositionalArgumentFinder implements Predicate<Pair<PositionalArgumentMetadata, Object>> {

private final PositionalArgumentMetadata posArg;

public ParsedPositionalArgumentFinder(PositionalArgumentMetadata posArg) {
this.posArg = posArg;
}

@Override
public boolean evaluate(Pair<PositionalArgumentMetadata, Object> parsedPosArg) {
if (parsedPosArg == null) return false;
if (this.posArg == null) return false;

return this.posArg.equals(parsedPosArg.getLeft());
}

}
Loading

0 comments on commit 7476930

Please sign in to comment.