Exemple #1
0
        public static void Parse(string[] tokens, ArgumentValue[] arguments)
        {
            ThrowHelper.CheckAtMostOneDefault(arguments);
            ThrowHelper.CheckConvertible(arguments);

            Dictionary<ArgumentValue, int> argumentsToIndexes =
                GetArgumentsToIndexes(tokens, arguments);

            foreach (var argumentsToIndex in argumentsToIndexes)
            {
                if (argumentsToIndex.Key.IsFlag)
                {
                    argumentsToIndex.Key.Value = true;
                }
                else
                {
                    string value =
                        ParseArgumentValue(tokens, argumentsToIndex, argumentsToIndexes);

                    argumentsToIndex.Key.Value =
                        Convert.ChangeType(value, argumentsToIndex.Key.Type);
                }
            }

            ThrowHelper.CheckNonOptional(arguments);
        }
Exemple #2
0
        private static Dictionary<ArgumentValue, int> GetArgumentsToIndexes(string[] tokens, ArgumentValue[] arguments)
        {
            Dictionary<ArgumentValue, int> withoutDefault =
                GetWithoutDefault(tokens, arguments);

            ArgumentValue defaultParameter =
                arguments.FirstOrDefault(x => x.IsDefault);

            if (tokens.Any() &&
                (defaultParameter != null) &&
                !withoutDefault.ContainsKey(defaultParameter) &&
                !withoutDefault.ContainsValue(0))
            {
                withoutDefault[defaultParameter] = 0;
            }

            return withoutDefault;
        }
Exemple #3
0
        private static Dictionary<ArgumentValue, int> GetWithoutDefault(string[] tokens, ArgumentValue[] arguments)
        {
            var argumentsToIndexes =
                tokens.Select
                    ((token, index) =>
                     new
                         {
                             Index = index,
                             Argument = arguments.FirstOrDefault(
                                 argument => argument.Aliases.Any(
                                     alias => token.StartsWith(alias, StringComparison.InvariantCultureIgnoreCase)))
                         })
                      .Where(x => x.Argument != null)
                      .ToDictionary(x => x.Argument,
                                    x => x.Index);

            return argumentsToIndexes;
        }
Exemple #4
0
            public static void CheckNonOptional(ArgumentValue[] arguments)
            {
                ArgumentValue missingValue =
                    arguments.FirstOrDefault(x => !x.IsOptional && !x.HasValue);

                if (missingValue != null)
                {
                    throw new ArgumentException(
                        string.Format("The argument {0} isn't optional and has no value",
                                      missingValue.Name));
                }
            }
Exemple #5
0
            public static void CheckConvertible(ArgumentValue[] arguments)
            {
                ArgumentValue notConvertible =
                    arguments.FirstOrDefault(x => !x.IsOptional && !typeof (IConvertible).IsAssignableFrom(x.Type));

                if (notConvertible != null)
                {
                    throw new ArgumentException(
                        string.Format("The argument {0} is of type {1} which isn't convertible",
                                      notConvertible.Name,
                                      notConvertible.Type));
                }
            }
Exemple #6
0
 public static void CheckAtMostOneDefault(ArgumentValue[] arguments)
 {
     if (arguments.Where(x => x.IsDefault).Skip(1).Any())
     {
         throw new ArgumentException("There exists more than one default parameter");
     }
 }