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

Fix #490: Display appropriate separator when writing default enumerable values #895

Open
wants to merge 1 commit into
base: master
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
9 changes: 6 additions & 3 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,11 @@ specification is OptionSpecification optionSpecification &&
if (addEnumValuesToHelpText && specification.EnumValues.Any())
optionHelpText += " Valid values: " + string.Join(", ", specification.EnumValues);

var separator = (specification is OptionSpecification optionSpecification && optionSpecification.Separator != '\0')
? optionSpecification.Separator
: ' ';
specification.DefaultValue.Do(
defaultValue => optionHelpText = "(Default: {0}) ".FormatInvariant(FormatDefaultValue(defaultValue)) + optionHelpText);
defaultValue => optionHelpText = "(Default: {0}) ".FormatInvariant(FormatDefaultValue(defaultValue, separator)) + optionHelpText);

var optionGroupSpecification = GetOptionGroupSpecification();

Expand Down Expand Up @@ -1106,7 +1109,7 @@ private int GetMaxValueLength(ValueSpecification spec)
return specLength;
}

private static string FormatDefaultValue<T>(T value)
private static string FormatDefaultValue<T>(T value, char separator)
{
if (value is bool)
return value.ToStringLocal().ToLowerInvariant();
Expand All @@ -1122,7 +1125,7 @@ private static string FormatDefaultValue<T>(T value)
foreach (var item in asEnumerable)
builder
.Append(item.ToStringLocal())
.Append(" ");
.Append(separator);

return builder.Length > 0
? builder.ToString(0, builder.Length - 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System.Collections.Generic;

namespace CommandLine.Tests.Fakes
{
class Options_With_Default_Set_To_Sequence_With_Separator
{
[Option('z', "strseq", Default = new[] { "a", "b", "c" }, Separator = ',')]
public IEnumerable<string> StringSequence { get; set; }

[Option('y', "intseq", Default = new[] { 1, 2, 3 }, Separator = ',')]
public IEnumerable<int> IntSequence { get; set; }

[Option('q', "dblseq", Default = new[] { 1.1, 2.2, 3.3 }, Separator = ',')]
public IEnumerable<int> DoubleSequence { get; set; }
}
}
27 changes: 27 additions & 0 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,33 @@ public void Options_should_be_separated_by_spaces()

// Teardown
}

[Fact]
public void Options_should_be_separated_by_separator()
{
// Fixture setup
var handlers = new CultureInfo("en-US").MakeCultureHandlers();
var fakeResult =
new NotParsed<Options_With_Default_Set_To_Sequence_With_Separator>(
typeof(Options_With_Default_Set_To_Sequence_With_Separator).ToTypeInfo(),
Enumerable.Empty<Error>()
);

// Exercize system
handlers.ChangeCulture();
var helpText = HelpText.AutoBuild(fakeResult);
handlers.ResetCulture();

// Verify outcome
var text = helpText.ToString();
var lines = text.ToLines().TrimStringArray();

lines[3].Should().Be("-z, --strseq (Default: a,b,c)");
lines[5].Should().Be("-y, --intseq (Default: 1,2,3)");
lines[7].Should().Be("-q, --dblseq (Default: 1.1,2.2,3.3)");

// Teardown
}

[Fact]
public void Options_Should_Render_OptionGroup_In_Parenthesis_When_Available()
Expand Down