在c#中使用Plossum命令行解析数组
本文关键字:命令行 数组 Plossum | 更新日期: 2023-09-27 18:07:52
我试图使用Plossum CommandLine
解析器来解析C#
中的数组,但它似乎不起作用。
下面的代码是我在源代码中找到的一个示例的精简版本:
using Plossum.CommandLine;
using System;
using System.Collections.Generic;
namespace PlossumCommandLine
{
[CommandLineManager(ApplicationName = "Example 2", Copyright = "Copyright (C) Peter Palotas 2007",
EnabledOptionStyles = OptionStyles.Group | OptionStyles.LongUnix)]
[CommandLineOptionGroup("commands", Name = "Commands", Require = OptionGroupRequirement.ExactlyOne)]
[CommandLineOptionGroup("options", Name = "Options")]
class Options
{
[CommandLineOption(Name = "filter", RequireExplicitAssignment = true,
Description = "Specifies a filter on which files to include or exclude", GroupId = "options")]
public List<string> Filters
{
get { return mFilters; }
set { mFilters = value; }
}
[CommandLineOption(Name = "h", Aliases = "help", MinOccurs = 0, Description = "Shows this help text", GroupId = "commands")]
public bool Help
{
get { return mHelp; }
set { mHelp = value; }
}
private bool mHelp;
private List<string> mFilters = new List<string>();
}
class Program
{
static int Main(string[] args)
{
Options options = new Options();
CommandLineParser parser = new CommandLineParser(options);
parser.Parse();
if (options.Help)
{
Console.WriteLine(parser.UsageInfo.ToString(78, false));
return 0;
}
else if (parser.HasErrors)
{
Console.WriteLine(parser.UsageInfo.ToString(78, true));
return -1;
}
// No errors present and all arguments correct
// Do work according to arguments
Console.WriteLine("Filters: " + string.Join(",", options.Filters.ToArray()));
return 0;
}
}
}
我用来调用上面代码的语法是:
program.exe --filter abc
结果是:
Example 2 version 1.0.0.0
Copyright (C) Peter Palotas 2007
Errors:
* Missing required value for option "filter"
* One of the options "h" must be specified
Commands:
-h, --help Shows this help text
Options:
--filter Specifies a filter on which files to include or exclude
我系统: VS: 2015操作系统:win7 x64
net: 4
Plossum:来自nuGet的那个
我可能只是看不见我面前的墙。
选项OptionStyles.LongUnix
的命令行应该是:
program.exe --filter=abc