Beispiel #1
0
        /// <summary>
        /// Parses the specified <see><cref>T:System.String[]</cref></see> into appropriate <see cref="ParsedOption"/> objects..
        /// </summary>
        /// <param name="args">The <see><cref>T:System.String[]</cref></see> to parse.</param>
        /// <returns>An <see cref="ParserEngineResult"/> representing the results of the parse operation.</returns>
        public ParserEngineResult Parse(string[] args)
        {
            args = args ?? new string[0];
            var list = new List<ParsedOption>();

            for (int index = 0; index < args.Length; index++)
            {
                string currentArg = args[index];

                // we only want to find keys at this point
                string prefix = ExtractPrefix(currentArg);

                if (prefix == null) continue;

                var parsedOption = new ParsedOption
                {
                    RawKey = currentArg,
                    Prefix = prefix,
                    Key = currentArg.Remove(0, prefix.Length),
                    Suffix = ExtractSuffix(currentArg)
                };

                TrimSuffix(parsedOption);

                DetermineOptionValue(args, index, parsedOption);

                var needToSplitKey = PrefixIsShortOption(prefix) && parsedOption.Key.Length > 1;

                if (needToSplitKey)
                    list.AddRange(CloneAndSplit(parsedOption));
                else
                    list.Add(parsedOption);
            }

            return new ParserEngineResult(list, null);
        }
Beispiel #2
0
 private static IEnumerable<ParsedOption> CloneAndSplit(ParsedOption parsedOption)
 {
     return parsedOption.Key.Select(c =>
     {
         var clone = parsedOption.Clone();
         clone.Key = new string(new[] { c });
         return clone;
     }).ToList();
 }
Beispiel #3
0
        private static void TryGetValueFromKey(ParsedOption option)
        {
            var splitted = option.Key.Split(SpecialCharacters.ValueAssignments, 2, StringSplitOptions.RemoveEmptyEntries);

            option.Key = splitted[0];

            if (splitted.Length > 1)
                option.Value = splitted[1].WrapInDoubleQuotesIfContainsWhitespace();
        }
Beispiel #4
0
 private static void TrimSuffix(ParsedOption parsedOption)
 {
     if (parsedOption.HasSuffix)
     {
         parsedOption.Key = parsedOption.Key.TrimEnd(parsedOption.Suffix.ToCharArray());
     }
 }
Beispiel #5
0
        static void DetermineOptionValue(string[] args, int currentIndex, ParsedOption option)
        {
            if (SpecialCharacters.ValueAssignments.Any(option.Key.Contains))
            {
                TryGetValueFromKey(option);
            }

            var allValues = new List<string>();
            var additionalValues = new List<string>();

            var otherValues = CombineValuesUntilNextKey(args, currentIndex + 1);

            if (option.HasValue) allValues.Add(option.Value);

            if (otherValues.IsNullOrEmpty() == false)
            {
                allValues.AddRange(otherValues);

                if (otherValues.Count() > 1)
                {
                    additionalValues.AddRange(otherValues);
                    additionalValues.RemoveAt(0);
                }
            }

            option.Value = allValues.FirstOrDefault();
            option.Values = allValues.ToArray();
            option.AddtionalValues = additionalValues.ToArray();
        }
        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 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);
 }