System.CommandLine 参数未传递给处理程序
System.CommandLine argument not being passed to handler
我正在努力掌握 System.CommandLine。
我可以在命令行上解析布尔选项,例如 -x
并且我可以传递字符串选项,例如 -f myfile
但是我遇到的问题是可以选择传递的字符串参数到程序 - 不是一个选项。
解析器在某种程度上理解了参数。如果我在命令行上提供零个或一个字符串,则不会出现错误。如果我提供额外的字符串,则会收到错误“无法识别的命令或参数”。但是,在处理程序中,参数始终为空。即使我指定了默认值并且未将字符串标记为可为空。
我正在使用的沙坑代码如下。谁能建议我做错了什么?我在其他地方发现了很多关于选项的讨论,但关于参数的讨论却很少。
using System.CommandLine;
using System.CommandLine.IO;
using System.CommandLine.NamingConventionBinder;
namespace ConsoleApp1
{
public static class Program
{
public static int Main(string[] args)
{
RootCommand _cmd = new()
{
new Option<bool>(new[] { "--immediatecalc", "-c" }, () => { return false; }, "Automatically run calculations when a file is loaded"),
new Option<bool>(new[] { "--autoexit", "-x" }, () => { return false; }, "Exit after carrying out directed tasks"),
new Option<bool>(new[] { "--saveJSON", "-s" }, () => { return false; }, "Export on deck JSON file after calculating"),
new Option<string?>(new[] { "--JSONfile", "-f" }, () => { return null; }, "File name for exported JSON file"),
new Argument<string?>("Filename", () => { return null; }, "The file to load.")
};
_cmd.Handler = CommandHandler.Create<bool, bool, bool, string?, string?, IConsole>(Handler);
return _cmd.Invoke(args);
}
static void Handler(bool immediatecalc, bool autoexit, bool saveJSON, string? jsonFile, string? sourceFile, IConsole console)
{
console.WriteLine("Immediate calc " + immediatecalc);
console.WriteLine("Autoexit " + autoexit);
console.WriteLine("saveJSON " + saveJSON);
console.WriteLine("Json file is " + jsonFile);
console.WriteLine("Source file is " + sourceFile);
}
}
}
这就是我使用最新的 System.CommandLine
版本 2.0.0-beta3.22114.1
进行设置的方式
using System.CommandLine;
namespace ConsoleApp1
{
public static class Program
{
public static int Main(string[] args)
{
var immediateCalcOption = new Option<bool>(new[] { "--immediatecalc", "-c" }, () => { return false; }, "Automatically run calculations when a file is loaded");
var autoExitOption = new Option<bool>(new[] { "--autoexit", "-x" }, () => { return false; }, "Exit after carrying out directed tasks");
var saveJsonOption = new Option<bool>(new[] { "--saveJSON", "-s" }, () => { return false; }, "Export on deck JSON file after calculating");
var jsonFileOption = new Option<string?>(new[] { "--JSONfile", "-f" }, () => { return null; }, "File name for exported JSON file");
var fileNameArgument = new Argument<string?>("Filename", () => { return null; }, "The file to load.");
RootCommand _cmd = new()
{
immediateCalcOption,
autoExitOption,
saveJsonOption,
jsonFileOption,
fileNameArgument
};
_cmd.SetHandler<bool, bool, bool, string?, string?, IConsole>(Handler, immediateCalcOption, autoExitOption, saveJsonOption, jsonFileOption, fileNameArgument);
return _cmd.Invoke(args);
}
static void Handler(bool immediatecalc, bool autoexit, bool saveJSON, string? jsonFile, string? sourceFile, IConsole console)
{
console.WriteLine("Immediate calc " + immediatecalc);
console.WriteLine("Autoexit " + autoexit);
console.WriteLine("saveJSON " + saveJSON);
console.WriteLine("Json file is " + jsonFile);
console.WriteLine("Source file is " + sourceFile);
}
}
}
这对我有用 args 输入 filename -c -x -s -f hello
给了我这个输出
Immediate calc True
Autoexit True
saveJSON True
Json file is hello
Source file is filename
我正在努力掌握 System.CommandLine。
我可以在命令行上解析布尔选项,例如 -x
并且我可以传递字符串选项,例如 -f myfile
但是我遇到的问题是可以选择传递的字符串参数到程序 - 不是一个选项。
解析器在某种程度上理解了参数。如果我在命令行上提供零个或一个字符串,则不会出现错误。如果我提供额外的字符串,则会收到错误“无法识别的命令或参数”。但是,在处理程序中,参数始终为空。即使我指定了默认值并且未将字符串标记为可为空。
我正在使用的沙坑代码如下。谁能建议我做错了什么?我在其他地方发现了很多关于选项的讨论,但关于参数的讨论却很少。
using System.CommandLine;
using System.CommandLine.IO;
using System.CommandLine.NamingConventionBinder;
namespace ConsoleApp1
{
public static class Program
{
public static int Main(string[] args)
{
RootCommand _cmd = new()
{
new Option<bool>(new[] { "--immediatecalc", "-c" }, () => { return false; }, "Automatically run calculations when a file is loaded"),
new Option<bool>(new[] { "--autoexit", "-x" }, () => { return false; }, "Exit after carrying out directed tasks"),
new Option<bool>(new[] { "--saveJSON", "-s" }, () => { return false; }, "Export on deck JSON file after calculating"),
new Option<string?>(new[] { "--JSONfile", "-f" }, () => { return null; }, "File name for exported JSON file"),
new Argument<string?>("Filename", () => { return null; }, "The file to load.")
};
_cmd.Handler = CommandHandler.Create<bool, bool, bool, string?, string?, IConsole>(Handler);
return _cmd.Invoke(args);
}
static void Handler(bool immediatecalc, bool autoexit, bool saveJSON, string? jsonFile, string? sourceFile, IConsole console)
{
console.WriteLine("Immediate calc " + immediatecalc);
console.WriteLine("Autoexit " + autoexit);
console.WriteLine("saveJSON " + saveJSON);
console.WriteLine("Json file is " + jsonFile);
console.WriteLine("Source file is " + sourceFile);
}
}
}
这就是我使用最新的 System.CommandLine
版本 2.0.0-beta3.22114.1
using System.CommandLine;
namespace ConsoleApp1
{
public static class Program
{
public static int Main(string[] args)
{
var immediateCalcOption = new Option<bool>(new[] { "--immediatecalc", "-c" }, () => { return false; }, "Automatically run calculations when a file is loaded");
var autoExitOption = new Option<bool>(new[] { "--autoexit", "-x" }, () => { return false; }, "Exit after carrying out directed tasks");
var saveJsonOption = new Option<bool>(new[] { "--saveJSON", "-s" }, () => { return false; }, "Export on deck JSON file after calculating");
var jsonFileOption = new Option<string?>(new[] { "--JSONfile", "-f" }, () => { return null; }, "File name for exported JSON file");
var fileNameArgument = new Argument<string?>("Filename", () => { return null; }, "The file to load.");
RootCommand _cmd = new()
{
immediateCalcOption,
autoExitOption,
saveJsonOption,
jsonFileOption,
fileNameArgument
};
_cmd.SetHandler<bool, bool, bool, string?, string?, IConsole>(Handler, immediateCalcOption, autoExitOption, saveJsonOption, jsonFileOption, fileNameArgument);
return _cmd.Invoke(args);
}
static void Handler(bool immediatecalc, bool autoexit, bool saveJSON, string? jsonFile, string? sourceFile, IConsole console)
{
console.WriteLine("Immediate calc " + immediatecalc);
console.WriteLine("Autoexit " + autoexit);
console.WriteLine("saveJSON " + saveJSON);
console.WriteLine("Json file is " + jsonFile);
console.WriteLine("Source file is " + sourceFile);
}
}
}
这对我有用 args 输入 filename -c -x -s -f hello
给了我这个输出
Immediate calc True
Autoexit True
saveJSON True
Json file is hello
Source file is filename