解析C#中的命令行参数/选项 [英] Parse command line arguments/options in C#
问题描述
我有一个带有一些参数和选项的控制台应用程序,所以我想使用免费的第三方库.
I have a console application with some arguments and options so I would like to use a free third-party library.
我为此找到了两个库: NDesk.Options 和
I have found two libraries for this purpose: NDesk.Options and Command Line Parser Library
最后,我决定使用命令行分析器库,因为使用属性可以使它更清晰,因此我下载了该库并添加了对它的引用.
Finally I have decided to use Command Line Parser Library because it is clearer using properties so I have downloaded it and added a reference to it.
问题是,当将引用添加到我的.NET Framework 3.5项目时,我得到一个警告图标.从上面下载了它的页面上,它说兼容性是.NET Framework 3.5+,所以我知道3.5是兼容的,对吗?如果不是,它的哪个早期版本与.NET Framework 3.5兼容?
The problem is that when adding the reference to my .NET Framework 3.5 project I get a warning icon. From the above page where I have downloaded it, it says that compatibility is .NET Framework 3.5+ so I understand 3.5 is compatible, am I right? If not which previous version of it is compatible with .NET Framework 3.5?
推荐答案
您还可以使用新的 Microsoft CommandLineUtils 库. nuget软件包在这里,但仅适用于.NET Core或Framrwork 4.5.2. 但是您可以下载源代码(仅7个文件)并包含在您的projet中.对于Framework 3.5,您只有2个编译错误要解决:删除一个额外的方法(使用Tasks)和删除一行(在HandleUnexpectedArg中).
You can also use the new Microsoft CommandLineUtils library. The nuget package is here, but only for .NET Core or Framrwork 4.5.2. But you can download the source code (only 7 files) and include in your projet. For the Framework 3.5, you have only 2 compilation errors to solve: remove an extra method (using Tasks) and remove one line (in HandleUnexpectedArg).
- Nuget: https://www.nuget.org/packages/Microsoft.Extensions .CommandLineUtils
- 来源: https://github.com /aspnet/Common/tree/dev/shared/Microsoft.Extensions.CommandLineUtils.Sources
- Nuget: https://www.nuget.org/packages/Microsoft.Extensions.CommandLineUtils
- Source: https://github.com/aspnet/Common/tree/dev/shared/Microsoft.Extensions.CommandLineUtils.Sources
要使用此库,请在此处找到第一个示例:
To use this library, find here a first sample:
static void Main(string[] args)
{
var cmd = new CommandLineApplication();
var argAdd = cmd.Option("-a | --add <value>", "Add a new item", CommandOptionType.SingleValue);
cmd.OnExecute(() =>
{
Console.WriteLine(argAdd.Value());
return 0;
});
cmd.HelpOption("-? | -h | --help");
cmd.Execute(args);
}
这篇关于解析C#中的命令行参数/选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!