Home > OS >  How to bind NodaTime.Duration using System.CommandLine?
How to bind NodaTime.Duration using System.CommandLine?

Time:10-17

Is it possible to directly bind command line input to NodaTime.Duration (or System.TimeSpan) using System.CommandLine? Maybe by supplying a custom converter somewhere?

static void Main(string[] args)
{
    var rootCommand = new RootCommand
    {
        new Option<Duration>("--my-duration"),
    };

    rootCommand.Handler = CommandHandler.Create(
        (Duration myDuration) => { Console.WriteLine(myDuration); });

    rootCommand.InvokeAsync(args);
}

CodePudding user response:

I haven't worked with System.CommandLine much, and there's not much documentation around custom parsing, but I believe what you want is a ParseArgument<T>. Fortunately it's fairly easy to write that an extension method on the NodaTime IPattern<T> to create an instance of that.

Here's a not-very-thoroughly-tested extension method:

using NodaTime.Text;
using System.CommandLine.Parsing;

namespace Demo
{
    public static class PatternExtensions
    {
        public static ParseArgument<T> ToParseArgument<T>(this IPattern<T> pattern) =>
            result => ParseResult<T>(pattern, result);

        private static T ParseResult<T>(IPattern<T> pattern, ArgumentResult result)
        {
            // TODO: Check whether Tokens is actually what we want to use.
            if (result.Tokens.Count != 1)
            {
                result.ErrorMessage = "Only a single token can be parsed";
                return default;
            }
            var token = result.Tokens[0];
            var nodaResult = pattern.Parse(token.Value);
            if (nodaResult.Success)
            {
                return nodaResult.Value;
            }
            else
            {
                result.ErrorMessage = nodaResult.Exception.Message;
                return default;
            }
        }
    }
}

And code to use it:

using NodaTime;
using NodaTime.Text;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using Demo; // To make the extension method available

class Program
{
    static void Main(string[] args)
    {
        var rootCommand = new RootCommand
        {
            new Option<Duration>(
                "--my-duration",
                DurationPattern.JsonRoundtrip.ToParseArgument()),
        };

        rootCommand.Handler = CommandHandler.Create(
            (Duration myDuration) => Console.WriteLine(myDuration));

        rootCommand.Invoke(args);
    }
}

The pattern being parsed (DurationPattern.JsonRoundtrip) uses "number of hours" as the most significant part, whereas the default format (used by Console.WriteLine in this case) uses "number of days", so it's easy to tell that it really is parsing:

$ dotnet run -- --my-duration=27:23:45
1:03:23:45
  • Related