Commandline: So confused.. what is going on here?

Created on 6 Nov 2017  路  4Comments  路  Source: commandlineparser/commandline

It seems I have been running around in circles. Obviously the codeplex 1.9x version is now obsolete but there seem to be two forks of this project now?

Any way reading the getting started does not work with the latest beta and looking at the Demo just looks like some kind of convoluted magic happening.

I am an hour in and honestly.. I don't know what is going on any more or how to use this package?

Can we update the docs please? Starting with simple examples where I can just access the object as the original developer intended. Was supposed to be quick and easy.

var result = Parser.Default.ParseArguments<Options>(args);

then i just want to access

Options.

Dammit no intellisense working? Cant access the actual options?

image

I actually do not understand what is going on here?

Most helpful comment

I quickly threw the latest documentation from the old wiki for 2.0 onto this repository's Wiki. At the moment, this is the most complete set of documentation for 2.0 that's available outside of reading the source code (1.9x is not receiving any new changes).

What you have written above will absolutely do what you expect, but adding "side effects" inside that delegate is considered a practice to avoid. What _I_ would recommend is the following, but you can certainly keep doing what you have above:

private void RunApplication(CommandLineOptions options)
{
    // do work with options
}

private void OnError(IEnumerable<Error> errors)
{
    foreach (var error in errors)
    {
        Console.WriteLine(error.ToString());
    }
}

static void Main(string[] args)
{
    var parserResults = Parser.Default.ParseArguments<CommandLineOptions>(args);
    parserResults
        .WithParsed<CommandLineOptions>(RunApplication)
        .WithNotParsed(OnError);
    // No more code here - put application code inside RunApplication
}

All 4 comments

Take a look at the For verbs: section in the readme. It works the same for Options and no verbs, except there are only two delegates: one for the options, one for the errors. You're right though, the docs need to be updated.

P.S. intellisense is working as expected here, although it may not be obvious. The "WithParsed" and "WithNotParsed" are ways to handle the success and error cases, respectively. Otherwise you don't know whether parsing succeeded or failed. "MapResult" combines both of those into a single method. You can choose either of those to get at the options class.

OK - I kind of get it now.. the complexity of the parser makes it highly flexible.. The problem is that at first sight it is highly unclear what is going on. Especially constantly reading that the API has changed and the 1.9x stuff is not relevant anymore, etc, etc...

So at a basic level, first time user.. is this acceptable?

CommandLineOptions options = new CommandLineOptions();

var parserResults = Parser.Default.ParseArguments<CommandLineOptions>(args);

var thingi = parserResults.WithParsed<CommandLineOptions>(opt => options = opt);

var propA = options.Property;

image

PS I am happy to update the docs after a learn a bit more about this..

I quickly threw the latest documentation from the old wiki for 2.0 onto this repository's Wiki. At the moment, this is the most complete set of documentation for 2.0 that's available outside of reading the source code (1.9x is not receiving any new changes).

What you have written above will absolutely do what you expect, but adding "side effects" inside that delegate is considered a practice to avoid. What _I_ would recommend is the following, but you can certainly keep doing what you have above:

private void RunApplication(CommandLineOptions options)
{
    // do work with options
}

private void OnError(IEnumerable<Error> errors)
{
    foreach (var error in errors)
    {
        Console.WriteLine(error.ToString());
    }
}

static void Main(string[] args)
{
    var parserResults = Parser.Default.ParseArguments<CommandLineOptions>(args);
    parserResults
        .WithParsed<CommandLineOptions>(RunApplication)
        .WithNotParsed(OnError);
    // No more code here - put application code inside RunApplication
}

Fantastic - Makes sense!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielbisar picture danielbisar  路  6Comments

TheNybbler picture TheNybbler  路  3Comments

ericnewton76 picture ericnewton76  路  4Comments

ericnewton76 picture ericnewton76  路  3Comments

ericnewton76 picture ericnewton76  路  4Comments