Simpler Way to Display Enum Values
Hey all - apologies if this exists, can't find it easily.
I'm looking for an easier capability to display the valid enum values. I don't need any custom HelpText behavior whatsoever, and it looks like there's a nontrivial amount of work to capture the full functionality of the default HelpText portion.
There should be an option (top-level on the Parser instantiation?) that lets you trivially define these enum values without having to reimplement any bits of the default HelpText display.
Cheers! Sven
I had the same problem. This is how I solved it. It's not simple but it works.
I have some logic I removed from WithParsed so you can add your own logging there for example.
private static Options _opts;
static void Main(string[] args)
{
_opts = Validate.Options.General<Options>(args, "appName");
}
And the Validate.Options.General method:
/// <summary>
/// Validates the options and args.
/// </summary>
/// <param name="args">The arguments.</param>
/// <param name="appName">The application name. Optional.</param>
public static T General<T>(string[] args, string appName = "")
{
T toReturn = default(T);
//We replace by our own parser because we want case insensitive enum.
Parser myParser = new Parser(config =>
{
config.HelpWriter = null;
config.CaseInsensitiveEnumValues = true;
config.AutoHelp = true;
}
);
ParserResult<T> parserResult = myParser.ParseArguments<T>(args);
parserResult.WithParsed(o => { toReturn = o; });
parserResult.WithNotParsed(errors =>
{
// Use custom help text to ensure valid enum values are displayed
HelpText helpText = HelpText.AutoBuild(parserResult);
helpText.AddEnumValuesToHelpText = true;
helpText.AddOptions(parserResult);
string header = "INVOKED WITH INVALID - Parameters:";
string msg = header + Environment.NewLine + helpText;
Console.WriteLine(msg);
string errorsText = HelpText.RenderParsingErrorsText(parserResult,
helpText.SentenceBuilder.FormatError,
helpText.SentenceBuilder.FormatMutuallyExclusiveSetErrors,
2
);
if (!appName.IsNullOrEmpty())
{
errorsText = $"{appName}: {errorsText}";
}
throw new ApplicationArgumentException(errorsText);
});
return toReturn;
}
Or this which is even simpler: https://github.com/commandlineparser/commandline/issues/643
@mthgr You can see what I mean by a simpler solution. I don't desire any change in behavior other than showing the enum values in helptext, and it feels very off to me that the only way to display enum values is by replacing the internal helptext behavior, mimicking the error/not-parsed handling, and then adding it
Agreed ;-)
AddEnumValuesToHelpText Doesn't work for non-required options 😭