Skip to content

Commit

Permalink
Display appropriate separator when writing default enumerable values …
Browse files Browse the repository at this point in the history
…in HelpText

Fix commandlineparser#490
  • Loading branch information
gfs committed Jul 27, 2023
1 parent 1e3607b commit 89a8e78
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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

0 comments on commit 89a8e78

Please sign in to comment.