-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use a custom HelpText for help/error output? #259
Comments
Hi @tmillican, your workaround is mostly the same as the solution in #242 - just set it to null instead of a black hole writer.
This is pretty much the standard customization point - if you want to change the default help output you need to suppress it and then print it yourself on error. The static void Main(...)
{
// ... parsing here
result
.WithParsed<Options>(opts => DoParsed(opts))
.WithNotParsed(errs => DoError(result, errs));
}
static void DoError(ParseResult<Options> result, IEnumerable<Error> errs)
{
// helptext autobuild
} |
Thanks for the reply. In my actual project code I'm capturing the result, as you point out. I just couldn't tell if I was missing some direct way to furnish a "top level" It would be nice if we could get a page for help text customization on the wiki. Judging from several issues, it looks like I'm not the only one having trouble figuring out the standard methodology for this in 2.2.x |
More particularly, I'm trying to force a line break spacer between the copyright notice and the "USAGE:" line (and between "USAGE:" and "ERROR(S):". But in the general case, I can't figure out how to inject a custom
HelpText
instance into the output process. I've read #224, but this approach doesn't seem to actually work -- at least not in 2.2.1.Example code:
If you run this, you'll see that
DoError(...)
isn't invoked until after the help text is output. If you define a custom HelpText inDoError(...)
and output it there, as suggested in #242 and elsewhere, you can get whatever output you'd like, but only after the default help text has already been printed.After poking at the source code, it seems that
Parser.ParseArguments<T>(...)
invokesParser.MakeParserResult<T>(...)
, which invokesParser.DisplayHelp<T>(...)
, which ultimately invokesHelpText.AutoBuild<<T>(...)
. The only control the user has over this process (that I can determine) is the ParserSettings object used to construct the Parser. This lets you specify which TextWriter to use, and whether to print line breaks between options, but that's all.As a workaround, I can specify a
TextWriter
object that blackholes the output and then substitute my own output inDoError(...)
, but it's pretty kludgey.TL;DR: Help/error output occurs before
ParseArguments<T>(...)
returns, so nothing you subsequently do withWithUnparsed<T>(...)
orMapResult<...>(...)
can affect the output. SinceHelpOptionAttribute
went away, what's the injection point for customizing error output?The text was updated successfully, but these errors were encountered: