我正在为控制台应用程序开发 POC,并且在设置中使用 AddCommandLine 后,我正在努力从配置中检索命令行值。
csproj
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
节目班
public static class Program
{
public static async Task Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.RollingFile("Logs//log.txt")
.CreateLogger();
await CreateHostBuilder(args)
.Build()
.RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("settings.json", true, true);
config.AddCommandLine(args);
})
.ConfigureServices((hostcontext, services) =>
{
services.AddHostedService<ConsoleApp>();
});
}
ConsoleApp 类
public class ConsoleApp : IHostedService
{
private readonly IConfiguration config;
private readonly ILogger<ConsoleApp> log;
public ConsoleApp(IConfiguration configuration, ILogger<ConsoleApp> logger)
{
config = configuration;
log = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
var t = config.GetSection("Args");
Parser.Default.ParseArguments<DeleteOptions>(t)
.WithParsed<DeleteOptions>()
.WithNotParsed();
foreach (var c in config.AsEnumerable())
{
log.LogInformation($"{c.Key, -15}:{c.Value}");
}
log.LogInformation($"Completing Start Task");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
log.LogInformation($"Complete End Task");
return Task.CompletedTask;
}
}
foreach 循环之前的 Parser 部分无法编译,并且循环的输出不会打印出我添加的任何参数。
我知道一般建议var someValue = Configuration.GetValue<int>("MySetting:SomeValue");
参数在哪里是--MySetting=SomeValue
检索 cmd 行值的推荐方法。
我用作参数的值是delete -e CI -t depchpolestar -l de-DE
,当我查看我的配置对象时,我看到
这就是为什么我认为该行var t = config.GetSection("Args");
应该检索 args 数组。我也尝试过var t = config.GetValue<string[]>("Args");
,但似乎都没有。在我看来,配置对象的索引 4 是一个以“Args”为键的字符串数组
如何检索字符串数组以便将其传递给 CommandLineParser 的 ParseArguments 方法?
[编辑]一种解决方案:
我现在可以让参数通过,但这不是一个特别好的方法;如果我将参数构造为--delete "-e CI -t depchpolestar -l de-DE"
而不是delete -e CI -t depchpolestar -l de-DE
将以下代码添加到 ConsoleApp 类:
var args = config.GetValue<string>("delete");
string[] arguments = null;
if(!string.IsNullOrEmpty(args))
{
var tempArgs = args.Split(" ");
arguments = new string[tempArgs.Length + 1];
arguments[0] = "delete";
for(int i = 0; i < tempArgs.Length; ++i)
{
arguments[i + 1] = tempArgs[i];
}
}
Parser.Default.ParseArguments<DeleteOptions>(arguments)
.WithParsed<DeleteOptions>(async c => await c.Dowork())
.WithNotParsed(HandleParseError);
执行命中 DoWork 方法。很好,但 DeleteOptions.cs 定义了一个动词,目的是添加更多命令。所以要做更多的工作,但要走正确的路。
[编辑] 我也意识到我不需要添加AddCommandLine()
调用,因为它们是默认添加的。