Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly assign arguments after a double dash to values #610

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/CommandLine/Core/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private static IEnumerable<Token> OfSequence(this IEnumerable<Token> tokens, Tok
return info.NextValue.MapValueOrDefault(
_ => info.MaxItems.MapValueOrDefault(
n => tokens.Skip(nameIndex + 1).Take(n),
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue())),
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue()));
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue() && !v.IsValueForced())),
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue() && !v.IsValueForced()));
}
return new Token[] { };
}
Expand Down
28 changes: 25 additions & 3 deletions src/CommandLine/Core/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static Token Value(string text, bool explicitlyAssigned)
return new Value(text, explicitlyAssigned);
}

public static Token ValueForced(string text)
{
return new Value(text, false, true);
}

public TokenType Tag
{
get { return tag; }
Expand Down Expand Up @@ -80,23 +85,35 @@ public bool Equals(Name other)
class Value : Token, IEquatable<Value>
{
private readonly bool explicitlyAssigned;
private readonly bool forced;

public Value(string text)
: this(text, false)
: this(text, false, false)
{
}

public Value(string text, bool explicitlyAssigned)
: this(text, explicitlyAssigned, false)
{
}

public Value(string text, bool explicitlyAssigned, bool forced)
: base(TokenType.Value, text)
{
this.explicitlyAssigned = explicitlyAssigned;
this.forced = forced;
}

public bool ExplicitlyAssigned
{
get { return explicitlyAssigned; }
}

public bool Forced
{
get { return forced; }
}

public override bool Equals(object obj)
{
var other = obj as Value;
Expand All @@ -120,7 +137,7 @@ public bool Equals(Value other)
return false;
}

return Tag.Equals(other.Tag) && Text.Equals(other.Text);
return Tag.Equals(other.Tag) && Text.Equals(other.Text) && this.Forced == other.Forced;
}
}

Expand All @@ -135,5 +152,10 @@ public static bool IsValue(this Token token)
{
return token.Tag == TokenType.Value;
}

public static bool IsValueForced(this Token token)
{
return token.IsValue() && ((Value)token).Forced;
}
}
}
}
2 changes: 1 addition & 1 deletion src/CommandLine/Core/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Result<IEnumerable<Token>, Error> PreprocessDashDash(
if (arguments.Any(arg => arg.EqualsOrdinal("--")))
{
var tokenizerResult = tokenizer(arguments.TakeWhile(arg => !arg.EqualsOrdinal("--")));
var values = arguments.SkipWhile(arg => !arg.EqualsOrdinal("--")).Skip(1).Select(Token.Value);
var values = arguments.SkipWhile(arg => !arg.EqualsOrdinal("--")).Skip(1).Select(Token.ValueForced);
return tokenizerResult.Map(tokens => tokens.Concat(values));
}
return tokenizer(arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace CommandLine.Tests.Fakes
{
public class Options_With_Option_Sequence_And_Value_Sequence
{
[Option('o', "option-seq")]
public IEnumerable<string> OptionSequence { get; set; }

[Value(0)]
public IEnumerable<string> ValueSequence { get; set; }
}
}
20 changes: 20 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ public void Parse_options_with_double_dash()
// Teardown
}

[Fact]
public void Parse_options_with_double_dash_and_option_sequence()
{
var expectedOptions = new Options_With_Option_Sequence_And_Value_Sequence
{
OptionSequence = new[] { "option1", "option2", "option3" },
ValueSequence = new[] { "value1", "value2", "value3" }
};

var sut = new Parser(with => with.EnableDashDash = true);

// Exercize system
var result =
sut.ParseArguments<Options_With_Option_Sequence_And_Value_Sequence>(
new[] { "--option-seq", "option1", "option2", "option3", "--", "value1", "value2", "value3" });

// Verify outcome
((Parsed<Options_With_Option_Sequence_And_Value_Sequence>)result).Value.Should().BeEquivalentTo(expectedOptions);
}

[Fact]
public void Parse_options_with_double_dash_in_verbs_scenario()
{
Expand Down