user3566041
user3566041

Reputation: 71

Cannot convert Options to System.Type on Parser.ParseArguments

I'm Trying to compile the following code on a project

...

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

                }
            }
        }
    }

But I just seem to make it work since there's an error on this line

CommandLine.Parser.Default.ParseArguments(args,options)

Exception Thrown

Cannot convert from 'Buzzbox.Program.Options' to 'System.Type'

It doesn't allow me to hardcast it and I'haven't found anything whatsoever on ho to solve this issue although I feel like the solution might be rather simple since I find some other people mentioning it like you can just cast it like this code does without any problem like in here

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

Upvotes: 7

Views: 2128

Answers (2)

Andrew
Andrew

Reputation: 20071

Their website is outdated, but the github is up to date with full working examples:

Program
{
    public class Options
    {
        [Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
        public bool Verbose { get; set; }
    }

    static void Main(string[] args)
    {
        Parser.Default.ParseArguments<Options>(args)
               .WithParsed<Options>(o =>
               {
                   if (o.Verbose)
                   {
                       Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
                       Console.WriteLine("Quick Start Example! App is in Verbose mode!");
                   }
                   else
                   {
                       Console.WriteLine($"Current Arguments: -v {o.Verbose}");
                       Console.WriteLine("Quick Start Example!");
                   }
               });
    }
}

https://github.com/commandlineparser/commandline

Upvotes: 0

HellKnight Hicks
HellKnight Hicks

Reputation: 125

So I did some digging around apparently in the latest version of the Command Line parser application its required that you do things as follows.

CommandLine.Parser.Default.ParseArguments<Options>(args)  
    .WithParsed<Options>(opts => options = opts);

I had a ruff time finding the proper way to do this as well.

Upvotes: 9

Related Questions