-
Notifications
You must be signed in to change notification settings - Fork 383
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
Please add support for DateOnly and TimeOnly types #1732
Comments
Also, counter-intuitively (IMHO), when providing defaults the exact opposite fails ( using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
internal class Program
{
public static async Task Main()
{
var root = new Command("Root");
var datecommand1 = new Command("testdate1") { Handler = CommandHandler.Create<DateTime>((date) => Console.WriteLine(date)) };
datecommand1.AddOption(new Option<DateOnly>(new[] { "--date", "-d" }, () => DateOnly.Parse("2999-12-31"), "Test date") { IsRequired = false });
var datecommand2 = new Command("testdate2") { Handler = CommandHandler.Create<DateOnly>((date) => Console.WriteLine(date)) };
datecommand2.AddOption(new Option<DateOnly>(new[] { "--date", "-d" }, () => DateOnly.Parse("2999-12-31"), "Test date") { IsRequired = false });
var timecommand1 = new Command("testtime1") { Handler = CommandHandler.Create<DateTime>((time) => Console.WriteLine(time)) };
timecommand1.AddOption(new Option<TimeOnly>(new[] { "--time", "-t" }, () => TimeOnly.Parse("23:59:59"), "Test time") { IsRequired = false });
var timecommand2 = new Command("testtime2") { Handler = CommandHandler.Create<TimeOnly>((time) => Console.WriteLine(time)) };
timecommand2.AddOption(new Option<TimeOnly>(new[] { "--time", "-t" }, () => TimeOnly.Parse("23:59:59"), "Test time") { IsRequired = false });
var datetimecommand = new Command("testdatetime") { Handler = CommandHandler.Create<DateTime>((datetime) => Console.WriteLine(datetime)) };
datetimecommand.AddOption(new Option<DateTime>(new[] { "--datetime", "-dt" }, () => DateTime.Parse("2999-12-31T23:59:59"), "Test time"));
var datetimeoffsetcommand = new Command("testdatetimeoffset") { Handler = CommandHandler.Create<DateTimeOffset>((datetimeoffset) => Console.WriteLine(datetimeoffset)) };
datetimeoffsetcommand.AddOption(new Option<DateTimeOffset>(new[] { "--datetimeoffset", "-dto" }, () => DateTimeOffset.Parse("2999-12-31T23:59:59Z"), "Test time"));
root.AddCommand(datecommand1);
root.AddCommand(datecommand2);
root.AddCommand(timecommand1);
root.AddCommand(timecommand2);
root.AddCommand(datetimecommand);
root.AddCommand(datetimeoffsetcommand);
var tests = new[]
{
new[] { "testdate1" },
new[] { "testdate2" },
new[] { "testtime1" },
new[] { "testtime2" },
new[] { "testdatetime" },
new[] { "testdatetimeoffset" }
};
foreach (var t in tests)
{
Console.WriteLine($"Test: {string.Join(" ", t)}");
await root.InvokeAsync(t);
}
}
} Test: testdate1
1-1-0001 00:00:00
Test: testdate2
31-12-2999
Test: testtime1
1-1-0001 00:00:00
Test: testtime2
23:59
Test: testdatetime
31-12-2999 23:59:59
Test: testdatetimeoffset
31-12-2999 23:59:59 +00:00 |
Not really counter-intuitive in my opnion, considering neither DateOnly, TimeOnly, nor DateTime have conversion operators allowing explicit/implicit conversions between these types (as far as i can tell, according to the documentation). The behavior i personally find surprising is presented in your first post. Due to the absence of said conversion operators, i would expect ( |
Counter-intuitive in the sense that the cases that failed first when given explicit values now succeed and vice versa. |
Yeah. In this sense, it's counter-intuitve. I was arguing from the perspective that testdate1 and testtime1 from your first post seemingly working is not being intuitive and expected at all, given the conflict between the options being of DateOnly/TimeOnly type and a handler parameter that is of a type (DateTime) which neither DateOnly nor TimeOnly can be explicitly/implictly converted into. |
It's worth pointing out that implicit conversions and support for built-in It would be clearer to base the expected versus actual behavior on the var dateOnlyOption = new Option<DateOnly>("-d");
var timeOnlyOption = new Option<TimeOnly>("-t");
var cmd = new RootCommand
{
dateOnlyOption,
timeOnlyOption
};
cmd.Parse("-d 2022-05-09").GetValueForOption(dateOnlyOption).Should().Be( /* ??? */ );
cmd.Parse("-t 1:39").GetValueForOption(timeOnlyOption).Should().Be( /* ??? */ );
This is likely the core of the issue. |
See the following example:
When run, this is the output:
The
testdate2
andtesttime2
"tests" seem to fail; the only difference is that we useDateOnly
/TimeOnly
for the generic type at theCommandHandler.Create
invocations. instead ofDateTime
The text was updated successfully, but these errors were encountered: