@@ -19,6 +19,7 @@ public class UnParserSettings
19
19
private bool groupSwitches ;
20
20
private bool useEqualToken ;
21
21
private bool showHidden ;
22
+ private bool skipDefault ;
22
23
23
24
/// <summary>
24
25
/// Gets or sets a value indicating whether unparsing process shall prefer short or long names.
@@ -56,6 +57,14 @@ public bool ShowHidden
56
57
set { PopsicleSetter . Set ( Consumed , ref showHidden , value ) ; }
57
58
}
58
59
/// <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>
59
68
/// Factory method that creates an instance of <see cref="CommandLine.UnParserSettings"/> with GroupSwitches set to true.
60
69
/// </summary>
61
70
/// <returns>A properly initalized <see cref="CommandLine.UnParserSettings"/> instance.</returns>
@@ -90,7 +99,7 @@ public static class UnParserExtensions
90
99
/// <returns>A string with command line arguments.</returns>
91
100
public static string FormatCommandLine < T > ( this Parser parser , T options )
92
101
{
93
- return parser . FormatCommandLine ( options , config => { } ) ;
102
+ return parser . FormatCommandLine ( options , config => { } ) ;
94
103
}
95
104
96
105
/// <summary>
@@ -119,34 +128,38 @@ public static string FormatCommandLine<T>(this Parser parser, T options, Action<
119
128
var specs =
120
129
( from info in
121
130
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 )
126
139
. Memorize ( ) ;
127
140
128
141
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 ;
134
147
135
148
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 ;
141
154
142
155
var optSpecs = settings . GroupSwitches
143
156
? allOptSpecs . Where ( info => ! shortSwitches . Contains ( info ) )
144
157
: allOptSpecs ;
145
158
146
159
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 ;
150
163
151
164
builder = settings . GroupSwitches && shortSwitches . Any ( )
152
165
? builder . Append ( '-' ) . Append ( string . Join ( string . Empty , shortSwitches . Select (
@@ -191,6 +204,7 @@ private static string FormatValue(Specification spec, object value)
191
204
192
205
private static object FormatWithQuotesIfString ( object value )
193
206
{
207
+ if ( value is DateTime ) value = $ "\" { value } \" ";
194
208
Func < string , string > doubQt = v
195
209
=> v . Contains ( "\" " ) ? v . Replace ( "\" " , "\\ \" " ) : v ;
196
210
@@ -218,7 +232,7 @@ private static string FormatName(this OptionSpecification optionSpec, UnParserSe
218
232
{
219
233
// Have a long name and short name not preferred? Go with long!
220
234
// 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 )
222
236
|| optionSpec . ShortName . Length == 0 ;
223
237
224
238
return
@@ -242,9 +256,11 @@ private static object NormalizeValue(this object value)
242
256
return value ;
243
257
}
244
258
245
- private static bool IsEmpty ( this object value )
259
+ private static bool IsEmpty ( this object value , Specification specification , bool skipDefault )
246
260
{
247
261
if ( value == null ) return true ;
262
+
263
+ if ( skipDefault && value . Equals ( specification . DefaultValue . FromJust ( ) ) ) return true ;
248
264
#if ! SKIP_FSHARP
249
265
if ( ReflectionHelper . IsFSharpOptionType ( value . GetType ( ) ) && ! FSharpOptionHelper . IsSome ( value ) ) return true ;
250
266
#endif
0 commit comments