Esempio n. 1
0
        public CommandResponse StoreValidOptions()
        {
            if (_args.Length == 0)
            {
                return CommandResponse.Set(false, "Please enter valid arguments.");
            }

            for (var a = 0; a < _args.Length; a++)
            {
                //Only interested in option names.
                //If not a valid option name then the command is probably incorrect
                if (!IsEven(a))
                {
                    continue;
                }

                var arg = _args[a];
                if (!string.IsNullOrWhiteSpace(arg))
                {
                    var option = _args.ElementAt(a).Replace("-", "").ToLowerInvariant();
                    var optionValueKey = a + 1;

                    //Check if is valid option
                    var validOption = ValidOptions.FirstOrDefault(k => k.Key.Equals(option) || k.Key.First().ToString() == option);
                    
                    if (validOption.Key == null)
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} is unknown.");
                    }

                    //Checks to see if last OPTION has a value
                    //Apart from -v & -h all other options should have a value
                    if (optionValueKey > _args.Length - 1)
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} has not been supplied with a value.");
                    }

                    var optionValue = _args.ElementAt(optionValueKey);

                    if (!string.IsNullOrEmpty(optionValue) && !SensitiveOptions.Contains(validOption.Key))
                    {
                        optionValue = optionValue.ToLowerInvariant();
                    }

                    //If the OPTION has set values, check the incoming against this list
                    if (validOption.Key != "parameters" && validOption.Key != "setheaders" && validOption.Value.Any() && optionValue != null && !validOption.Value.Contains(optionValue))
                    {
                        return CommandResponse.Set(false, $"The OPTION {option} value is invalid.");
                    }

                    //Store the Option values
                    //Doing this here because we already have the key/values parsed
                    StoreOptionValue(validOption.Key, optionValue);
                }
            }

            return CommandResponse.Set(true, "Command Validated.");
        }
Esempio n. 2
0
        private ValidOption GetLongOption(string longOpt)
        {
            switch (ValidOptions.Count(v => v.LongOption.StartsWith(longOpt)))
            {
            case 0:
                throw new InvalidOptionException(string.Format("{0} is not a valid option", longOpt));

            case 1:
                return(ValidOptions.FirstOrDefault(v => v.LongOption.StartsWith(longOpt)));

            default:
                string message = string.Format("{0} could be interpreted as {1}", longOpt,
                                               string.Join(" or ", (from v in ValidOptions
                                                                    where v.LongOption.StartsWith(longOpt)
                                                                    select v.LongOption).ToList()));
                throw new AmbiguousOptionException(message);
            }
        }
Esempio n. 3
0
        private IDictionary<string, string> GetOptionDictionay(string key, string value)
        {
            var paramsList = value?.Split(",") ?? new string[0];

            var parameters = new Dictionary<string, string>();

            for (var p = 0; p < paramsList.Length; p++)
            {
                var keyValParam = paramsList.ElementAt(p).Split("|");

                var validOptions = ValidOptions.FirstOrDefault(k => k.Key.Equals(key));

                if (keyValParam.Length == 2 && validOptions.Value.Contains(keyValParam.ElementAt(0)))
                {
                    parameters.Add(keyValParam.ElementAt(0), keyValParam.ElementAt(1));
                }
            }

            return parameters;
        }