1

I'm writing a Console App (.NET Framework) in C#. I want to use arguments from the command line, and I'm trying to use the Command Line Parser library to help me do this.

This is the package on Nuget - https://www.nuget.org/packages/CommandLineParser/

I found out about it from this StackOverflow question - Best way to parse command line arguments in C#?

MWE

using System;
using CommandLine;

namespace CLPtest
{
    class Program
    { 

        class SomeOptions
        {
            [Option('n', "name")]
            public string Name { get; set; }

        }

        static void Main(string[] args)
        {

            var options = new SomeOptions();

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

        }
    }
}

When I try create a minimal working example, I get an error for options on this line:

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

The error is Argument 2: cannot convert from 'CLPtest.Program.SomeOptions' to 'System.Type'

I'm really confused as I have seen this same example code on at least 3 tutorials for how to use this library. (see for example - Parsing Command Line Arguments with Command Line Parser Library)

Mike
  • 143
  • 9
  • [This is the file that OP didn't link to](https://github.com/commandlineparser/commandline/blob/master/src/CommandLine/Parser.cs). There are several overloads of ParseArguments(). My advice would be to decide which one you want to call, and pass arguments of the correct types. Naturally, their code can't know about your SomeOptions class. The tutorial you link is blocked by the firewall where I work, but maybe somebody else can take a look. – 15ee8f99-57ff-4f92-890c-b56153 Nov 05 '19 at 18:20
  • I've looked at the tutorial. Maybe it's possible that there's a different nuget package by that same name, which happens to have a very similar interface, but which also doesn't turn up when I search nuget for "CommandLineParser". But that's farfetched. It's also possible that the tutorial refers to a version of the library that's obsolete or hasn't been released yet. It's a fact that the current version, 2.6.0, does not have the method the tutorial is calling. Unfortunately, many tutorials are worthless at best. – 15ee8f99-57ff-4f92-890c-b56153 Nov 05 '19 at 18:33
  • @EdPlunkett Ok, thanks for looking into it. The nuget package I downloaded had 11M downloads so I'm guessing it's 'the' one. But yes, it could be that the tutorial is now out of date. – Mike Nov 05 '19 at 18:36
  • I spot-checked a few old versions and none of them had any such method. I guess you could try them all, and maybe one or two will have it. – 15ee8f99-57ff-4f92-890c-b56153 Nov 05 '19 at 18:36

1 Answers1

1

(This answer is being written at the time of v2.7 of this library)

From looking at their repository's README, it appears as if this is part of the API change that is mentioned earlier in the README. It looks as though the arguments are now handled differently since the example code you reference. So, now you should do something like this inside of Main:

...

static void Main(string[] args)
{
    CommandLine.Parser.Default.ParseArguments<SomeOptions>(args);
}

...

To actually do something with those options you can use WithParsed which takes in the options that are defined in your SomeOptions class.

...

static void Main(string[] args)
{
    CommandLine.Parser.Default.ParseArguments<SomeOptions>(args).WithParsed(option =>
    {
        // Do something with your parsed arguments in here...

        Console.WriteLine(option.Name); // This is the property from your SomeOptions class.
    });
}

...

The C# Example further down the README shows that you can pass in a method into WithParsed to handle your options instead of doing everything within Main.

cGilmore
  • 11
  • 1