【问题标题】:Parse arrays using Plossum CommandLine in C#在 C# 中使用 Plossum 命令行解析数组
【发布时间】:2017-09-20 20:27:11
【问题描述】:

我正在尝试使用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

我的系统:

对比:2015
操作系统:Win 7 x64
.NET:4
Plossum:来自 nuGet 的那个

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

【问题讨论】:

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


    【解决方案1】:

    使用选项OptionStyles.LongUnix 命令行应该是:

    program.exe --filter=abc
    

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      相关资源
      最近更新 更多