在 C# 中解析命令行参数
这篇文章将介绍使用 C# 编程语言解析命令行参数。我们将研究两种方法和策略来完成这一目标。
命令的标准格式是 commandName -argumentName argumentValue
,其中 commandName
是命令的名称。以 Linux 中使用的著名的 apt
命令为例。
apt install exiftool
在 C#
使用 CommandLineParser
中解析参数
你可以使用诸如 CommandLineParser
之类的库来为你执行任务,而不是手动解析命令和参数,因此你不必担心它。然后你的主要注意力可能会去把命令逻辑付诸行动。
我们将向你展示如何开发允许 CommandLineParser
接收参数并以以下方式执行你的命令的代码。
static void Main(string[] args) {
Parser.Default.ParseArguments<UpdateCommand, InstallCommand>(args).WithParsed<ICommand>(
t => t.Execute());
}
首先,你必须安装 NuGet
包,可以通过执行以下命令来完成。
Install-Package CommandLineParser
如果你的命令不存在,请为它们创建一个基类或接口。另一方面,构建接口使你可以在代码中仅使用一个 .WithParsed<ICommand>
。
public interface ICommand {
void Execute();
}
必须先添加 UpdateCommand
,然后添加 Verb
属性。此参数指示 CommandLineParser
在遇到单词 update
时生成一个 UpdateCommand
对象。
[Verb("update", HelpText = "updates your system to the newest version")]
public class UpdateCommand : ICommand {
public void Execute() {
Console.WriteLine("Updating the system");
}
}
此时放入 InstallCommand
和 install
命令的 message
参数。包括 Message
属性和 Option
属性作为直接结果。
此属性为 CommandLineParser
提供有关将参数转换为属性的说明。
[Verb("install", HelpText = "installing your software")]
public class InstallCommand : ICommand {
[Option("message", Required = true, HelpText = "installing the program")]
public string Message { get; set; }
public void Execute() {
Console.WriteLine($"Executing install for the : {Message}");
}
}
最后,将参数作为类型参数传递给 CommandLineParser
以解析它们。
在 C#
中使用 If-Else
语句解析参数
下面是解析命令行参数的示例。
static void Main(string[] args) {
if (!(args.Length != 0)) {
Console.WriteLine("arguments not correct");
return;
}
}
读取 static void Main(string[] args)
的行的目的是从命令行获取参数并将它们保存在 args
数组中。if (!(args.Length != 0))
行检查总参数的长度是否为 0。
如果长度为 0,则表明没有向程序提供命令行参数。
读取 Console.WriteLine("arguments not correct");
的行在屏幕上显示参数不正确,以便用户下次输入正确数量的参数。
var c = args[0];
var c = args[0];
line 创建变量名 c
并存储在命令行中传递的第一个参数。
if (c == "update") {
update();
} else if (c == "install") {
if (args.Length == 2)
install(args[1]);
} else {
Console.WriteLine("Invalid Command");
}
我们将确定变量 c
是否在通过命令行发送的参数的开头提供参数 update
。如果答案是肯定的,则调用 update()
函数。
install
条件检查 c
变量是否在其开头包含 install
参数,如果包含,则通过比较 args
的长度来确定它是否应该有两个参数变量为 2。
因此,我们通过命令行验证参数列表里面给它的参数。
如果 args
的长度为 2,它会调用 install()
函数,并将 args[1]
作为参数。
只要其他情况都不匹配,就会执行 else
情况,此时它将在屏幕上打印出 Invalid Command
。
static void update() {
Console.WriteLine("updating your system");
}
当 update
作为参数发送时,update()
函数会打印 "Updating your system"
。
static void install(string message) {
Console.WriteLine($"installing the program {message}");
}
当 install
作为参数发送时,install()
函数会打印 "Installing the program {message}"
。
完整源代码:
using System;
class CommandLineParser {
static void Main(string[] args) {
if ((args.Length == 0)) {
Console.WriteLine("arguments not correct");
return;
}
var c = args[0];
if (c == "update") {
update();
} else if (c == "install") {
if (args.Length == 2)
install(args[1]);
} else {
Console.WriteLine("Invalid Command");
}
}
static void update() {
Console.WriteLine("updating your system");
}
static void install(string message) {
Console.WriteLine($"installing the program {message}");
}
}
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径