static void Run()
        {
            while (true)
            {  
                var consoleInput = ReadFromConsole();
                if (string.IsNullOrWhiteSpace(consoleInput)) continue;

                try
                {
                    // Create a ConsoleCommand instance:
                    var cmd = new ConsoleCommand(consoleInput);

                    // Execute the command:
                    string result = Execute(cmd);

                    // Write out the result:
                    WriteToConsole(result);
                }
                catch (Exception ex)
                {
                    // OOPS! Something went wrong - Write out the problem:
                    WriteToConsole(ex.Message);
                }
            }
        }
Esempio n. 2
0
        static void Run()
        {
            AppState.SetState(State.RUNNING);
            while (AppState.GetState() > State.IDLE)
            {
                var consoleInput = ReadFromConsole();
                if (string.IsNullOrWhiteSpace(consoleInput))
                {
                    continue;
                }

                try
                {
                    // Create a ConsoleCommand instance:
                    var cmd = new ConsoleCommand(consoleInput);

                    switch (cmd.Name)
                    {
                    case "help":
                    case "?":
                    case "ayuda":
                        WriteToConsole(BuildHelpMessage());
                        break;

                    case "exit":
                    case "salir":
                        WriteToConsole("Closing program...");
                        return;

                    default:
                        // Execute the command:
                        string result = CommandHandler.Execute(cmd);

                        // Write out the result:
                        WriteToConsole(result);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    // OOPS! Something went wrong - Write out the problem:
                    WriteToConsole(ex.Message);
                }
            }
        }
Esempio n. 3
0
        private static bool ProcessInput(string consoleInput)
        {
            if (string.IsNullOrWhiteSpace(consoleInput))
            {
                return(false);
            }
            else if (consoleInput.Equals("q", StringComparison.OrdinalIgnoreCase) ||
                     consoleInput.Equals("quit", StringComparison.OrdinalIgnoreCase) ||
                     consoleInput.Equals("exit", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            // Create a ConsoleCommand instance:
            var cmd = new ConsoleCommand(consoleInput);

            // Execute the command:
            string result = Execute(cmd);

            // Write out the result:
            WriteToConsole(result);

            return(false);
        }
        private static void Run()
        {
            while (true)
            {
                var consoleInput = ReadFromConsole();

                if (string.IsNullOrWhiteSpace(consoleInput))
                {
                    continue;
                }

                try
                {
                    var    cmd    = new ConsoleCommand(consoleInput);
                    string result = Execute(cmd);

                    WriteToConsole(result);
                }
                catch (Exception ex)
                {
                    WriteToConsole(ex.Message);
                }
            }
        }
        static string Execute(ConsoleCommand command)
        {
            // Validate the class name and command name:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            string badCommandMessage = string.Format(""
                + "Unrecognized command \'{0}.{1}\'. "
                + "Please type a valid command.",
                command.LibraryClassName, command.Name);

            // Validate the command name:
            if (!_commandLibraries.ContainsKey(command.LibraryClassName))
            {
                return badCommandMessage;
            }
            var methodDictionary = _commandLibraries[command.LibraryClassName];
            if (!methodDictionary.ContainsKey(command.Name))
            {
                return badCommandMessage;
            }

            // Make sure the corret number of required arguments are provided:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            var methodParameterValueList = new List<object>();
            IEnumerable<ParameterInfo> paramInfoList = methodDictionary[command.Name].ToList();

            // Validate proper # of required arguments provided. Some may be optional:
            var requiredParams = paramInfoList.Where(p => p.IsOptional == false);
            var optionalParams = paramInfoList.Where(p => p.IsOptional == true);
            int requiredCount = requiredParams.Count();
            int optionalCount = optionalParams.Count();
            int providedCount = command.Arguments.Count();

            if (requiredCount > providedCount)
            {
                return string.Format(
                    "Missing required argument. {0} required, {1} optional, {2} provided",
                    requiredCount, optionalCount, providedCount);
            }

            // Make sure all arguments are coerced to the proper type, and that there is a 
            // value for every emthod parameter. The InvokeMember method fails if the number 
            // of arguments provided does not match the number of parameters in the 
            // method signature, even if some are optional:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            if (paramInfoList.Count() > 0)
            {
                // Populate the list with default values:
                foreach (var param in paramInfoList)
                {
                    // This will either add a null object reference if the param is required 
                    // by the method, or will set a default value for optional parameters. in 
                    // any case, there will be a value or null for each method argument 
                    // in the method signature:
                    methodParameterValueList.Add(param.DefaultValue);
                }

                // Now walk through all the arguments passed from the console and assign 
                // accordingly. Any optional arguments not provided have already been set to 
                // the default specified by the method signature:
                for (int i = 0; i < command.Arguments.Count(); i++)
                {
                    var methodParam = paramInfoList.ElementAt(i);
                    var typeRequired = methodParam.ParameterType;
                    object value = null;
                    try
                    {
                        // Coming from the Console, all of our arguments are passed in as 
                        // strings. Coerce to the type to match the method paramter:
                        value = CoerceArgument(typeRequired, command.Arguments.ElementAt(i));
                        methodParameterValueList.RemoveAt(i);
                        methodParameterValueList.Insert(i, value);
                    }
                    catch (ArgumentException ex)
                    {
                        string argumentName = methodParam.Name;
                        string argumentTypeName = typeRequired.Name;
                        string message =
                            string.Format(""
                            + "The value passed for argument '{0}' cannot be parsed to type '{1}'",
                            argumentName, argumentTypeName);
                        throw new ArgumentException(message);
                    }
                }
            }

            // Set up to invoke the method using reflection:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            Assembly current = typeof(Program).Assembly;

            // Need the full Namespace for this:
            Type commandLibaryClass =
                current.GetType(_commandNamespace + "." + command.LibraryClassName);

            object[] inputArgs = null;
            if (methodParameterValueList.Count > 0)
            {
                inputArgs = methodParameterValueList.ToArray();
            }
            var typeInfo = commandLibaryClass;

            // This will throw if the number of arguments provided does not match the number 
            // required by the method signature, even if some are optional:
            try
            {
                var result = typeInfo.InvokeMember(
                    command.Name,
                    BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                    null, null, inputArgs);
                return result.ToString();
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Esempio n. 6
0
        //static List<object> methodParameterValueList = new List<object>();
        //static IEnumerable<ParameterInfo> paramInfoList;

        public static string Execute(ConsoleCommand command2Execute)
        {
            // Validate the class name and command name:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            string badCommandMessage = string.Format(""
                                                     + "Unrecognized command \'{0}.{1}\'. "
                                                     + "Please type a valid command.",
                                                     command2Execute.LibraryClassName, command2Execute.Name);

            // Validate the command name:
            if (!CommandLibrary.Content.ContainsKey(command2Execute.LibraryClassName))
            {
                return(badCommandMessage);
            }

            var commandClassInfo = CommandLibrary.Content[command2Execute.LibraryClassName];

            if (!commandClassInfo.MethodDictionary.ContainsKey(command2Execute.Name))
            {
                return(badCommandMessage);
            }

            // Make sure the corret number of required arguments are provided:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            var methodParameterValueList = new List <object>();
            IEnumerable <ParameterInfo> paramInfoList = commandClassInfo.MethodDictionary[command2Execute.Name].ToList();

            // Validate proper # of required arguments provided. Some may be optional:
            var requiredParams = paramInfoList.Where(p => p.IsOptional == false);
            var optionalParams = paramInfoList.Where(p => p.IsOptional == true);
            int requiredCount  = requiredParams.Count();
            int optionalCount  = optionalParams.Count();
            int providedCount  = command2Execute.Arguments.Count();

            if (requiredCount > providedCount)
            {
                return(string.Format(
                           "Missing required argument. {0} required, {1} optional, {2} provided",
                           requiredCount, optionalCount, providedCount));
            }

            // Make sure all arguments are coerced to the proper type, and that there is a
            // value for every emthod parameter. The InvokeMember method fails if the number
            // of arguments provided does not match the number of parameters in the
            // method signature, even if some are optional:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            if (paramInfoList.Count() > 0)
            {
                // Populate the list with default values:
                foreach (var param in paramInfoList)
                {
                    // This will either add a null object reference if the param is required
                    // by the method, or will set a default value for optional parameters. in
                    // any case, there will be a value or null for each method argument
                    // in the method signature:
                    methodParameterValueList.Add(param.DefaultValue);
                }

                // Now walk through all the arguments passed from the console and assign
                // accordingly. Any optional arguments not provided have already been set to
                // the default specified by the method signature:
                for (int i = 0; i < command2Execute.Arguments.Count(); i++)
                {
                    var    methodParam  = paramInfoList.ElementAt(i);
                    var    typeRequired = methodParam.ParameterType;
                    object value        = null;
                    try
                    {
                        // Coming from the Console, all of our arguments are passed in as
                        // strings. Coerce to the type to match the method paramter:
                        value = CoerceArgument(typeRequired, command2Execute.Arguments.ElementAt(i));
                        methodParameterValueList.RemoveAt(i);
                        methodParameterValueList.Insert(i, value);
                    }
                    catch (ArgumentException)
                    {
                        string argumentName     = methodParam.Name;
                        string argumentTypeName = typeRequired.Name;
                        string message          =
                            string.Format(""
                                          + "The value passed for argument '{0}' cannot be parsed to type '{1}'",
                                          argumentName, argumentTypeName);
                        throw new ArgumentException(message);
                    }
                }
            }

            // Set up to invoke the method using reflection:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            Assembly current = commandClassInfo.OwningAssembly; //typeof(Program).Assembly;

            // Need the full Namespace for this:
            Type commandLibaryClass =
                current.GetType(CommandLibrary.CommandNamespace + "." + command2Execute.LibraryClassName);

            object[] inputArgs = null;
            if (methodParameterValueList.Count > 0)
            {
                inputArgs = methodParameterValueList.ToArray();
            }
            var typeInfo = commandLibaryClass;

            // This will throw if the number of arguments provided does not match the number
            // required by the method signature, even if some are optional:
            try
            {
                var result = typeInfo.InvokeMember(
                    command2Execute.Name,
                    BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                    null, null, inputArgs);
                return(result.ToString());
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
        private static string Execute(ConsoleCommand command)
        {
            // Validate the class name and command name:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            string badCommandMessage = string.Format($"Unrecognized command \'{command.LibraryClassName}.{command.Name}\'. Please type a valid command.");

            // Validate the command name:
            var methodDictionary = _commandLibraries[command.LibraryClassName];

            if (!_commandLibraries.ContainsKey(command.LibraryClassName) || !methodDictionary.ContainsKey(command.Name))
            {
                return(badCommandMessage);
            }

            // Make sure the corret number of required arguments are provided:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            var methodParameterValueList = new List <object>();
            IEnumerable <ParameterInfo> paramInfoList = methodDictionary[command.Name].ToList();

            // Validate proper # of required arguments provided. Some may be optional:
            var requiredParams = paramInfoList.Where(p => !p.IsOptional);
            var optionalParams = paramInfoList.Where(p => p.IsOptional);
            int requiredCount  = requiredParams.Count();
            int optionalCount  = optionalParams.Count();
            int providedCount  = command.Arguments.Count();

            if (requiredCount > providedCount)
            {
                return(string.Format($"Missing required argument. {requiredCount} required, {optionalCount} optional, {providedCount} provided"));
            }

            // Make sure all arguments are coerced to the proper type, and that there is a
            // value for every method parameter. The InvokeMember method fails if the number
            // of arguments provided does not match the number of parameters in the
            // method signature, even if some are optional:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            if (paramInfoList.Any())
            {
                foreach (var param in paramInfoList)
                {
                    methodParameterValueList.Add(param.DefaultValue);
                }

                for (int i = 0; i < command.Arguments.Count(); i++)
                {
                    var    methodParam  = paramInfoList.ElementAt(i);
                    var    typeRequired = methodParam.ParameterType;
                    object value        = null;

                    try
                    {
                        value = CoerceArguement(typeRequired, command.Arguments.ElementAt(i));
                        methodParameterValueList.RemoveAt(i);
                        methodParameterValueList.Insert(i, value);
                    }
                    catch (ArgumentException ex)
                    {
                        string argumentName     = methodParam.Name;
                        string argumentTypeName = typeRequired.Name;
                        string message          = string.Format($"The value passed for argument '{argumentName}' cannot be parsed to type '{argumentTypeName}'");

                        throw new ArgumentException(message);
                    }
                }
            }

            // Set up to invoke the method using reflection:
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            Assembly current             = typeof(Program).Assembly;
            Type     commandLibraryClass = current.GetType(_commandNamespace + "." + command.LibraryClassName);

            object[] inputArgs = null;

            if (methodParameterValueList.Count > 0)
            {
                inputArgs = methodParameterValueList.ToArray();
            }

            var typeInfo = commandLibraryClass;

            try
            {
                var result = typeInfo.InvokeMember(command.Name,
                                                   BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                                                   null, null, inputArgs);

                return(result.ToString());
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }