예제 #1
0
        /// <summary>
        /// Parses the command line argument
        /// </summary>
        /// <param name="args">Contains the file name to scan</param>
        /// <returns>Filename of the file to scan</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when args is null</exception>
        /// <exception cref="System.ArgumentException">Thrown when args is empty</exception>
        public static (ArgumentResponseItem ArgResponse, bool ValidOption) Parse(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!args.Any())
            {
                throw new ArgumentException("Args was empty");
            }

            var response = new ArgumentResponseItem(SupportedArguments);

            var incrementer = INCREMENTER_KEY_VALUE;

            for (var x = 0; x < args.Length; x += incrementer)
            {
                if (!args[x].StartsWith(ARGUMENT_PREFIX))
                {
                    Console.WriteLine($"Invalid option: {args[x]}");

                    continue;
                }

                var argumentKey = args[x].ToLower().Substring(1);

                var argument = SupportedArguments.FirstOrDefault(a => a.Argument == argumentKey);

                if (argument == null)
                {
                    Console.WriteLine($"Invalid option: {argumentKey}");

                    continue;
                }

                if (argument.KeyOnly)
                {
                    incrementer = INCREMENTER_KEY_ONLY;

                    continue;
                }

                incrementer = INCREMENTER_KEY_VALUE;

                if (args.Length < x + incrementer)
                {
                    Console.WriteLine($"{argumentKey} requires a value, but was not found");

                    throw new ArgumentException($"Associated value for {argumentKey} was not found");
                }

                var argumentValue = args[x + 1];

                if (!argument.ValidArgument(argumentValue))
                {
                    Console.WriteLine($"{argumentValue} is an invalid value for {argumentKey}");

                    continue;
                }

                response.UpdateProperty(argument.PropertyMap, argument.GetValue(argumentValue));
            }

            return(response, response.IsValid());
        }
예제 #2
0
        public void InvalidProperty()
        {
            var argumentResponse = new ArgumentResponseItem(new List <BaseArgument>());

            argumentResponse.UpdateProperty("blah", null);
        }