'Command Line Parser NUGet Package getting simple example program to work
I'm finding this very challenging and I appreciate any help you are willing of offer me.
Currently I'm trying to implement Command Line Parser (https://github.com/commandlineparser/commandline).
I just want to get a basic example application working and I am stuck.
Ultimately I want the following pattern
MyProgram -soureid 1231 -domain alpha
Where I get sourceid and domain as valid variables. sourceid would have the value of 1231 and domain would have the value of "alpha".
This is a C# .net core application (2.3) and I'm running Visual Studio 2017.
Here is the code that I have so far...
using System;
using CommandLine;
namespace Program
{
class Program
{
public static void Main(String[] args)
{
var options = new SomeOptions();
CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));
Console.WriteLine(options.Age);
Console.ReadLine();
}
}
class SomeOptions
{
[Option('n', "name", Required = true)]
public string Name { get; set; }
[Option('a', "age")]
public int Age { get; set; }
}
}
This code does not work. When I pass -n Jason I get this..
CommandLineArgumentParsing 1.0.0
Copyright (C) 2019 CommandLineArgumentParsing
ERROR(S):
Verb '-n' is not recognized.
--help Display this help screen.
--version Display version information.
0
I believe this issue is with this line..
CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));
It seems like this line should be this..
CommandLine.Parser.Default.ParseArguments(args, typeof(options));
However the compiler is complaining that "'options' is a variable but is used like a type"
What am I doing wrong?
Solution 1:[1]
I figured this out about two seconds after I asked the question..
Replace..
CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));
With...
Parser.Default.ParseArguments<SomeOptions>(args).WithParsed(parsed => options = parsed);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | codingguy3000 |
