Skip to content

Commit 89a8e78

Browse files
committed
Display appropriate separator when writing default enumerable values in HelpText
Fix commandlineparser#490
1 parent 1e3607b commit 89a8e78

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Diff for: src/CommandLine/Text/HelpText.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,11 @@ specification is OptionSpecification optionSpecification &&
971971
if (addEnumValuesToHelpText && specification.EnumValues.Any())
972972
optionHelpText += " Valid values: " + string.Join(", ", specification.EnumValues);
973973

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

977980
var optionGroupSpecification = GetOptionGroupSpecification();
978981

@@ -1106,7 +1109,7 @@ private int GetMaxValueLength(ValueSpecification spec)
11061109
return specLength;
11071110
}
11081111

1109-
private static string FormatDefaultValue<T>(T value)
1112+
private static string FormatDefaultValue<T>(T value, char separator)
11101113
{
11111114
if (value is bool)
11121115
return value.ToStringLocal().ToLowerInvariant();
@@ -1122,7 +1125,7 @@ private static string FormatDefaultValue<T>(T value)
11221125
foreach (var item in asEnumerable)
11231126
builder
11241127
.Append(item.ToStringLocal())
1125-
.Append(" ");
1128+
.Append(separator);
11261129

11271130
return builder.Length > 0
11281131
? builder.ToString(0, builder.Length - 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
5+
namespace CommandLine.Tests.Fakes
6+
{
7+
class Options_With_Default_Set_To_Sequence_With_Separator
8+
{
9+
[Option('z', "strseq", Default = new[] { "a", "b", "c" }, Separator = ',')]
10+
public IEnumerable<string> StringSequence { get; set; }
11+
12+
[Option('y', "intseq", Default = new[] { 1, 2, 3 }, Separator = ',')]
13+
public IEnumerable<int> IntSequence { get; set; }
14+
15+
[Option('q', "dblseq", Default = new[] { 1.1, 2.2, 3.3 }, Separator = ',')]
16+
public IEnumerable<int> DoubleSequence { get; set; }
17+
}
18+
}

Diff for: tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,33 @@ public void Options_should_be_separated_by_spaces()
849849

850850
// Teardown
851851
}
852+
853+
[Fact]
854+
public void Options_should_be_separated_by_separator()
855+
{
856+
// Fixture setup
857+
var handlers = new CultureInfo("en-US").MakeCultureHandlers();
858+
var fakeResult =
859+
new NotParsed<Options_With_Default_Set_To_Sequence_With_Separator>(
860+
typeof(Options_With_Default_Set_To_Sequence_With_Separator).ToTypeInfo(),
861+
Enumerable.Empty<Error>()
862+
);
863+
864+
// Exercize system
865+
handlers.ChangeCulture();
866+
var helpText = HelpText.AutoBuild(fakeResult);
867+
handlers.ResetCulture();
868+
869+
// Verify outcome
870+
var text = helpText.ToString();
871+
var lines = text.ToLines().TrimStringArray();
872+
873+
lines[3].Should().Be("-z, --strseq (Default: a,b,c)");
874+
lines[5].Should().Be("-y, --intseq (Default: 1,2,3)");
875+
lines[7].Should().Be("-q, --dblseq (Default: 1.1,2.2,3.3)");
876+
877+
// Teardown
878+
}
852879

853880
[Fact]
854881
public void Options_Should_Render_OptionGroup_In_Parenthesis_When_Available()

0 commit comments

Comments
 (0)