Skip to content

Commit

Permalink
Some fixes to pos args parsing (#91)
Browse files Browse the repository at this point in the history
- Select correct target Java type to convert to based on the current
  positional argument being parsed
- Use correct title for both positional and non-positional arguments
  • Loading branch information
rvesse committed Apr 16, 2019
1 parent a7b105d commit d85581a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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.AbstractCommonRestriction;
import com.github.rvesse.airline.restrictions.ArgumentsRestriction;
import com.github.rvesse.airline.restrictions.OptionRestriction;
import com.github.rvesse.airline.types.TypeConverter;
Expand Down Expand Up @@ -166,12 +167,12 @@ public ParseState<T> withArgument(List<PositionalArgumentMetadata> positionalArg
String rawValue) {
// Are we still parsing positional arguments or are we on non-positional
// arguments?
boolean positional = positionalArgs.size() > 0 && parsedArguments.size() < positionalArgs.size();
int posIndex = positional ? parsedArguments.size() : -1;
boolean positional = positionalArgs.size() > 0 && parsedPositionalArgs.size() < positionalArgs.size();
int posIndex = positional ? parsedPositionalArgs.size() : -1;
if (!positional && arguments == null) {
return withUnparsedInput(rawValue);
}
PositionalArgumentMetadata posArg = positionalArgs.get(posIndex);
PositionalArgumentMetadata posArg = positional ? positionalArgs.get(posIndex) : null;
List<ArgumentsRestriction> restrictions = positional ? posArg.getRestrictions() : arguments.getRestrictions();

// Pre-validate
Expand All @@ -191,8 +192,9 @@ public ParseState<T> withArgument(List<PositionalArgumentMetadata> positionalArg
try {
TypeConverter converter = positional ? posArg.getTypeConverterProvider().getTypeConverter(posArg, this)
: arguments.getTypeConverterProvider().getTypeConverter(arguments, this);
Object value = converter.convert(positional ? posArg.getTitle() : arguments.getTitle().get(0),
arguments.getJavaType(), rawValue);
Class<?> javaType = positional ? posArg.getJavaType() : arguments.getJavaType();
String title = positional ? posArg.getTitle() : AbstractCommonRestriction.getArgumentTitle(this, arguments);
Object value = converter.convert(title, javaType, rawValue);

// Post-validate
for (ArgumentsRestriction restriction : restrictions) {
Expand Down Expand Up @@ -275,7 +277,7 @@ public OptionMetadata getCurrentOption() {
public List<Pair<OptionMetadata, Object>> getParsedOptions() {
return parsedOptions;
}

public List<Pair<PositionalArgumentMetadata, Object>> getParsedPositionalArguments() {
return parsedPositionalArgs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.github.rvesse.airline.args.ArgsPositional;
import com.github.rvesse.airline.args.ArgsPositionalGap;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.command.SingleCommandParser;

import static com.github.rvesse.airline.TestingUtil.singleCommandParser;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
Expand All @@ -13,26 +11,35 @@
import org.testng.Assert;
import org.testng.annotations.Test;

@SuppressWarnings("unused")
public class TestPositionalArgs {

@Test
public void positional_args_01() {
SingleCommand<ArgsPositional> parser = singleCommandParser(ArgsPositional.class);
assertFalse(parser.getCommandMetadata().getPositionalArguments().isEmpty());
assertEquals(parser.getCommandMetadata().getPositionalArguments().size(), 2);

PositionalArgumentMetadata posArg = parser.getCommandMetadata().getPositionalArguments().get(0);
assertEquals(posArg.getZeroBasedPosition(), 0);
assertEquals(posArg.getOneBasedPosition(), 1);
assertEquals(posArg.getJavaType(), String.class);

assertEquals(posArg.getTitle(), "File");

posArg = parser.getCommandMetadata().getPositionalArguments().get(1);
assertEquals(posArg.getZeroBasedPosition(), 1);
assertEquals(posArg.getOneBasedPosition(), 2);
assertEquals(posArg.getJavaType(), Integer.class);

assertEquals(posArg.getTitle(), "Mode");

ArgsPositional cmd = parser.parse("example.txt", "600", "extra");
assertEquals(cmd.file, "example.txt");
assertEquals(cmd.mode, new Integer(600));
assertEquals(cmd.parameters.size(), 1);
assertEquals(cmd.parameters.get(0), "extra");

}

@Test(expectedExceptions = IllegalStateException.class)
public void positional_args_gap_01() {
singleCommandParser(ArgsPositionalGap.class);
Expand Down

0 comments on commit d85581a

Please sign in to comment.