无法在 Parser.ParseArguments 上将选项转换为 System.Type

use*_*041 7 c# command-line-arguments

我正在尝试在项目中编译以下代码

...

namespace Buzzbox
    {
        class Program
        {

            //Command line options through CommandLine: http://commandline.codeplex.com/
            class Options
            {
                [Option('i', "input", 
                    Required = true, 
                    HelpText = "Path to input file to be Encoded, must be in hearthstonejson format.")]
                public string InputFile { get; set; }

                [Option('o', "output",
                    HelpText = "Output file path.",
                    Default = "output.txt")]
                public string OutputFile { get; set; }

                [Option('e', "encoding",
                    HelpText = "Which encoding format to use. Supported formats are scfdivineFormat and MtgEncoderFormat.",
                    Default = EncodingFormats.MtgEncoderFormat)]
                public EncodingFormats EncodingFormat { get; set; }

                [Option("shuffle-fields", Default = false,
                    HelpText = "Shuffles the fields of the output in supported Encoding Formats.")]
                public bool ShuffleFields { get; set; }

                [Option("shuffle-cards", Default = false,
                    HelpText = "Shuffles the the cards, randomizing the order of output.")]
                public bool ShuffleCards { get; set; }

                [Option("flavor-text", Default = false,
                    HelpText = "Include flavortext field.")]
                public bool FlavorText { get; set; }

                [Option("verbose", Default = false,
                   HelpText = "Output additional information. Exclusive with the --silent option.")]
                public bool Verbose { get; set; }

                [Option("silent", Default = false,
                   HelpText = "Never output anything but error messages. Exclusive with the --verbose option.")]
                public bool Silent { get; set; }

            }

            private static void Main(string[] args)
            {
                //Parse Commandline options
                var options = new Options();
                var encode = new Encode
                {
                    ShuffleFields = options.ShuffleFields,
                    IncludeFlavorText = options.FlavorText
                };

                //Only continue if commandline options fullfilled. CommandLine will handle helptext if something was off.
                if (CommandLine.Parser.Default.ParseArguments(args,options))
                {
                  //extra things

                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我似乎只是让它工作,因为这条线上有一个错误

CommandLine.Parser.Default.ParseArguments(args,options)
Run Code Online (Sandbox Code Playgroud)

抛出异常

无法从“Buzzbox.Program.Options”转换为“System.Type”

它不允许我硬投射它,我还没有找到任何解决这个问题的方法,尽管我觉得解决方案可能相当简单,因为我发现其他人提到它就像你可以像这样投射它代码没有任何问题,就像这里

http://simontimms.com/2014/07/09/parsing-command-line-arguments-in-c/

小智 9

所以我在命令行解析器应用程序的最新版本中显然做了一些挖掘,它要求你做如下事情。

CommandLine.Parser.Default.ParseArguments<Options>(args)  
    .WithParsed<Options>(opts => options = opts);
Run Code Online (Sandbox Code Playgroud)

我也有一段时间找到了正确的方法来做到这一点。