gsscoder / commandline

Terse syntax C# command line parser for .NET with F# support
1.63k stars 293 forks source link

Displaying help text with fluent syntax? #223

Open jawn opened 9 years ago

jawn commented 9 years ago

Using the latest nuget of the version 2.0 pre-release, I can display help text like this:

var results = Parser.Default.ParseArguments<Options>(args)
  .WithParsed(opts => /* handle options here */)

if (results.Tag == ParserResultType.NotParsed)
{
  var helpText = HelpText.AutoBuild(results);
  Console.WriteLine(helpText);
}

Is there a way to do this within the fluent syntax?

Something like var results = Parser.Default.ParseArguments(args) .WithNotParsed(opts => Console.WriteLine(HelpText.AutoBuild())); .WithParsed(opts => /* handle options here */)

Since AutoBuild() requires results, this can't be called within the fluent syntax. That's already logged as #88, however I would like to know whether a different solution is available.

gsscoder commented 9 years ago

Hi @jawn, happy you choose this library.

If you invoke the parser using default singleton like in posted sample, you don't need any call to AutoBuild(). This is because this instance is pre-configured with a ParserSettings.HelpWriter set to Console.Error and when this property has a value the help screen is automatically generated and displayed.

In this case just stay with the WithParsed and job is done.

If you want handle help text manually, there's various way to do it from using AutoBuild() to mixing various intermediate step to get whatever customization you want.

Any way a possible fluent version, could be an extension to ParserResult like result.AutoBuild()...

If this not helped, please let me know.

PS: If possible use ParserResult extension methods instead of manually checking Tag discriminator and cast the instance.