Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda