commandline icon indicating copy to clipboard operation
commandline copied to clipboard

OptionAttribute Default not respected in usage example

Open fmelink opened this issue 3 years ago • 0 comments

I'm using CommandLineParser version 2.8.0. The usage example seems to use default(MyEnum) instead of the OptionAttribute Default or even (new MyOptions()).Enum as the default value. A workaround is to reorder the enum values so default(MyEnum) and Default are the same.

enum MyEnum
{
  A,
  B,
  C,
}

class MyOptions
{
  [Option('e', "enum", Default = MyEnum.B)]
  MyEnum Enum { get; init; } = MyEnum.B;

  [Usage]
  static IEnumerable<Example> Examples
  {
    get
    {
      yield return new Example("Do stuff", new MyOptions { Enum = MyEnum.A });
      yield return new Example("Do stuff", new MyOptions { });
      yield return new Example("Do stuff", new MyOptions { Enum = MyEnum.C });
    }
  }
}

Expected usage examples: myprogram --enum A myprogram myprogram --enum C

Actual usage examples: myprogram myprogram --enum B myprogram --enum C

Note that if Enum is not initialized to MyEnum.B in MyOptions, then the actual usage examples become: myprogram myprogram myprogram --enum C

fmelink avatar Feb 25 '22 14:02 fmelink