/// <summary> /// Sets the value of a property from the next value of the given /// enumerator. Optionally applies a transformation function /// to the argument before assigning. /// </summary> private static void SetValue <T>(object container, CliOptionInternal option, IEnumerator <string> argsEnumerator, Func <string, T> transformation) { if (!option.PropertyType.IsAssignableFrom(typeof(T))) { throw new CliInternalException($"Internal Error when parsing cli " + $"options: Transformation function for option {option.Flags.First()} " + $"returns value of type {typeof(T).Name}, but the associated " + $"property is of type {option.PropertyType}."); } if (argsEnumerator.MoveNext()) { string rawValue = argsEnumerator.Current; T value = transformation(rawValue); option.Setter.Invoke(container, new object[] { value }); } else { throw new CliOptionsException($"Value expected for option " + $"{option.Flags.First()}."); } }
/// <summary> /// Sets the value of a property from the given command line arguments. /// </summary> /// <param name="argsEnumerator"> /// An enumerator over the command line arguments. /// The value of argsEnumerator.Current should be the name of the /// current option. /// </param> private static void SetPropertyFromArgs(object container, IEnumerator <string> argsEnumerator, CliOptionInternal option) { if (option.PropertyType == typeof(string)) { SetValue(container, option, argsEnumerator, x => x); } else if (option.PropertyType == typeof(bool)) { option.Setter.Invoke(container, new object[] { true }); } }
private static string GetHelpForOption(CliOptionInternal option) { var flags = string.Join(", ", option.Flags); return($"{flags}: {option.Description}"); }