.net core 如何使用自定义解析器调用CommandLineUtils ExecuteAsync?

.net core 如何使用自定义解析器调用CommandLineUtils ExecuteAsync?,.net-core,commandlineutils,.net Core,Commandlineutils,我想使用不支持开箱即用和异步执行方法的选项类型的属性 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() {

我想使用不支持开箱即用和异步执行方法的选项类型的属性

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 
    }
  }  
如果我使用的是字符串类型,则静态ExecuteAsync工作正常

var ret = CommandLineApplication.ExecuteAsync<Program>(args);
有人建议,是否可以将ExecuteAsync、属性选项和自定义解析器结合起来


到目前为止,我发现的解决方法是使用字符串选项并将它们解析为DateTime?在我的应用程序中。

CommandLineApplication.ExecuteAsync(args)是

using (var app = new CommandLineApplication<TApp>())
{
    app.SetContext(context);
    app.Conventions.UseDefaultConventions();
    return app.Execute(context.Arguments);
}
使用(var app=new CommandLineApplication())
{
app.SetContext(context);
app.Conventions.UseDefaultConventions();
返回app.Execute(context.Arguments);
}
(见附件)

看起来您需要添加
app.Conventions.UseDefaultConventions()
添加到您的
AddValueParsersAndExecute
方法

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) 
using (var app = new CommandLineApplication<TApp>())
{
    app.SetContext(context);
    app.Conventions.UseDefaultConventions();
    return app.Execute(context.Arguments);
}