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());
 }
コード例 #4
0
        /// <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));
        }
コード例 #6
0
        /// <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));
        }
コード例 #7
0
        /// <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));
        }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
        /// <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));
        }
コード例 #10
0
 /// <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);
 }
コード例 #11
0
        /// <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());
        }
コード例 #12
0
        /// <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);
        }
コード例 #13
0
        void BindAnyAdditionalArgs(ParsedOption option)
        {
            if (!this.HasAdditionalArgumentsCallback)
            {
                return;
            }

            if (option.AdditionalValues.Any())
            {
                this.AdditionalArgumentsCallback(option.AdditionalValues);
            }
        }
コード例 #14
0
        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);
        }
コード例 #15
0
 /// <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));
 }
コード例 #16
0
        /// <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));
        }
コード例 #17
0
        /// <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));
        }
コード例 #18
0
 /// <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);
     }
 }
コード例 #19
0
        /// <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("\"")));
        }
コード例 #21
0
        /// <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);
            }));
        }
コード例 #22
0
        /// <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);
            }
        }
コード例 #23
0
        /// <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);
            }));
        }
コード例 #24
0
 /// <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));
コード例 #27
0
 /// <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);
 }
コード例 #28
0
ファイル: ParsedOption.cs プロジェクト: vmafelt/ParLog
 /// <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));
 }
コード例 #29
0
 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);
 }
コード例 #30
0
 /// <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;
 }
コード例 #31
0
 /// <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);
 }
コード例 #33
0
		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);
		}