예제 #1
0
        //read a command from console and load the command
        public static bool readCommand()
        {
            command = null;
            options = new Dictionary <string, string>();
            string line = Console.ReadLine();

            if (line != null)
            {
                return(load(line.Trim().Split(' ')));
            }
            else
            {
                CommandOutput.commandErrorOutput("Fail to read command.");
                return(false);
            }
        }
예제 #2
0
        //validate given options
        //the number of options must be 2: config and testset
        public bool validateOptions(Dictionary <string, string> options)
        {
            string config  = "";
            string testset = "";

            if (options.Count != 2 || !options.TryGetValue(CONFIG_OPTION, out config) || !options.TryGetValue(TESTSET_OPTION, out testset))
            {
                CommandOutput.commandErrorOutput("Command should be: " + commandSample);
                config  = "";
                testset = "";
                return(false);
            }

            if (config.Equals("") || testset.Equals(""))
            {
                CommandOutput.commandErrorOutput("Command should be: " + commandSample);
                return(false);
            }

            return(true);
        }
예제 #3
0
        public static bool load(string[] args)
        {
            command = null;
            options = new Dictionary <string, string>();
            if (args.Length < 1)
            {
                Console.WriteLine("Please provide a command.");
                return(false);
            }
            command = args[0].Trim();

            options = new Dictionary <string, string>();
            string option;

            //each option can have only one option value or no option value, like: -option1 value1 -option2
            for (int i = 1; i < args.Length; i++)
            {
                option = args[i].Trim();
                if (!option.StartsWith("-"))
                {
                    CommandOutput.commandErrorOutput("Option must begin with -");
                    return(false);
                }
                option = option.Substring(1).Trim();
                options.Add(option, "");
                if (i + 1 < args.Length)
                {
                    args[i + 1] = args[i + 1].Trim();
                    if (!args[i + 1].StartsWith("-"))
                    {
                        options[option] = args[i + 1];
                        i++;
                    }
                }
            }
            return(true);
        }
예제 #4
0
        public static bool run(string command, Dictionary <string, string> options)
        {
            ICommand cmd = CommandFactory.getCommand(command);

            if (cmd == null)
            {
                CommandOutput.commandErrorOutput("No such command!");
                return(false);
            }

            StatusMachine   stateMachine = new StatusMachine();
            ChangeCondition condition    = stateMachine.mapToCondition(cmd);
            ProcessState    currentState = stateMachine.MoveNext(condition);

            Boolean runResult, isResourceCreated;

            while (currentState != ProcessState.Terminated)
            {
                if (stateMachine.isRunningState(currentState) == false)
                {
                    //command is not going to run, must be wrong input
                    CommandOutput.commandErrorOutput(stateMachine.Messages[currentState]);
                }
                else
                {
                    //need to run the command
                    runResult = cmd.execute(options);
                    if (cmd.GetType() == typeof(SetupCommand))
                    {
                        isResourceCreated = Resource.isResourceCreated();
                        condition         = stateMachine.mapToCondition(cmd, runResult, isResourceCreated);
                    }
                    else
                    {
                        condition = stateMachine.mapToCondition(cmd, runResult, false);
                    }
                    currentState = stateMachine.MoveNext(condition);
                    if (currentState == ProcessState.Terminated)
                    {
                        break;
                    }
                }

                while (true)
                {
                    //Fetch a command
                    if (!CommandReader.readCommand())
                    {
                        return(false);
                    }
                    cmd = CommandFactory.getCommand(CommandReader.Command);
                    if (cmd != null)
                    {
                        break;
                    }
                    CommandOutput.commandErrorOutput("No such command!");
                }

                condition    = stateMachine.mapToCondition(cmd);
                currentState = stateMachine.MoveNext(condition);
            }
            return(true);
        }