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 a9a973ddc..9b476480f 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 @@ -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; @@ -166,12 +167,12 @@ public ParseState withArgument(List 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 restrictions = positional ? posArg.getRestrictions() : arguments.getRestrictions(); // Pre-validate @@ -191,8 +192,9 @@ public ParseState withArgument(List 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) { @@ -275,7 +277,7 @@ public OptionMetadata getCurrentOption() { public List> getParsedOptions() { return parsedOptions; } - + public List> getParsedPositionalArguments() { return parsedPositionalArgs; } diff --git a/airline-core/src/test/java/com/github/rvesse/airline/TestPositionalArgs.java b/airline-core/src/test/java/com/github/rvesse/airline/TestPositionalArgs.java index 6bd8dd992..fc669bee4 100644 --- a/airline-core/src/test/java/com/github/rvesse/airline/TestPositionalArgs.java +++ b/airline-core/src/test/java/com/github/rvesse/airline/TestPositionalArgs.java @@ -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; @@ -13,6 +11,7 @@ import org.testng.Assert; import org.testng.annotations.Test; +@SuppressWarnings("unused") public class TestPositionalArgs { @Test @@ -20,19 +19,27 @@ public void positional_args_01() { SingleCommand 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);