I saw some samples that look like this:
[HelpOption]
public string GetUsage()
{
var help = new HelpText {
Heading = new HeadingInfo("<<app title>>", "<<app version>>"),
Copyright = new CopyrightInfo("<<app author>>", 2014),
AdditionalNewLineAfterOption = true,
AddDashesToOption = true
};
help.AddPreOptionsLine("<<license details here.>>");
help.AddPreOptionsLine("Usage: app -p Someone");
help.AddOptions(this);
return help;
}
However it doesn't seem like HelpOptionAttribute exists in the latest release (2.1.1-beta). What do I use instead?
Was struggling with this too and the workaround that worked for me was:
var parser = new Parser();
var parserResult = parser.ParseArguments<MyOptions>(args);
parserResult.WithParsed<MyOptions>(options => DoSomething(options));
parserResult.WithNotParsed(errs =>
{
var helpText = HelpText.AutoBuild(parserResult, h =>
{
// Configure HelpText here or create your own and return it
h.AdditionalNewLineAfterOption = false;
return HelpText.DefaultParsingErrorsHandler(parserResult, h);
}, e =>
{
return e;
});
Console.Error.Write(helpText);
}
);
Hmmm...downgrading to old version. Thanks.
Just curious, what are you seeing for help text, versus what you want to see?
Is it possible to derive the usage text without being in the middle of a parse operation? All I want is a method Options.GetUsage() that spits out the usage info as it would as if --help was one of the arguments.
Not currently (without reflection at least), the help system is very tightly integrated with the parser result object.
Just my two cents - when I have logic around switches - if this is set then that, or if this is not set then require that - it would be very helpful to be able to dump out the help text after parsing.
I've not found a way to make conditional parsing logic with this version, and the inability to at least do something like options.AutoBuild(options) would be handy.
I was just updating a legacy app from CommandLineParser 1.9.71 under which this was possible.
I appreciate the library has been overhauled but getting the usage seems like a reasonable requirement.
Not a nice workaround:
c#
public static string GetUsage()
{
var result = Parser.Default.ParseArguments<Options>(new string[] { "--help" });
return HelpText.RenderUsageText(result);
}
@hrobertson
You need not to re-use your code (GetUsage) of v1.x to build help.
Help in v2.0 is re-engineered and it's auto generated with zero code configuration.
Also, you can configure help to have more control:
Have a look to HelpText-Configuration and usage-attribute
@moh-hassan I'm fully aware of the changes and that help is auto-generated. My comment was in response to this earlier comment by @theOrillian (echoed by @inzi) :
Is it possible to derive the usage text without being in the middle of a parse operation? All I want is a method Options.GetUsage() that spits out the usage info as it would as if --help was one of the arguments.
OK, so the new help system v2.x is a nice workaround of v1.x :)
Most helpful comment
Is it possible to derive the usage text without being in the middle of a parse operation? All I want is a method Options.GetUsage() that spits out the usage info as it would as if --help was one of the arguments.