Example #1
0
        private void SetAggregatedOptions(object commandObject, Queue <string> argQ, string options, string value)
        {
            var argsRequired = 0;

            options = options.Substring(1);
            foreach (var option in options)
            {
                var optionProperty = AssemblySearcher
                                     .GetCommandOptionPropertyByName(
                    commandObject.GetType(),
                    option.ToString()).First();

                if (optionProperty.PropertyType == typeof(bool))
                {
                    optionProperty.SetValue(commandObject, true);
                }
                else if (optionProperty.PropertyType == typeof(string))
                {
                    optionProperty.SetValue(commandObject, value ?? argQ.Dequeue());
                    argsRequired++;
                }
                else if (_converterFuncs.ContainsKey(optionProperty.PropertyType))
                {
                    optionProperty.SetValue(commandObject, _converterFuncs[optionProperty.PropertyType](value ?? argQ.Dequeue()));
                    argsRequired++;
                }

                if (argsRequired > 1)
                {
                    throw new ArgumentException("Aggregated options can only contain a single option that requires a value. Multiple were present.");
                }
            }
        }
Example #2
0
        public static void ShowUsageForAll()
        {
            var cmdTypes = AssemblySearcher
                           .GetCommandClasses()
                           .OrderBy(t => AssemblySearcher.GetCommandName(t));

            //foreach type get the names of the parameters and create a printout
            foreach (var type in cmdTypes)
            {
                var cmdName = AssemblySearcher.GetCommandName(type);
                Console.WriteLine($"{cmdName} command");
                var desc = type.GetCustomAttributes(typeof(CommandClassAttribute), false).Cast <CommandClassAttribute>().First().ShortDescription;
                if (desc?.Length > 0)
                {
                    Console.WriteLine(desc);
                }

                Console.WriteLine($"usage:");

                foreach (var method in AssemblySearcher.GetCommandMethods(type))
                {
                    var           handlerDesc = method.GetCustomAttributes(typeof(CommandHandlerAttribute), false).Cast <CommandHandlerAttribute>().First().ShortDescription;
                    StringBuilder sb          = new StringBuilder();
                    sb.Append(cmdName).Append(" ");
                    foreach (var param in method.GetParameters())
                    {
                        sb.Append(param.Name).Append(" ");
                    }
                    Console.WriteLine($"  {sb,-60} {handlerDesc}");
                }
                Console.WriteLine("\n");
            }
        }
Example #3
0
        private Type RetrieveTypeForCommand(string command)
        {
            var possibleCommandHandlers = AssemblySearcher.GetCommandClasses(command, IgnoreCommandCase);

            if (possibleCommandHandlers.Count() > 1)
            {
                throw new Exception($"More than one class assigned to handle command {command}");
            }

            if (possibleCommandHandlers.Count() < 1)
            {
                throw new Exception($"No handler registered for command: {command}");
            }

            return(possibleCommandHandlers.First());
        }
Example #4
0
        private void SetFullOption(object commandObject, Queue <string> argQ, string options, string value)
        {
            var optionProperty = AssemblySearcher
                                 .GetCommandOptionPropertyByName(
                commandObject.GetType(),
                options.Substring(2)).First();

            if (optionProperty.PropertyType == typeof(bool))
            {
                optionProperty.SetValue(commandObject, true);
            }
            else if (optionProperty.PropertyType == typeof(string))
            {
                optionProperty.SetValue(commandObject, value ?? argQ.Dequeue());
            }
            else if (_converterFuncs.ContainsKey(optionProperty.PropertyType))
            {
                optionProperty.SetValue(commandObject, _converterFuncs[optionProperty.PropertyType](value ?? argQ.Dequeue()));
            }
        }
Example #5
0
        private void InvokeHandlerMethod(object cmd, IEnumerable <string> paramArgs)
        {
            var possibleHandlerMethods = AssemblySearcher.GetCommandMethods(cmd.GetType(), paramArgs.Count());

            //Temporary exception condition. Eventually we will just try multiple handlers starting with
            //the most specific signatures (the one that requires the most type conversion)
            if (possibleHandlerMethods.Count() > 1)
            {
                throw new Exception("Multiple handlers exist with the same signature. Could not determine which handler to call.");
            }

            if (possibleHandlerMethods.Count() < 1)
            {
                throw new Exception("No handler was found for the given arguments.");
            }

            var handlerMethod = possibleHandlerMethods.First();

            List <object> convertedParams = ConvertParams(handlerMethod.GetParameters(), paramArgs);

            handlerMethod.Invoke(cmd, convertedParams.ToArray());
        }