Display help without requiring ParseArguments
Issue by Steve887
Wednesday Jan 20, 2016 at 01:30 GMT
Originally opened as https://github.com/gsscoder/commandline/issues/281
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>?
Comment by aggieben
Friday Jan 22, 2016 at 21:39 GMT
I also am interested in having the help command not generate an error which creates noise in my logging that is undesirable..
Comment by nemec
Wednesday Mar 23, 2016 at 23:46 GMT
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.
Comment by Steve887
Thursday Mar 24, 2016 at 00:07 GMT
@nemec The problem is NotParsed is an internal class so is not available during general usage.
Comment by nemec
Thursday Mar 24, 2016 at 00:39 GMT
Ah, you're right. I was looking at the class but it's the constructor that's internal.
Comment by azforeversupporter
Wednesday Aug 03, 2016 at 09:39 GMT
@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
This is still a problem and would make the usage of this library a lot better. I have some business specific validation rules to perform and these business rules are document in the help message. So when the parsed values are invalid, I'd like to give an error message that says what went wrong and display the help/usage. Would love to see this get fixed.