Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetDefault and Callback when using DateTime results in 01-01-0001 output #90

Open
VisualBean opened this issue Nov 24, 2017 · 1 comment

Comments

@VisualBean
Copy link

We are using the following code

parser.Setup(arg => arg.ExecutionDate)
                .As('d', "date")
                .SetDefault(DateTime.Today)
                .Callback((date) =>
                {
                    if (date > DateTime.Today)
                        date = DateTime.Today;
                });
parser.Object;

When we are NOT providing the -d parser.Object returns ExecutionDate as 01-01-0001 and not datetime.today

@siywilliams
Copy link
Member

siywilliams commented Nov 25, 2017

Hi @VisualBean

The callback is there to allow you to do something with the final parsed value, it was the only way to return the parsed value in the first versions of fclp.

The callback shouldn't be used when using the Generic Fluent Command Line Parser which is what you are doing as it can replace the internal function being called as part of the builder object. I did add a comment on the xml documentation "Do no use this if you are using the Fluent Command Line Builder" for that function, but it can be easily missed and people probably don't know the difference between the builder and the original parser (I introduced builder for backwards compatibility).

As there is no built in API to validate a value I would suggest you move that functionality to your setter on the ExecutionDate property and remove the callback use.

public DateTime ExecutionDate
{
  get { return _executionDate; }
  set
  {
    if (value > DateTime.Today)
    {
      value = DateTime.Today;
    }
    _executionDate = value;
  }
}

static void Main(string[] args)
{
  var fclp = new FluentCommandLineParser();
  fclp.Setup(arg => arg.ExecutionDate)
        .As('d', "date")
        .SetDefault(DateTime.Today); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants