fclp / fluent-command-line-parser

A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface
Other
530 stars 85 forks source link

Feature: Late Binding or SetDefault method #93

Open caverna opened 6 years ago

caverna commented 6 years ago

Hi! I have this situation:

private DateTime _firstDate;
private DateTime _lastDate;

private FluentCommandLineParser CreateParser()
{
    var parser = new FluentCommandLineParser();

    parser.SetupHelp("?", "help")
        .Callback(text => Console.WriteLine(text));

    parser
        .Setup<DateTime>('d', "FirstDate")
        .Callback(value => _firstDate = value.Date)
        .SetDefault(DateTime.Today);

    parser
        .Setup<DateTime>('l', "LastDate")
        .Callback(value => _lastDate = value)
        .SetDefault(_firstDate); // this should be a kind of 'late binding'!

    return parser;
}

The behaviour that I expect would be, when an user doesn't provide 'L', just get the 'D' value.

My current workaround is:

var result = parser.Parse(args);
if (_lastDate == new DateTime())
    _lastDate = _firstDate;
siywilliams commented 6 years ago

Hi @caverna

I can't see your proposed "late-binding" being required in many scenarios, however it might be easy to add a generic func overload for the SetDefault method which can be alternatively used in your particular scenario.

So for example,

parser.Setup<DateTime>('l', "LastDate")
      .Callback(value => _lastDate = value)
      .SetDefault(() => _firstDate); // 'late binding' achieved using new overload