Skip to content

Fix issue #104 of nullable enum #453

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

Merged
merged 1 commit into from
Jul 29, 2019
Merged
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
6 changes: 3 additions & 3 deletions src/CommandLine/Core/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CommandLine.Infrastructure;
using CSharpx;

namespace CommandLine.Core
Expand Down Expand Up @@ -115,9 +116,8 @@ public static Specification FromProperty(PropertyInfo property)
if (oa.Count() == 1)
{
var spec = OptionSpecification.FromAttribute(oa.Single(), property.PropertyType,
property.PropertyType.GetTypeInfo().IsEnum
? Enum.GetNames(property.PropertyType)
: Enumerable.Empty<string>());
ReflectionHelper.GetNamesOfEnum(property.PropertyType));

if (spec.ShortName.Length == 0 && spec.LongName.Length == 0)
{
return spec.WithLongName(property.Name.ToLowerInvariant());
Expand Down
20 changes: 15 additions & 5 deletions src/CommandLine/Infrastructure/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
// Test support
if (_overrides != null)
{
return
return
_overrides.ContainsKey(typeof(TAttribute)) ?
Maybe.Just((TAttribute)_overrides[typeof(TAttribute)]) :
Maybe.Nothing< TAttribute>();
Maybe.Nothing<TAttribute>();
}

var assembly = GetExecutingOrEntryAssembly();
Expand Down Expand Up @@ -84,7 +84,7 @@ public static bool IsFSharpOptionType(Type type)

public static T CreateDefaultImmutableInstance<T>(Type[] constructorTypes)
{
var t = typeof(T);
var t = typeof(T);
return (T)CreateDefaultImmutableInstance(t, constructorTypes);
}

Expand All @@ -100,7 +100,17 @@ private static Assembly GetExecutingOrEntryAssembly()
{
//resolve issues of null EntryAssembly in Xunit Test #392,424,389
//return Assembly.GetEntryAssembly();
return Assembly.GetEntryAssembly()??Assembly.GetCallingAssembly();
return Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
}

public static IEnumerable<string> GetNamesOfEnum(Type t)
{
if (t.IsEnum)
return Enum.GetNames(t);
Type u = Nullable.GetUnderlyingType(t);
if (u != null && u.IsEnum)
return Enum.GetNames(u);
return Enumerable.Empty<string>();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

namespace CommandLine.Tests.Fakes
{
class Options_With_Nullable_Enum_Having_HelpText
{
[Option(HelpText = "Define a string value here.")]
public string StringValue { get; set; }

[Option(HelpText="Define a enum value here.")]
public Shapes? Shape { get; set; }
}
}
68 changes: 68 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue104Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Linq;
using CommandLine.Tests.Fakes;
using CommandLine.Text;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

//Issue #104
//When outputting HelpText, the code will correctly list valid values for enum type options. However, if the option is a nullable enum type, then it will not list the valid values.

namespace CommandLine.Tests.Unit
{
public class Issue104Tests
{

[Fact]
public void Create_instance_with_enum_options_enabled_and_nullable_enum()
{
// Fixture setup
// Exercize system
var sut = new HelpText { AddDashesToOption = true, AddEnumValuesToHelpText = true, MaximumDisplayWidth = 80 }
.AddPreOptionsLine("pre-options")
.AddOptions(new NotParsed<Options_With_Nullable_Enum_Having_HelpText>(TypeInfo.Create(typeof(Options_With_Enum_Having_HelpText)), Enumerable.Empty<Error>()))
.AddPostOptionsLine("post-options");

// Verify outcome

var lines = sut.ToString().ToNotEmptyLines().TrimStringArray();
lines[0].Should().BeEquivalentTo("pre-options");
lines[1].Should().BeEquivalentTo("--stringvalue Define a string value here.");
lines[2].Should().BeEquivalentTo("--shape Define a enum value here. Valid values: Circle, Square,");
lines[3].Should().BeEquivalentTo("Triangle");
lines[4].Should().BeEquivalentTo("--help Display this help screen.");
lines[5].Should().BeEquivalentTo("--version Display version information.");
lines[6].Should().BeEquivalentTo("post-options");
// Teardown
}

[Fact]
public void Help_with_enum_options_enabled_and_nullable_enum()
{
// Fixture setup
// Exercize system
var args = "--help".Split();
var sut = new Parser(config => config.HelpWriter = null);
var parserResult = sut.ParseArguments<Options_With_Nullable_Enum_Having_HelpText>(args);
HelpText helpText = null;
parserResult.WithNotParsed(errors =>
{
// Use custom help text to ensure valid enum values are displayed
helpText = HelpText.AutoBuild(parserResult);
helpText.AddEnumValuesToHelpText = true;
helpText.AddOptions(parserResult);
});

// Verify outcome

var lines = helpText.ToString().ToNotEmptyLines().TrimStringArray();
lines[2].Should().BeEquivalentTo("--stringvalue Define a string value here.");
lines[3].Should().BeEquivalentTo("--shape Define a enum value here. Valid values: Circle, Square,");
lines[4].Should().BeEquivalentTo("Triangle");
lines[5].Should().BeEquivalentTo("--help Display this help screen.");
lines[6].Should().BeEquivalentTo("--version Display version information.");
// Teardown
}
}

}