4using System.Collections;
108 return parser.FormatCommandLine(options, config => { });
120 return parser.FormatCommandLine(options, config => { })
139 throw new ArgumentNullException(
"options");
143 configuration(settings);
144 settings.Consumed =
true;
146 Type type = options.GetType();
147 StringBuilder builder =
new StringBuilder();
149 type.GetVerbSpecification()
150 .MapValueOrDefault(verb => builder.Append(verb.Name)
157 type.GetSpecifications(pi =>
new
160 Value = pi.GetValue(options,
null)
162 PropertyValue = pi.GetValue(options,
null),
165 where !info.PropertyValue.IsEmpty(info.Specification, settings.
SkipDefault)
169 var allOptSpecs = from info in specs.Where(i => i.Specification.Tag ==
SpecificationType.Option)
172 (o.TargetType ==
TargetType.Switch && o.FlagCounter && (
int)info.Value > 0) ||
173 (o.TargetType ==
TargetType.Switch && (
bool)info.Value)
175 orderby o.UniqueName()
178 var shortSwitches = from info in allOptSpecs
181 where o.ShortName.Length > 0
182 orderby o.UniqueName()
185 var optSpecs = settings.GroupSwitches
186 ? allOptSpecs.Where(info => !shortSwitches.Contains(info))
189 var valSpecs = from info in specs.Where(i => i.Specification.Tag ==
SpecificationType.Value)
194 builder = settings.GroupSwitches && shortSwitches.Any()
195 ? builder.Append(
'-')
196 .Append(
string.Join(
string.Empty,
197 shortSwitches.Select(info =>
199 OptionSpecification o =
200 (OptionSpecification)info
219 optSpecs.ForEach(opt =>
227 valSpecs.ForEach(val => builder.Append(
FormatValue(val.Specification, val.Value))
249 Action<UnParserSettings> configuration)
251 return FormatCommandLine(parser, options, configuration)
257 StringBuilder builder =
new StringBuilder();
266 char sep = spec.SeperatorOrSpace();
268 Func<object, object> format = v
270 IEnumerator e = ((IEnumerable)value).GetEnumerator();
274 builder.Append(format(e.Current))
278 builder.TrimEndIfMatch(sep);
283 return builder.ToString();
288 string s = value.ToString();
290 if (!
string.IsNullOrEmpty(s) && !s.Contains(
"\"") && s.Contains(
" "))
295 Func<string, string> doubQt = v
296 => v.Contains(
"\"") ? v.Replace(
"\"",
"\\\"") : v;
299 .MapValueOrDefault(v => v.Contains(
' ') || v.Contains(
"\"")
300 ?
"\"".JoinTo(doubQt(v),
"\"")
309 .MapValueOrDefault(o => o.Separator !=
'\0' ? o.Separator :
' ',
' ');
314 return new StringBuilder()
315 .Append(spec.FormatName(value, settings))
324 bool longName = (optionSpec.LongName.Length > 0 && !settings.
PreferShortName) ||
327 string formattedName =
328 new StringBuilder(longName
337 return optionSpec.FlagCounter
338 ?
string.Join(
" ", Enumerable.Repeat(formattedName, (
int)value))
362 if (skipDefault && value.Equals(specification.
DefaultValue.FromJust()))
367 if (Nullable.GetUnderlyingType(specification.
ConversionType) !=
null)
375 if (value is ValueType &&
376 value.Equals(value.GetType()
383 if (value is
string && ((
string)value).Length == 0)
388 if (value is IEnumerable &&
389 !((IEnumerable)value).GetEnumerator()
408 public static string[]
SplitArgs(
this string command,
bool keepQuote =
false)
410 if (
string.IsNullOrEmpty(command))
412 return new string[0];
415 bool inQuote =
false;
417 char[] chars = command.ToCharArray()
425 return !inQuote && v ==
' ' ?
'\n' : v;
430 return new string(chars).Split(
'\n')
431 .Select(x => keepQuote ? x : x.Trim(
'"'))
432 .Where(x => !
string.IsNullOrWhiteSpace(x))
static Specification FromProperty(PropertyInfo property)
Maybe< object > DefaultValue
Type ConversionType
This information is denormalized to decouple Specification from PropertyInfo.
static bool IsSome(object value)
static object ValueOf(object value)
static bool IsFSharpOptionType(Type type)
Provides methods to parse command line arguments.
ParserSettings Settings
Gets the instance that implements CommandLine.ParserSettings in use.
bool EnableDashDash
Gets or sets a value indicating whether enable double dash '–' syntax, that forces parsing of all sub...
Provides overloads to unparse options 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 cons...
static bool IsEmpty(this object value, Specification specification, bool skipDefault)
static string FormatName(this OptionSpecification optionSpec, object value, UnParserSettings settings)
static string FormatValue(Specification spec, object value)
static object FormatWithQuotesIfString(object value)
static string FormatOption(OptionSpecification spec, object value, UnParserSettings settings)
static object NormalizeValue(this object value)
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)
Format a command line argument string from a parsed instance.
static char SeperatorOrSpace(this Specification spec)
Provides settings for when formatting command line from an options instance../>.
static UnParserSettings WithGroupSwitchesOnly()
Factory method that creates an instance of CommandLine.UnParserSettings with GroupSwitches set to tru...
bool PreferShortName
Gets or sets a value indicating whether unparsing process shall prefer short or long names.
bool UseEqualToken
Gets or sets a value indicating whether unparsing process shall use equal sign with long names.
bool ShowHidden
Gets or sets a value indicating whether unparsing process shall expose hidden options.
bool SkipDefault
Gets or sets a value indicating whether unparsing process shall skip options with DefaultValue.
static UnParserSettings WithUseEqualTokenOnly()
Factory method that creates an instance of CommandLine.UnParserSettings with UseEqualToken set to tru...
bool GroupSwitches
Gets or sets a value indicating whether unparsing process shall group switches.