前两篇说到Command和Option,这篇说说Argument。Argument的用法和Option很像,只是他的构造和属性有些差别,再就是在运行输入时有区别,接下来看看实现。.
一个参数
//创建根命令var rootCommand = new RootCommand("这是一个命令行工具:旦猫");rootCommand.SetHandler(() =>{Console.WriteLine("欢迎使用《旦猫》");});//创建子命令 showvar showCommand = new Command("show", "显示一些信息");//创建参数 colorvar showArgument = new Argument<string>(name: "color", description: "设置输出信息的色彩"){//这里设置只有一个color参数Arity = ArgumentArity.ExactlyOne,};//添加参数到show命令中showCommand.AddArgument(showArgument);//设置命令show执行的动作,这是带上color参数,类型为stringshowCommand.SetHandler((string color) =>{Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color,true);Console.WriteLine($"这是《旦猫》的show命令");Console.ResetColor();}, showArgument);//添加命令show到 根命令中rootCommand.Add(showCommand);await rootCommand.InvokeAsync(args);
运行结果:

二个参数
如果当有多个参数时:
//创建根命令var rootCommand = new RootCommand("这是一个命令行工具:旦猫");rootCommand.SetHandler(() =>{Console.WriteLine("欢迎使用《旦猫》");});//创建子命令 showvar showCommand = new Command("show", "显示一些信息");//创建参数 colorvar colorArgument = new Argument<string>(name: "color", description: "设置输出信息的色彩"){//这里设置只有一个color参数Arity = ArgumentArity.ExactlyOne,};//创建参数 timesvar timesArgument = new Argument<int>(name: "times", description: "设置输出的次数"){//这里设置只有一个times参数Arity = ArgumentArity.ExactlyOne,};//添加参数到show命令中showCommand.AddArgument(colorArgument);showCommand.AddArgument(timesArgument);//设置命令show执行的动作,这是带上color参数,类型为stringshowCommand.SetHandler((string color, int times) =>{for (var i = 1; i <= times; i++){Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color, true);Console.WriteLine($"这是《旦猫》的show命令");}Console.ResetColor();}, colorArgument, timesArgument);//添加命令show到 根命令中rootCommand.Add(showCommand);await rootCommand.InvokeAsync(args);
运行结果:

参数输入值类型转换
在第一个例子的SetHandler中,我们做了string到ConsoleColor的类型转换,其实这里可以通过参数自带的构造函数参数parse来实现类型转换。代码如下:
//创建根命令var rootCommand = new RootCommand("这是一个命令行工具:旦猫");rootCommand.SetHandler(() =>{Console.WriteLine("欢迎使用《旦猫》");});//创建子命令 showvar showCommand = new Command("show", "显示一些信息");//创建参数 colorvar showArgument = new Argument<ConsoleColor>(name: "color", description: "设置输出信息的色彩", parse: ParseColor<ConsoleColor>){Arity = ArgumentArity.ExactlyOne,};//类型转换内置方法ConsoleColor ParseColor<ConsoleColor>(ArgumentResult result){var color = result.Tokens[0].Value;return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color.ToString(), true);}//添加参数到show命令中showCommand.AddArgument(showArgument);//设置命令show执行的动作,这是带上times参数,类型为ConsoleColorshowCommand.SetHandler((ConsoleColor color) =>{Console.ForegroundColor = color;Console.WriteLine($"这是《旦猫》的show命令");Console.ResetColor();}, showArgument);//添加命令show到 根命令中rootCommand.Add(showCommand);await rootCommand.InvokeAsync(args);
结果如下:

一个参数多个值
当一个参数多个值的时候,实现方式如下:
//创建根命令var rootCommand = new RootCommand("这是一个命令行工具:旦猫");rootCommand.SetHandler(() =>{Console.WriteLine("欢迎使用《旦猫》");});//创建子命令 showvar showCommand = new Command("show", "显示一些信息");//创建参数 colorvar showArgument = new Argument<ConsoleColor[]>(name: "color", description: "设置输出信息的色彩", parse: ParseColor<ConsoleColor>){Arity = ArgumentArity.OneOrMore,};//类型转换内置方法ConsoleColor[] ParseColor<ConsoleColor>(ArgumentResult result){var colors = new List<ConsoleColor>();foreach (var color in result.Tokens){colors.Add((ConsoleColor)Enum.Parse(typeof(ConsoleColor), color.Value.ToString(), true));}return colors.ToArray();}//添加参数到show命令中showCommand.AddArgument(showArgument);//设置命令show执行的动作,这是带上times参数,类型为ConsoleColor数组showCommand.SetHandler((ConsoleColor[] colors) =>{foreach (var color in colors){Console.ForegroundColor = color;Console.WriteLine($"这是《旦猫》的show命令");Console.ResetColor();}}, showArgument);//添加命令show到 根命令中rootCommand.Add(showCommand);await rootCommand.InvokeAsync(args);
结果:
