Skip to content

Fix for issue 850 #852

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

Closed
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
9 changes: 5 additions & 4 deletions src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
@@ -154,8 +154,8 @@ public static string FormatCommandLine<T>(this Parser parser, T options, Action<
var allOptSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Option)
let o = (OptionSpecification)info.Specification
where o.TargetType != TargetType.Switch ||
(o.TargetType == TargetType.Switch && o.FlagCounter && ((int)info.Value > 0)) ||
(o.TargetType == TargetType.Switch && ((bool)info.Value))
(o.TargetType == TargetType.Switch && o.FlagCounter && info.Value is int i && i > 0) ||
(o.TargetType == TargetType.Switch && info.Value is bool b && b)
where !o.Hidden || settings.ShowHidden
orderby o.UniqueName()
select info;
@@ -294,14 +294,15 @@ private static object NormalizeValue(this object value)
private static bool IsEmpty(this object value, Specification specification, bool skipDefault)
{
if (value == null) return true;

if (specification.Required) return false; // always output arguments with Required = true
if (skipDefault && value.Equals(specification.DefaultValue.FromJust())) return true;
if (Nullable.GetUnderlyingType(specification.ConversionType) != null) return false; //nullable

#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;
// Option arguments should be output if Required = false && value == default && OptionAttribute.Default != default
if (!specification.IsOption() && 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;
6 changes: 4 additions & 2 deletions tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -227,9 +227,10 @@ public static void UnParsing_instance_with_timespan()
}

[Theory]
[InlineData(false, 0, "")] //default behaviour based on type
[InlineData(false, 0, "-v 0")] // value == default(int) && value != OptionAttribute.Default
[InlineData(false, 1, "-v 1")] //default skip=false
[InlineData(false, 2, "-v 2")]
[InlineData(true, 0, "-v 0")] // value == default(int) && value != OptionAttribute.Default
[InlineData(true, 1, "")] //default skip=true
public static void UnParsing_instance_with_int(bool skipDefault, int value, string expected)
{
@@ -241,10 +242,11 @@ public static void UnParsing_instance_with_int(bool skipDefault, int value, stri
}

[Theory]
[InlineData(false, 0, "-v 0")]
[InlineData(false, 0, "-v 0")] // value == default(int) && value != OptionAttribute.Default
[InlineData(false, 1, "-v 1")] //default
[InlineData(false, 2, "-v 2")]
[InlineData(false, null, "")]
[InlineData(true, 0, "-v 0")] // value == default(int) && value != OptionAttribute.Default
[InlineData(true, 1, "")] //default
public static void UnParsing_instance_with_int_nullable(bool skipDefault, int? value, string expected)
{