From 32898c42f0fcca14618692d57f7ad5e2f18bbe24 Mon Sep 17 00:00:00 2001 From: Gordon Li Date: Tue, 27 Sep 2022 22:42:22 +0100 Subject: [PATCH] Always output options where Required = true, output options where value == default && OptionAttribute.Default != default --- src/CommandLine/UnParserExtensions.cs | 9 +++++---- tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/CommandLine/UnParserExtensions.cs b/src/CommandLine/UnParserExtensions.cs index e823a7fa..284cde06 100644 --- a/src/CommandLine/UnParserExtensions.cs +++ b/src/CommandLine/UnParserExtensions.cs @@ -154,8 +154,8 @@ public static string FormatCommandLine(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; diff --git a/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs b/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs index 7e878f32..f79480b0 100644 --- a/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs +++ b/tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs @@ -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) {