Skip to content

Fix #420 - Check for value token before expanding enumerable types with separator char #672

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/CommandLine/Core/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static Result<IEnumerable<Token>, Error> ExplodeOptionList(
Tuple.Create(-1, '\0'))).SkipWhile(x => x.Item1 < 0).Memoize();

var exploded = tokens.Select((t, i) =>
replaces.FirstOrDefault(x => x.Item1 == i).ToMaybe()
replaces.FirstOrDefault(x => x.Item1 == i && t.Tag == TokenType.Value).ToMaybe()
.MapValueOrDefault(r => t.Text.Split(r.Item2).Select(Token.Value),
Enumerable.Empty<Token>().Concat(new[] { t })));

Expand Down
50 changes: 50 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue420Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using System.Collections.Generic;
using Xunit;

namespace CommandLine.Tests.Unit
{
public class Issue420Tests
{
// Test method (xUnit) which fails
[Fact]
public void parsing_of_enumerable_types_with_separator_chars_should_validate_value_is_present()
{
string[] args = { "-j", "1", "--categories", "--flag" };

ParserResult<Options> parsedOptions = Parser.Default.ParseArguments<Options>(args);

parsedOptions.Tag.Should().Be(ParserResultType.NotParsed);

ParserResult<OptionsWithSeparator> parsedOptionsWithSeparator = Parser.Default.ParseArguments<OptionsWithSeparator>(args);

parsedOptionsWithSeparator.Tag.Should().Be(ParserResultType.NotParsed);
}

// Options
internal class Options
{
[Option('f', "flag", HelpText = "Flag")]
public bool Flag { get; set; }

[Option('c', "categories", Required = false, HelpText = "Categories")]
public IEnumerable<string> Categories { get; set; }

[Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
public int JobId { get; set; }
}

// Options
internal class OptionsWithSeparator
{
[Option('f', "flag", HelpText = "Flag")]
public bool Flag { get; set; }

[Option('c', "categories", Required = false, Separator = ',', HelpText = "Categories")]
public IEnumerable<string> Categories { get; set; }

[Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
public int JobId { get; set; }
}
}
}