gsscoder / commandline

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

Display help without requiring ParseArguments #281

Open Steve887 opened 8 years ago

Steve887 commented 8 years ago

I'm trying to print the help text for my options without having to force a parser error.

Currently, it seems the only way to print the help text is to call something like

Parser.Default.ParseArguments<Options>(new string[] { "verb", "--help" });

I see there's HelpText.AutoBuild but that seems to require a NotParsed result.

Is there a way to write the help text for an options class without having to parse arguments? Maybe something like HelpText.DisplayHelp<Options>?

aggieben commented 8 years ago

I also am interested in having the help command not generate an error which creates noise in my logging that is undesirable..

nemec commented 8 years ago

I just noticed this snippet from the HelpTextTests unit test that just creates a fake result. Does this do what you were looking for?

var help = new HelpText { AddDashesToOption = true }
    .AddOptions(new NotParsed<Options_With_HelpText_And_MetaValue>(
        TypeInfo.Create(typeof(Options_With_HelpText_And_MetaValue)),
        Enumerable.Empty<Error>()));
Console.WriteLine(help.ToString());

It doesn't do the Copyright info or project name, but you can call the AddPreOptionsLine method on HelpText to add it in yourself.

Steve887 commented 8 years ago

@nemec The problem is NotParsed is an internal class so is not available during general usage.

nemec commented 8 years ago

Ah, you're right. I was looking at the class but it's the constructor that's internal.

robertkruis commented 8 years ago

@Steve887, @nemec

Even though the NotParsed class is internal, you could create an instance by using reflection (although it's questionable if you should do so). I created a gist with an extension method GetAutoBuildHelpText on the ParserResult class that will do exactly the same as the code in the unit test, except for the fact that it'll do this by using reflection.

You can find the gist here: https://gist.github.com/azforeversupporter/82b794ded5d16a390d89eb2071035388