@@ -19,6 +19,7 @@ public class UnParserSettings
1919 private bool groupSwitches ;
2020 private bool useEqualToken ;
2121 private bool showHidden ;
22+ private bool skipDefault ;
2223
2324 /// <summary>
2425 /// Gets or sets a value indicating whether unparsing process shall prefer short or long names.
@@ -56,6 +57,14 @@ public bool ShowHidden
5657 set { PopsicleSetter . Set ( Consumed , ref showHidden , value ) ; }
5758 }
5859 /// <summary>
60+ /// Gets or sets a value indicating whether unparsing process shall skip options with DefaultValue.
61+ /// </summary>
62+ public bool SkipDefault
63+ {
64+ get { return skipDefault ; }
65+ set { PopsicleSetter . Set ( Consumed , ref skipDefault , value ) ; }
66+ }
67+ /// <summary>
5968 /// Factory method that creates an instance of <see cref="CommandLine.UnParserSettings"/> with GroupSwitches set to true.
6069 /// </summary>
6170 /// <returns>A properly initalized <see cref="CommandLine.UnParserSettings"/> instance.</returns>
@@ -90,7 +99,7 @@ public static class UnParserExtensions
9099 /// <returns>A string with command line arguments.</returns>
91100 public static string FormatCommandLine < T > ( this Parser parser , T options )
92101 {
93- return parser . FormatCommandLine ( options , config => { } ) ;
102+ return parser . FormatCommandLine ( options , config => { } ) ;
94103 }
95104
96105 /// <summary>
@@ -119,34 +128,38 @@ public static string FormatCommandLine<T>(this Parser parser, T options, Action<
119128 var specs =
120129 ( from info in
121130 type . GetSpecifications (
122- pi => new { Specification = Specification . FromProperty ( pi ) ,
123- Value = pi . GetValue ( options , null ) . NormalizeValue ( ) , PropertyValue = pi . GetValue ( options , null ) } )
124- where ! info . PropertyValue . IsEmpty ( )
125- select info )
131+ pi => new
132+ {
133+ Specification = Specification . FromProperty ( pi ) ,
134+ Value = pi . GetValue ( options , null ) . NormalizeValue ( ) ,
135+ PropertyValue = pi . GetValue ( options , null )
136+ } )
137+ where ! info . PropertyValue . IsEmpty ( info . Specification , settings . SkipDefault )
138+ select info )
126139 . Memorize ( ) ;
127140
128141 var allOptSpecs = from info in specs . Where ( i => i . Specification . Tag == SpecificationType . Option )
129- let o = ( OptionSpecification ) info . Specification
130- where o . TargetType != TargetType . Switch || ( o . TargetType == TargetType . Switch && ( ( bool ) info . Value ) )
131- where ! o . Hidden || settings . ShowHidden
132- orderby o . UniqueName ( )
133- select info ;
142+ let o = ( OptionSpecification ) info . Specification
143+ where o . TargetType != TargetType . Switch || ( o . TargetType == TargetType . Switch && ( ( bool ) info . Value ) )
144+ where ! o . Hidden || settings . ShowHidden
145+ orderby o . UniqueName ( )
146+ select info ;
134147
135148 var shortSwitches = from info in allOptSpecs
136- let o = ( OptionSpecification ) info . Specification
137- where o . TargetType == TargetType . Switch
138- where o . ShortName . Length > 0
139- orderby o . UniqueName ( )
140- select info ;
149+ let o = ( OptionSpecification ) info . Specification
150+ where o . TargetType == TargetType . Switch
151+ where o . ShortName . Length > 0
152+ orderby o . UniqueName ( )
153+ select info ;
141154
142155 var optSpecs = settings . GroupSwitches
143156 ? allOptSpecs . Where ( info => ! shortSwitches . Contains ( info ) )
144157 : allOptSpecs ;
145158
146159 var valSpecs = from info in specs . Where ( i => i . Specification . Tag == SpecificationType . Value )
147- let v = ( ValueSpecification ) info . Specification
148- orderby v . Index
149- select info ;
160+ let v = ( ValueSpecification ) info . Specification
161+ orderby v . Index
162+ select info ;
150163
151164 builder = settings . GroupSwitches && shortSwitches . Any ( )
152165 ? builder . Append ( '-' ) . Append ( string . Join ( string . Empty , shortSwitches . Select (
@@ -191,6 +204,7 @@ private static string FormatValue(Specification spec, object value)
191204
192205 private static object FormatWithQuotesIfString ( object value )
193206 {
207+ if ( value is DateTime ) value = $ "\" { value } \" ";
194208 Func < string , string > doubQt = v
195209 => v . Contains ( "\" " ) ? v . Replace ( "\" " , "\\ \" " ) : v ;
196210
@@ -218,7 +232,7 @@ private static string FormatName(this OptionSpecification optionSpec, UnParserSe
218232 {
219233 // Have a long name and short name not preferred? Go with long!
220234 // No short name? Has to be long!
221- var longName = ( optionSpec . LongName . Length > 0 && ! settings . PreferShortName )
235+ var longName = ( optionSpec . LongName . Length > 0 && ! settings . PreferShortName )
222236 || optionSpec . ShortName . Length == 0 ;
223237
224238 return
@@ -242,9 +256,11 @@ private static object NormalizeValue(this object value)
242256 return value ;
243257 }
244258
245- private static bool IsEmpty ( this object value )
259+ private static bool IsEmpty ( this object value , Specification specification , bool skipDefault )
246260 {
247261 if ( value == null ) return true ;
262+
263+ if ( skipDefault && value . Equals ( specification . DefaultValue . FromJust ( ) ) ) return true ;
248264#if ! SKIP_FSHARP
249265 if ( ReflectionHelper . IsFSharpOptionType ( value . GetType ( ) ) && ! FSharpOptionHelper . IsSome ( value ) ) return true ;
250266#endif
0 commit comments