BadScript 2
Loading...
Searching...
No Matches
CommandLine.UnParserExtensions Class Reference

Provides overloads to unparse options instance. More...

Static Public Member Functions

static string FormatCommandLine< T > (this Parser parser, T options)
 Format a command line argument string from a parsed instance.
 
static string[] FormatCommandLineArgs< T > (this Parser parser, T options)
 Format a command line argument string from a parsed instance in the form of string[].
 
static string FormatCommandLine< T > (this Parser parser, T options, Action< UnParserSettings > configuration)
 Format a command line argument string from a parsed instance.
 
static string[] FormatCommandLineArgs< T > (this Parser parser, T options, Action< UnParserSettings > configuration)
 Format a command line argument string[] from a parsed instance.
 
static string[] SplitArgs (this string command, bool keepQuote=false)
 Returns a string array that contains the substrings in this instance that are delimited by space considering string between double quote.
 

Static Private Member Functions

static string FormatValue (Specification spec, object value)
 
static object FormatWithQuotesIfString (object value)
 
static char SeperatorOrSpace (this Specification spec)
 
static string FormatOption (OptionSpecification spec, object value, UnParserSettings settings)
 
static string FormatName (this OptionSpecification optionSpec, object value, UnParserSettings settings)
 
static object NormalizeValue (this object value)
 
static bool IsEmpty (this object value, Specification specification, bool skipDefault)
 

Detailed Description

Provides overloads to unparse options instance.

Definition at line 97 of file UnParserExtensions.cs.

Member Function Documentation

◆ FormatCommandLine< T >() [1/2]

static string CommandLine.UnParserExtensions.FormatCommandLine< T > ( this Parser  parser,
options 
)
static

Format a command line argument string from a parsed instance.

Template Parameters
TType of options .
Parameters
parserParser instance.
optionsA parsed (or manually correctly constructed instance).
Returns
A string with command line arguments.

Definition at line 106 of file UnParserExtensions.cs.

107 {
108 return parser.FormatCommandLine(options, config => { });
109 }

◆ FormatCommandLine< T >() [2/2]

static string CommandLine.UnParserExtensions.FormatCommandLine< T > ( this Parser  parser,
options,
Action< UnParserSettings configuration 
)
static

Format a command line argument string from a parsed instance.

Template Parameters
TType of options .
Parameters
parserParser instance.
optionsA parsed (or manually correctly constructed instance).
configurationThe Action<UnParserSettings> lambda used to configure aspects and behaviors of the unparsersing process.
Returns
A string with command line arguments.

Definition at line 135 of file UnParserExtensions.cs.

136 {
137 if (options == null)
138 {
139 throw new ArgumentNullException("options");
140 }
141
142 UnParserSettings settings = new UnParserSettings();
143 configuration(settings);
144 settings.Consumed = true;
145
146 Type type = options.GetType();
147 StringBuilder builder = new StringBuilder();
148
149 type.GetVerbSpecification()
150 .MapValueOrDefault(verb => builder.Append(verb.Name)
151 .Append(' '),
152 builder
153 );
154
155 var specs =
156 (from info in
157 type.GetSpecifications(pi => new
158 {
160 Value = pi.GetValue(options, null)
161 .NormalizeValue(),
162 PropertyValue = pi.GetValue(options, null),
163 }
164 )
165 where !info.PropertyValue.IsEmpty(info.Specification, settings.SkipDefault)
166 select info)
167 .Memoize();
168
169 var allOptSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Option)
170 let o = (OptionSpecification)info.Specification
171 where o.TargetType != TargetType.Switch ||
172 (o.TargetType == TargetType.Switch && o.FlagCounter && (int)info.Value > 0) ||
173 (o.TargetType == TargetType.Switch && (bool)info.Value)
174 where !o.Hidden || settings.ShowHidden
175 orderby o.UniqueName()
176 select info;
177
178 var shortSwitches = from info in allOptSpecs
179 let o = (OptionSpecification)info.Specification
180 where o.TargetType == TargetType.Switch
181 where o.ShortName.Length > 0
182 orderby o.UniqueName()
183 select info;
184
185 var optSpecs = settings.GroupSwitches
186 ? allOptSpecs.Where(info => !shortSwitches.Contains(info))
187 : allOptSpecs;
188
189 var valSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Value)
190 let v = (ValueSpecification)info.Specification
191 orderby v.Index
192 select info;
193
194 builder = settings.GroupSwitches && shortSwitches.Any()
195 ? builder.Append('-')
196 .Append(string.Join(string.Empty,
197 shortSwitches.Select(info =>
198 {
199 OptionSpecification o =
200 (OptionSpecification)info
201 .Specification;
202
203 return o.FlagCounter
204 ? string
205 .Concat(Enumerable
206 .Repeat(o.ShortName,
207 (int)info.Value
208 )
209 )
210 : o.ShortName;
211 }
212 )
213 .ToArray()
214 )
215 )
216 .Append(' ')
217 : builder;
218
219 optSpecs.ForEach(opt =>
220 builder
221 .Append(FormatOption((OptionSpecification)opt.Specification, opt.Value, settings))
222 .Append(' ')
223 );
224
225 builder.AppendWhen(valSpecs.Any() && parser.Settings.EnableDashDash, "-- ");
226
227 valSpecs.ForEach(val => builder.Append(FormatValue(val.Specification, val.Value))
228 .Append(' ')
229 );
230
231 return builder
232 .ToString()
233 .TrimEnd(' ');
234 }
static Specification FromProperty(PropertyInfo property)
static string FormatValue(Specification spec, object value)
static string FormatOption(OptionSpecification spec, object value, UnParserSettings settings)

◆ FormatCommandLineArgs< T >() [1/2]

static string[] CommandLine.UnParserExtensions.FormatCommandLineArgs< T > ( this Parser  parser,
options 
)
static

Format a command line argument string from a parsed instance in the form of string[].

Template Parameters
TType of options .
Parameters
parserParser instance.
optionsA parsed (or manually correctly constructed instance).
Returns
A string[] with command line arguments.

Definition at line 118 of file UnParserExtensions.cs.

119 {
120 return parser.FormatCommandLine(options, config => { })
121 .SplitArgs();
122 }
static string[] SplitArgs(this string command, bool keepQuote=false)
Returns a string array that contains the substrings in this instance that are delimited by space cons...

◆ FormatCommandLineArgs< T >() [2/2]

static string[] CommandLine.UnParserExtensions.FormatCommandLineArgs< T > ( this Parser  parser,
options,
Action< UnParserSettings configuration 
)
static

Format a command line argument string[] from a parsed instance.

Template Parameters
TType of options .
Parameters
parserParser instance.
optionsA parsed (or manually correctly constructed instance).
configurationThe Action<UnParserSettings> lambda used to configure aspects and behaviors of the unparsersing process.
Returns
A string[] with command line arguments.

Definition at line 247 of file UnParserExtensions.cs.

250 {
251 return FormatCommandLine(parser, options, configuration)
252 .SplitArgs();
253 }

◆ FormatName()

static string CommandLine.UnParserExtensions.FormatName ( this OptionSpecification  optionSpec,
object  value,
UnParserSettings  settings 
)
staticprivate

Definition at line 320 of file UnParserExtensions.cs.

321 {
322 // Have a long name and short name not preferred? Go with long!
323 // No short name? Has to be long!
324 bool longName = (optionSpec.LongName.Length > 0 && !settings.PreferShortName) ||
325 optionSpec.ShortName.Length == 0;
326
327 string formattedName =
328 new StringBuilder(longName
329 ? "--".JoinTo(optionSpec.LongName)
330 : "-".JoinTo(optionSpec.ShortName)
331 )
332 .AppendWhen(optionSpec.TargetType != TargetType.Switch,
333 longName && settings.UseEqualToken ? "=" : " "
334 )
335 .ToString();
336
337 return optionSpec.FlagCounter
338 ? string.Join(" ", Enumerable.Repeat(formattedName, (int)value))
339 : formattedName;
340 }

◆ FormatOption()

static string CommandLine.UnParserExtensions.FormatOption ( OptionSpecification  spec,
object  value,
UnParserSettings  settings 
)
staticprivate

Definition at line 312 of file UnParserExtensions.cs.

313 {
314 return new StringBuilder()
315 .Append(spec.FormatName(value, settings))
316 .AppendWhen(spec.TargetType != TargetType.Switch, FormatValue(spec, value))
317 .ToString();
318 }

◆ FormatValue()

static string CommandLine.UnParserExtensions.FormatValue ( Specification  spec,
object  value 
)
staticprivate

Definition at line 255 of file UnParserExtensions.cs.

256 {
257 StringBuilder builder = new StringBuilder();
258
259 switch (spec.TargetType)
260 {
261 case TargetType.Scalar:
262 builder.Append(FormatWithQuotesIfString(value));
263
264 break;
265 case TargetType.Sequence:
266 char sep = spec.SeperatorOrSpace();
267
268 Func<object, object> format = v
269 => sep == ' ' ? FormatWithQuotesIfString(v) : v;
270 IEnumerator e = ((IEnumerable)value).GetEnumerator();
271
272 while (e.MoveNext())
273 {
274 builder.Append(format(e.Current))
275 .Append(sep);
276 }
277
278 builder.TrimEndIfMatch(sep);
279
280 break;
281 }
282
283 return builder.ToString();
284 }
static object FormatWithQuotesIfString(object value)

◆ FormatWithQuotesIfString()

static object CommandLine.UnParserExtensions.FormatWithQuotesIfString ( object  value)
staticprivate

Definition at line 286 of file UnParserExtensions.cs.

287 {
288 string s = value.ToString();
289
290 if (!string.IsNullOrEmpty(s) && !s.Contains("\"") && s.Contains(" "))
291 {
292 return $"\"{s}\"";
293 }
294
295 Func<string, string> doubQt = v
296 => v.Contains("\"") ? v.Replace("\"", "\\\"") : v;
297
298 return s.ToMaybe()
299 .MapValueOrDefault(v => v.Contains(' ') || v.Contains("\"")
300 ? "\"".JoinTo(doubQt(v), "\"")
301 : v,
302 value
303 );
304 }

◆ IsEmpty()

static bool CommandLine.UnParserExtensions.IsEmpty ( this object  value,
Specification  specification,
bool  skipDefault 
)
staticprivate

Definition at line 355 of file UnParserExtensions.cs.

356 {
357 if (value == null)
358 {
359 return true;
360 }
361
362 if (skipDefault && value.Equals(specification.DefaultValue.FromJust()))
363 {
364 return true;
365 }
366
367 if (Nullable.GetUnderlyingType(specification.ConversionType) != null)
368 {
369 return false; //nullable
370 }
371
372#if !SKIP_FSHARP
373 if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) return true;
374#endif
375 if (value is ValueType &&
376 value.Equals(value.GetType()
377 .GetDefaultValue()
378 ))
379 {
380 return true;
381 }
382
383 if (value is string && ((string)value).Length == 0)
384 {
385 return true;
386 }
387
388 if (value is IEnumerable &&
389 !((IEnumerable)value).GetEnumerator()
390 .MoveNext())
391 {
392 return true;
393 }
394
395 return false;
396 }
Type ConversionType
This information is denormalized to decouple Specification from PropertyInfo.

◆ NormalizeValue()

static object CommandLine.UnParserExtensions.NormalizeValue ( this object  value)
staticprivate

Definition at line 342 of file UnParserExtensions.cs.

343 {
344#if !SKIP_FSHARP
345 if (value != null
346 && ReflectionHelper.IsFSharpOptionType(value.GetType())
347 && FSharpOptionHelper.IsSome(value))
348 {
349 return FSharpOptionHelper.ValueOf(value);
350 }
351#endif
352 return value;
353 }

◆ SeperatorOrSpace()

static char CommandLine.UnParserExtensions.SeperatorOrSpace ( this Specification  spec)
staticprivate

Definition at line 306 of file UnParserExtensions.cs.

307 {
308 return (spec as OptionSpecification).ToMaybe()
309 .MapValueOrDefault(o => o.Separator != '\0' ? o.Separator : ' ', ' ');
310 }

◆ SplitArgs()

static string[] CommandLine.UnParserExtensions.SplitArgs ( this string  command,
bool  keepQuote = false 
)
static

Returns a string array that contains the substrings in this instance that are delimited by space considering string between double quote.

Parameters
commandthe commandline string
keepQuotedon't remove the quote
Returns
a string array that contains the substrings in this instance

Definition at line 408 of file UnParserExtensions.cs.

409 {
410 if (string.IsNullOrEmpty(command))
411 {
412 return new string[0];
413 }
414
415 bool inQuote = false;
416
417 char[] chars = command.ToCharArray()
418 .Select(v =>
419 {
420 if (v == '"')
421 {
422 inQuote = !inQuote;
423 }
424
425 return !inQuote && v == ' ' ? '\n' : v;
426 }
427 )
428 .ToArray();
429
430 return new string(chars).Split('\n')
431 .Select(x => keepQuote ? x : x.Trim('"'))
432 .Where(x => !string.IsNullOrWhiteSpace(x))
433 .ToArray();
434 }

The documentation for this class was generated from the following file: