How to call CommandLineUtils ExecuteAsync with custom parser?

0

I want to use https://natemcmaster.github.io/CommandLineUtils/ using Attributes with option types not supported out of the box and Asynchronous Execute method.

class Program
{
    //--from-date="2018-01-01"  --to-date="2018-07-01"
    [Option()]
    public DateTime? FromDate { get; }
    [Option()]
    public DateTime? ToDate { get; }

    private async Task OnExecuteAsync()
    {
        //do the actual work 
    }
  }  

If I am using string types, the static ExecuteAsync works correctly.

var ret = CommandLineApplication.ExecuteAsync<Program>(args);

But if parameter is of type DateTime?, I have to add custom parser ( as suggested in Support passing Uri values #97)

I've created an app object,add parser, assigned OnExecute and call execute method

 private static int AddValueParsersAndExecute(string[] args)
    {
        var app = new CommandLineApplication<Program>();
        app.ValueParsers.AddOrReplace(new NullableDateTimeParser());
        app.OnExecute((Func<Task>) new Program().OnExecuteAsync);
        var ret = app.Execute(args);
        return ret;
    }

Unfortunately when I pass --from-date="2018-01-01" --to-date="2018-07-01" (and I tried different variants of option names) it causes an error

McMaster.Extensions.CommandLineUtils.CommandParsingException
  HResult=0x80131500
  Message=Unrecognized option '--from-date=2018-01-01'
  Source=McMaster.Extensions.CommandLineUtils
  StackTrace:
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.HandleUnexpectedArg(String argTypeName) in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 246
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessOption() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 162
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessNext() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 62
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.Process() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 35
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Parse(String[] args) in C:\projects\commandlineutils\src\CommandLineUtils\CommandLineApplication.cs:line 532
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) in C:\projects\commandlineutils\src\CommandLineUtils\CommandLineApplication.cs:line 611
   at BlobsProcessor.Program.AddValueParsersAndExecute(String[] args) 

Could anyone suggest, is it possible to combine ExecuteAsync, attribute options and custom parsers?

The workaround that I found so far it to have string options and parse them to DateTime? inside my application.

.net-core
commandlineutils
asked on Stack Overflow Aug 12, 2018 by Michael Freidgeim • edited Aug 12, 2018 by Michael Freidgeim

1 Answer

0

CommandLineApplication.ExecuteAsync<Program>(args) is sugar for

using (var app = new CommandLineApplication<TApp>())
{
    app.SetContext(context);
    app.Conventions.UseDefaultConventions();
    return app.Execute(context.Arguments);
}

(See https://github.com/natemcmaster/CommandLineUtils/blob/e65492d1270067087cfdb80f6266290af0a563c7/src/CommandLineUtils/CommandLineApplication.Execute.cs#L53-L58)

It looks like you need to add app.Conventions.UseDefaultConventions(); to your AddValueParsersAndExecute method.

answered on Stack Overflow Aug 20, 2018 by natemcmaster

User contributions licensed under CC BY-SA 3.0