Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How to bind NodaTime.Duration using System.CommandLine?

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);
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!