From 86cf5dcada29506fdcf5256d539bd44cea45bb3c Mon Sep 17 00:00:00 2001 From: Chris Hallett Date: Mon, 21 Oct 2024 21:03:11 +1100 Subject: [PATCH] fix-379 Fixing issue with default value on enums for help text --- src/CommandLine/UnParserExtensions.cs | 1 + .../Options_With_Empty_Enum_First_Element.cs | 37 +++++++++++++++++++ tests/CommandLine.Tests/Unit/Issue379Tests.cs | 28 ++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/CommandLine.Tests/Fakes/Options_With_Empty_Enum_First_Element.cs create mode 100644 tests/CommandLine.Tests/Unit/Issue379Tests.cs diff --git a/src/CommandLine/UnParserExtensions.cs b/src/CommandLine/UnParserExtensions.cs index e823a7fa..477d6229 100644 --- a/src/CommandLine/UnParserExtensions.cs +++ b/src/CommandLine/UnParserExtensions.cs @@ -301,6 +301,7 @@ private static bool IsEmpty(this object value, Specification specification, bool #if !SKIP_FSHARP if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) return true; #endif + if (value is Enum && value.Equals(value.GetType().GetDefaultValue())) return false; if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) return true; if (value is string && ((string)value).Length == 0) return true; if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) return true; diff --git a/tests/CommandLine.Tests/Fakes/Options_With_Empty_Enum_First_Element.cs b/tests/CommandLine.Tests/Fakes/Options_With_Empty_Enum_First_Element.cs new file mode 100644 index 00000000..9eafed03 --- /dev/null +++ b/tests/CommandLine.Tests/Fakes/Options_With_Empty_Enum_First_Element.cs @@ -0,0 +1,37 @@ +using CommandLine.Text; +using System.Collections.Generic; + +namespace CommandLine.Tests.Fakes +{ + public enum EntityType + { + T0, + T1, + T2 + } + + public class Options_With_Empty_Enum_First_Element + { + [Option('t', "type", HelpText = "My entity")] + public EntityType BaseEnum { get; set; } + + [Option('v', "value")] + public int Value { get; set; } + [Option('d')] + public double Double { get; set; } + [Option('b')] + public bool Bool { get; set; } + + [Usage(ApplicationAlias = "test.exe")] + public static IEnumerable Examples + { + get + { + yield return new Example("1", new Options_With_Empty_Enum_First_Element { BaseEnum = EntityType.T0 }); + yield return new Example("2", new Options_With_Empty_Enum_First_Element { BaseEnum = EntityType.T1 }); + yield return new Example("3", new Options_With_Empty_Enum_First_Element { Value = 1, Double = 0.1, Bool = false }); + yield return new Example("4", new Options_With_Empty_Enum_First_Element { BaseEnum = EntityType.T0, Value = 1, Double = 0.1, Bool = true }); + } + } + } +} diff --git a/tests/CommandLine.Tests/Unit/Issue379Tests.cs b/tests/CommandLine.Tests/Unit/Issue379Tests.cs new file mode 100644 index 00000000..43e3acba --- /dev/null +++ b/tests/CommandLine.Tests/Unit/Issue379Tests.cs @@ -0,0 +1,28 @@ +using CommandLine.Tests.Fakes; +using CommandLine.Text; +using System.Linq; +using Xunit; + +namespace CommandLine.Tests.Unit +{ + public class Issue379Tests + { + [Fact] + public void OptionExamples_With_Default_Values_Should_Display_In_Help_Text() + { + // Fixture setup + ParserResult result = + new NotParsed( + TypeInfo.Create(typeof(Options_With_Empty_Enum_First_Element)), Enumerable.Empty()); + + // Exercize system + var text = HelpText.RenderUsageText(result); + + // Verify outcome + Assert.Contains("test.exe --type T0", text); + Assert.Contains("test.exe --type T1", text); + Assert.Contains("test.exe -d 0.1 --type T0 --value 1", text); + Assert.Contains("test.exe -b -d 0.1 --type T0 --value 1", text); + } + } +}