7

Using Command Line Parser Library and having a list or array with a default value, the default value is printed as (Default: System.String[]). Is there any way to make it show the actual default values?

So with

[OptionList('l', "languages", Separator = ',', DefaultValue = new []{"eng"})]
public IList<string> Languages { get; set; }

the help text is printed as "(Default: System.String[]) ...". I'd like it to say "(Default: { "eng" })".

PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • parsing occur as expected? Is an help problem right? If yes, it could be an issue. But I'll investigate more. – jay Mar 04 '13 at 19:55
  • Correct. Parsing is successful, but the help text listing the value isn't very helpful. Judging by the code a ToString is made on the value inside `HelpText::AddOption`, which call `StringExtensions:FormatLocal`. – PHeiberg Mar 04 '13 at 19:59
  • @jay - In order to fix it, I guess a check has to be in place in the `AddOption` method that formats the help text as a list if it's an `IEnumerable` instance of primitive types. – PHeiberg Mar 04 '13 at 20:05
  • Thanks for the hint. An [issue](https://github.com/gsscoder/commandline/issues/65) was opened in github project. Updates will be posted here too. – jay Mar 04 '13 at 20:16
  • 2
    Just for reference: **2.0.x** uses `Default` instead of `DefaultValue`. And `OptionList` is no longer needed, just use `Option` with a valued `Seperator` when targeting `IEnumerable`. Docs [here](https://github.com/gsscoder/commandline/wiki/Latest-Version). – gsscoder Aug 06 '15 at 18:12

1 Answers1

2

HelpText suffered of using a generalized formatting function against DefaultValue.

The problem was (ref. to latest stable) in line 702 of HelpText.cs:

if (option.HasDefaultValue)
{
  option.HelpText = "(Default: {0}) ".FormatLocal(option.DefaultValue) + option.HelpText;
}

The current development branch (to my opinion usable) solves it with a new helper private method (covered also from a test perspective):

private static string FormatDefaultValue(object value)
{
    if (value is bool)
    {
        return value.ToLocalString().ToLowerInvariant();
    }

    if (value is string)
    {
        return value.ToLocalString();
    }

    var asEnumerable = value as IEnumerable;
    if (asEnumerable != null)
    {
        var builder = new StringBuilder();
        foreach (var item in asEnumerable)
        {
            builder.Append(item.ToLocalString());
            builder.Append(" ");
        }
        return builder.Length > 0 ? builder.ToString(0, builder.Length - 1) : string.Empty;
    }
    return value.ToLocalString();
}

To use the latest development branch:

git clone -b develop-1.9.8-beta https://github.com/gsscoder/commandline.git commandline-develop

For informations on its stability and how could change after first release, see here.

With this instructions should be easy also patch a fork of the current stable.

jay
  • 1,510
  • 2
  • 11
  • 19
  • 1
    I want to add that this fix was lost in **2.0.x version** (for various reasons of none interest here) and it's actually **re**_fixed_ in **2.0.223-beta** (quite stable to be used), available on [NuGet](https://goo.gl/P2QNzP) too. As always latest sources are here on [GitHub](https://goo.gl/KgsVAi) and **2.0.x** updated docs in this [wiki section](https://github.com/gsscoder/commandline/wiki/Latest-Version). – gsscoder Aug 03 '15 at 15:52
  • This unit test demonstrates the **fix**: https://github.com/gsscoder/commandline/blob/master/tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs#L434. – gsscoder Aug 03 '15 at 15:54