Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-379 Fixing issue with default value on enums for help text #937

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Example> 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 });
}
}
}
}
28 changes: 28 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue379Tests.cs
Original file line number Diff line number Diff line change
@@ -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<Options_With_Empty_Enum_First_Element> result =
new NotParsed<Options_With_Empty_Enum_First_Element>(
TypeInfo.Create(typeof(Options_With_Empty_Enum_First_Element)), Enumerable.Empty<Error>());

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