在C#中使用Plossum CommandLine解析数组

时间:2016-09-19 09:22:07

标签: c# visual-studio-2015 command-line-parsing

我正在尝试使用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年 操作系统:赢7 x64
.NET:4
Plossum:来自nuGet的那个

我可能只是看不到我面前的墙。

1 个答案:

答案 0 :(得分:0)

使用选项OptionStyles.LongUnix,命令行应为:

program.exe --filter=abc