private bool TryParse(ParsedOption parsedOption, out bool result) { if (parsedOption.Value.IsNullOrWhiteSpace()) { // for the suffix: // "-" means the value should be false // "+" or any other suffix means the value should be true. // if we don't have a result = parsedOption.HasSuffix == false || parsedOption.Suffix != "-"; return(true); } if ("on".Equals(parsedOption.Value, StringComparison.OrdinalIgnoreCase)) { result = true; return(true); } if ("off".Equals(parsedOption.Value, StringComparison.OrdinalIgnoreCase)) { result = false; return(true); } return(bool.TryParse(parsedOption.Value, out result)); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.Boolean"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns> /// A <see cref="System.Boolean"/> representing the parsed value. /// The value is optional. If no value is provided then <c>true</c> is returned. /// </returns> public bool Parse(ParsedOption parsedOption) { if (parsedOption.Value.IsNullOrWhiteSpace()) { // for the suffix: // "-" means the value should be false // "+" or any other suffix means the value should be true. // if we don't have a return(parsedOption.HasSuffix == false || parsedOption.Suffix != "-"); } if (parsedOption.Value == "on") { return(true); } if (parsedOption.Value == "off") { return(false); } bool result; TryParse(parsedOption, out result); return(result); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.String"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns></returns> public string Parse(ParsedOption parsedOption) { if (parsedOption.Value == null) { return(null); } return(parsedOption.Value.RemoveAnyWrappingDoubleQuotes()); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { // if the key exists with no value then this translates as true. // if the key exists but has a value then we must try to parse the value bool result; return(parsedOption.Value.IsNullOrWhiteSpace() || bool.TryParse(parsedOption.Value, out result)); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { // if the key exists with no value then this translates as true. // if the key exists but has a value then we must try to parse the value bool result; return(TryParse(parsedOption, out result)); }
/// <summary> /// Parses the specified <see cref="ParsedOption"/> into a nullable type. /// </summary> public TNullableType?Parse(ParsedOption parsedOption) { var parser = _parserFactory.CreateParser <TNullableType>(); if (parser.CanParse(parsedOption) == false) { return(null); } return(parser.Parse(parsedOption)); }
/// <summary> /// Parses the specified <see cref="ParsedOption"/> into a nullable type. /// </summary> public bool?Parse(ParsedOption parsedOption) { var parser = _parserFactory.CreateParser <bool>(); if (parser.CanParse(parsedOption) == false) { return(null); } return(parser.Parse(parsedOption)); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.Boolean"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns> /// A <see cref="System.Boolean"/> representing the parsed value. /// The value is optional. If no value is provided then <c>true</c> is returned. /// </returns> public TEnum Parse(ParsedOption parsedOption) { int result = 0; foreach (var value in parsedOption.Values) { result += (int)Enum.Parse(typeof(TEnum), value.ToLowerInvariant(), true); } return((TEnum)(object)result); }
/// <summary> /// Parses the specified <see cref="System.String"/> into the return type. /// </summary> /// <param name="parsedOption"></param> /// <returns>The parsed value.</returns> public TEnum?Parse(ParsedOption parsedOption) { if (parsedOption.HasValue == false) { return(null); } var parser = _parserFactory.CreateParser <TEnum>(); return(parser.Parse(parsedOption)); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.String"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns></returns> public string Parse(ParsedOption parsedOption) { if (parsedOption.Value == null) { return(null); } return(parsedOption.Value.ContainsWhitespace() ? parsedOption.Value.RemoveAnyWrappingDoubleQuotes() : parsedOption.Value); }
/// <summary> /// Parses the specified <see cref="System.String"/> into the return type. /// </summary> /// <param name="parsedOption"></param> /// <returns>The parsed value.</returns> public List <T> Parse(ParsedOption parsedOption) { var parser = _parserFactory.CreateParser <T>(); return(parsedOption.Values.Select(value => { var clone = parsedOption.Clone(); clone.Value = value; return parser.Parse(clone); }).ToList()); }
/// <summary> /// Binds the specified <see cref="System.String"/> to the Option. /// </summary> /// <param name="value">The <see cref="System.String"/> to bind.</param> public void Bind(ParsedOption value) { if (this.Parser.CanParse(value) == false) { throw new OptionSyntaxException(); } this.Bind(this.Parser.Parse(value)); this.BindAnyAdditionalArgs(value); }
void BindAnyAdditionalArgs(ParsedOption option) { if (!this.HasAdditionalArgumentsCallback) { return; } if (option.AdditionalValues.Any()) { this.AdditionalArgumentsCallback(option.AdditionalValues); } }
public void Ensure_That_If_Value_Is_Whitespace_Cannot_Be_Parsed_And_No_Default_Set_Then_optionSyntaxException_Is_Thrown() { var option = new ParsedOption(); const string value = " "; var mockParser = new Mock <ICommandLineOptionParser <string> >(); mockParser.Setup(x => x.CanParse(option)).Returns(false); var target = new CommandLineOption <string>("s", "long name", mockParser.Object); target.Bind(option); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption.HasValue == false) { return(false); } if (parsedOption.Value.IsNullOrWhiteSpace()) { return(false); } return(IsDefined(parsedOption.Value)); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.Boolean"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns> /// A <see cref="System.Boolean"/> representing the parsed value. /// The value is optional. If no value is provided then <c>true</c> is returned. /// </returns> public bool Parse(ParsedOption parsedOption) { if (parsedOption.Value.IsNullOrWhiteSpace()) { // for the suffix: // "-" means the value should be false // "+" or any other suffix means the value should be true. // if we don't have a return(parsedOption.HasSuffix == false || parsedOption.Suffix != "-"); } return(bool.Parse(parsedOption.Value)); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption == null) { return(false); } if (parsedOption.HasValue == false) { return(true); } var parser = _parserFactory.CreateParser <TEnum>(); return(parser.CanParse(parsedOption)); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { try { new Uri(parsedOption.Value); return(true); } catch (ArgumentNullException) { return(false); } catch (UriFormatException) { return(false); } }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption.Value.IsNullOrWhiteSpace()) { return(true); } if (parsedOption.HasValue == false) { return(true); } string value = (parsedOption.Value ?? "").Trim(); var items = value.SplitOnWhitespace(); return(items.Count() == 1); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption.Value.IsNullOrWhiteSpace()) { return(true); } if (parsedOption.HasValue == false) { return(true); } var value = (parsedOption.Value ?? "").Trim(); var items = value.SplitOnWhitespace(); return(items.Count() == 1 || (value.StartsWith("\"") && value.EndsWith("\""))); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption == null) { return(false); } if (parsedOption.HasValue == false) { return(false); } return(parsedOption.Values.All(value => { if (value.IsNullOrWhiteSpace()) { return false; } return IsDefined(value); })); }
/// <summary> /// Binds the specified <see cref="System.String"/> to the Option. /// </summary> /// <param name="value">The <see cref="System.String"/> to bind.</param> public void Bind(ParsedOption value) { if (this.Parser.CanParse(value) == false) { throw new OptionSyntaxException(); } var input = this.Parser.Parse(value); if (typeof(T).IsClass && object.Equals(input, default(T)) && this.HasDefault) { BindDefault(); } else { this.Bind(input); this.BindAnyAdditionalArgs(value); } }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { if (parsedOption == null) { return(false); } if (parsedOption.HasValue == false) { return(false); } var parser = _parserFactory.CreateParser <T>(); return(parsedOption.Values.All(value => { var clone = parsedOption.Clone(); clone.Value = value; clone.Values = new[] { value }; clone.AdditionalValues = new string[0]; return parser.CanParse(clone); })); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.Boolean"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns> /// A <see cref="System.Boolean"/> representing the parsed value. /// The value is optional. If no value is provided then <c>true</c> is returned. /// </returns> public TEnum Parse(ParsedOption parsedOption) { return((TEnum)Enum.Parse(typeof(TEnum), parsedOption.Value.ToLowerInvariant(), true)); }
/// <summary> /// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns> public bool CanParse(ParsedOption parsedOption) { return(TimeSpan.TryParse(TrimAnyUnwantedCharacters(parsedOption.Value), out var dtOut)); }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.TimeSpan"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns></returns> public TimeSpan Parse(ParsedOption parsedOption) => TimeSpan.Parse(TrimAnyUnwantedCharacters(parsedOption.Value));
/// <summary> /// Determines whether two specified <see cref="ParsedOption"/> objects have the same values. /// </summary> /// <param name="other">The other <see cref="ParsedOption"/> to compare.</param> /// <returns>true if they are equal; otherwise false.</returns> protected bool Equals(ParsedOption other) { return string.Equals(Key, other.Key) && string.Equals(Value, other.Value); }
/// <summary> /// Determines whether two specified <see cref="ParsedOption"/> objects have the same values. /// </summary> /// <param name="other">The other <see cref="ParsedOption"/> to compare.</param> /// <returns>true if they are equal; otherwise false.</returns> protected bool Equals(ParsedOption other) { return(string.Equals(Key, other.Key) && string.Equals(Value, other.Value)); }
private void AssertOption(PrefixKind expectedPrefix, string expectedKey, string expectedValue, ParsedOption actualOption) { Assert.Equal(expectedPrefix, actualOption.PrefixKind); Assert.Equal(expectedKey, actualOption.Key); Assert.Equal(expectedValue, actualOption.Value); }
/// <summary> /// Initialises a new instance of the <see cref="CommandLineParserErrorBase"/> class. /// </summary> /// <param name="cmdOption">The <see cref="ICommandLineOption"/> this error relates too. This must not be <c>null</c>.</param> /// <param name="parsedOption">The parsed option that caused the error.</param> /// <exception cref="ArgumentNullException">If <paramref name="cmdOption"/> is <c>null</c>.</exception> public OptionSyntaxParseError(ICommandLineOption cmdOption, ParsedOption parsedOption) : base(cmdOption) { ParsedOption = parsedOption; }
/// <summary> /// Parses the specified <see cref="System.String"/> into a <see cref="System.Uri"/>. /// </summary> /// <param name="parsedOption"></param> /// <returns> /// A <see cref="System.Uri"/> representing the parsed value. /// The value is optional. If no value is provided then <c>true</c> is returned. /// </returns> public Uri Parse(ParsedOption parsedOption) { return(new Uri(parsedOption.Value)); }
/// <summary> /// Determines whether the specified <see cref="ParsedOption"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>. /// </summary> public bool CanParse(ParsedOption parsedOption) { return(true); }
public void Ensure_That_If_Value_Is_Whitespace_Cannot_Be_Parsed_And_No_Default_Set_Then_optionSyntaxException_Is_Thrown() { var option = new ParsedOption(); const string value = " "; var mockParser = new Mock<ICommandLineOptionParser<string>>(); mockParser.Setup(x => x.CanParse(option)).Returns(false); var target = new CommandLineOption<string>("s", "long name", mockParser.Object); target.Bind(option); }