Exemple #1
0
        public static bool Parse(ParameterDefinitions definitions, string[] values, out ParameterValues output)
        {
            if (definitions == null)
            {
                throw new ArgumentNullException(nameof(definitions));
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (!AreTokensValid(definitions, values))
            {
                output = null;
                return(false);
            }

            ParameterValues parameters = new ParameterValues(definitions.Count);

            List <object> paramValues = new List <object>();

            for (int i = 0; i < definitions.Count; i++)
            {
                ParamDef definition = definitions[i];
                string   value      = values.Get(i, null);

                if (string.IsNullOrEmpty(value)) // Apply default value.
                {
                    value = definition.DefaultValue?.ToString();
                }

                if (value != null)
                {
                    parameters.lookup.Add(definition.Name, i);

                    object val = ConvertToken(definition, value);

                    parameters.objects[i] = val ?? value;
                }
                else
                {
                    throw new Exception("Error creating params!");
                }
            }

            output = parameters;
            return(true);
        }
Exemple #2
0
        public static bool AreTokensValid(ParameterDefinitions definitions, string[] tokens)
        {
            if (definitions == null)
            {
                throw new ArgumentNullException(nameof(definitions));
            }
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            if (tokens.Length > definitions.Count)
            {
                return(false);
            }

            for (int i = 0; i < definitions.Count; i++)
            {
                ParamDef definition = definitions[i];
                string   token      = tokens.Get(i, null);

                if (token != null && token.Length > 0)
                {
                    if (!IsTokenValid(definition, token))
                    {
                        return(false);
                    }
                }
                else if (definition.DefaultValue == null)     // Required param.
                {
                    return(false);
                }
            }

            return(true);
        }