commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Simpler Way to Display Enum Values

Open svengeance opened this issue 4 years ago • 6 comments

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

svengeance avatar Feb 08 '21 05:02 svengeance

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;
        }

mthgr avatar Feb 24 '21 19:02 mthgr

Or this which is even simpler: https://github.com/commandlineparser/commandline/issues/643

mthgr avatar Feb 24 '21 19:02 mthgr

@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

svengeance avatar Feb 27 '21 18:02 svengeance

Agreed ;-)

mthgr avatar Feb 27 '21 20:02 mthgr

AddEnumValuesToHelpText Doesn't work for non-required options 😭

jci-zimm avatar Jan 27 '22 16:01 jci-zimm