7

使用命令行解析器库并具有具有默认值的列表或数组,默认值打印为(默认值:System.String[])。有没有办法让它显示实际的默认值?

所以随着

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

帮助文本打印为"(Default: System.String[]) ...". 我想说"(Default: { "eng" })"

4

1 回答 1

2

HelpText 因使用针对 DefaultValue 的通用格式化函数而受苦。

问题是(参考最新稳定版)在 HelpText.cs 的第 702

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

当前的开发分支(我认为可用)使用新的辅助私有方法(也从测试的角度涵盖)解决了它:

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();
}

要使用最新的开发分支:

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.

于 2013-03-05T08:45:01.817 回答