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, Enum with 0 value not shown on Example #452

Open
wants to merge 3 commits 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
6 changes: 3 additions & 3 deletions src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static string FormatCommandLine<T>(this Parser parser, T options, Action<
type.GetSpecifications(
pi => new { Specification = Specification.FromProperty(pi),
Value = pi.GetValue(options, null).NormalizeValue(), PropertyValue = pi.GetValue(options, null) })
where !info.PropertyValue.IsEmpty()
where !info.PropertyValue.IsEmpty(info.Specification)
select info)
.Memorize();

Expand Down Expand Up @@ -242,13 +242,13 @@ private static object NormalizeValue(this object value)
return value;
}

private static bool IsEmpty(this object value)
private static bool IsEmpty(this object value, Specification spec)
{
if (value == null) return true;
#if !SKIP_FSHARP
if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) return true;
#endif
if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) return true;
if (!spec.Required && 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;
return false;
Expand Down
30 changes: 30 additions & 0 deletions tests/CommandLine.Tests/Fakes/Help_Fakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ public static IEnumerable<Example> Examples
}
}

class Options_With_Default_Value_Examples
{
[Option('t', "type", Required = true)]
public EntityType MyEntityType { get; set; }

public enum EntityType
{
T0
}

[Option('i', "int", Required = true)]
public int Int { get; set; }

[Option('d', "double", Required = true)]
public double Double { get; set; }

[Option('o', "optional")]
public bool Optional { get; set; }

[Usage(ApplicationAlias = "mono testapp.exe")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Normal scenario", new Options_With_Default_Value_Examples { });
yield return new Example("With Optional Value", new Options_With_Default_Value_Examples { Optional = true });
}
}
}

[Verb("secert", Hidden = true, HelpText = "This is a secert hidden verb that should never be visible to the user via help text.")]
public class Secert_Verb
{
Expand Down
22 changes: 22 additions & 0 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,28 @@ public static void RenderUsageText_returns_properly_formatted_text()
lines[10].Should().BeEquivalentTo(" mono testapp.exe value");
}

[Fact]
public static void RenderUsageText_returns_proper_text_for_Examples_with_default_values()
{
// Fixture setup
ParserResult<Options_With_Default_Value_Examples> result =
new NotParsed<Options_With_Default_Value_Examples>(
TypeInfo.Create(typeof(Options_With_Default_Value_Examples)), Enumerable.Empty<Error>());

// Exercize system
var text = HelpText.RenderUsageText(result);

// Verify outcome
var lines = text.ToNotEmptyLines();

lines[0].Should().BeEquivalentTo("Normal scenario:");
lines[1].Should().BeEquivalentTo(" mono testapp.exe --double 0 --int 0 --type T0");
lines[2].Should().BeEquivalentTo("With Optional Value:");
lines[3].Should().BeEquivalentTo(" mono testapp.exe --double 0 --int 0 --optional --type T0");

// Teardown
}

//[Fact]
public void Invoke_AutoBuild_for_Options_with_Usage_returns_appropriate_formatted_text()
{
Expand Down