Example #1
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 #2
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());
        }