Skip to content

Please add support for DateOnly and TimeOnly types #1732

@RobThree

Description

@RobThree

See the following example:

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" }, "Test date"));

        var datecommand2 = new Command("testdate2") { Handler = CommandHandler.Create<DateOnly>((date) => Console.WriteLine(date)) };
        datecommand2.AddOption(new Option<DateOnly>(new[] { "--date", "-d" }, "Test date"));

        var timecommand1 = new Command("testtime1") { Handler = CommandHandler.Create<DateTime>((time) => Console.WriteLine(time)) };
        timecommand1.AddOption(new Option<TimeOnly>(new[] { "--time", "-t" }, "Test time"));

        var timecommand2 = new Command("testtime2") { Handler = CommandHandler.Create<TimeOnly>((time) => Console.WriteLine(time)) };
        timecommand2.AddOption(new Option<TimeOnly>(new[] { "--time", "-t" }, "Test time"));

        var datetimecommand = new Command("testdatetime") { Handler = CommandHandler.Create<DateTime>((datetime) => Console.WriteLine(datetime)) };
        datetimecommand.AddOption(new Option<DateTime>(new[] { "--datetime", "-dt" }, "Test time"));

        var datetimeoffsetcommand = new Command("testdatetimeoffset") { Handler = CommandHandler.Create<DateTimeOffset>((datetimeoffset) => Console.WriteLine(datetimeoffset)) };
        datetimeoffsetcommand.AddOption(new Option<DateTimeOffset>(new[] { "--datetimeoffset", "-dto" }, "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", "-d", "2003-02-01" },
            new[] { "testdate2", "-d", "2003-02-01" },
            new[] { "testtime1", "-t", "12:34:56" },
            new[] { "testtime2", "-t", "12:34:56" },
            new[] { "testdatetime", "-dt", "2003-02-01T12:34:56" },
            new[] { "testdatetimeoffset", "-dto", "2003-02-01T12:34:56+02:00" }
        };


        foreach (var t in tests)
        {
            Console.WriteLine($"Test: {string.Join(" ", t)}");
            await root.InvokeAsync(t);
        }
    }
}

When run, this is the output:

est: testdate1 -d 2003-02-01
1-2-2003 00:00:00
Test: testdate2 -d 2003-02-01
1-1-0001
Test: testtime1 -t 12:34:56
9-5-2022 12:34:56
Test: testtime2 -t 12:34:56
00:00
Test: testdatetime -dt 2003-02-01T12:34:56
1-2-2003 12:34:56
Test: testdatetimeoffset -dto 2003-02-01T12:34:56+02:00
1-2-2003 12:34:56 +02:00

The testdate2 and testtime2 "tests" seem to fail; the only difference is that we use DateOnly / TimeOnly for the generic type at the CommandHandler.Create invocations. instead of DateTime

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions