コード例 #1
0
        public override void RunCommand(string remainingInput = "")
        {
            Dictionary <Command, string> reminderMethods = new Dictionary <Command, string>();

            foreach (Command remMethod in ReminderMethods)
            {
                if (remMethod is GmailCommand)
                {
                    reminderMethods.Add(remMethod, "Gmail (" + (remMethod as GmailCommand).To + ")");
                }
                else if (remMethod is GoogleCalendarEventCommand)
                {
                    reminderMethods.Add(remMethod, "GoogleCalendarEvent (" + (remMethod as GoogleCalendarEventCommand).Calendar + ")");
                }
                else
                {
                    reminderMethods.Add(remMethod, remMethod.GetType().ToString());
                }
            }

            int     reminderIndex  = Common.GetIndexFromList(reminderMethods.Values.ToList());
            Command reminderMethod = reminderMethods.Keys.ElementAt(reminderIndex);

            reminderMethod.RunCommand(remainingInput);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: derekantrican/Assistant
        private static void FindMatchingCommand(string input)
        {
            bool helpRequested = false;

            input = input.ToLower();

            Match help = Regex.Match(input, @"\s?\S+\s?");

            while (help.Success)
            {
                if (help.Value.Trim() == "?")
                {
                    helpRequested = true;
                }

                help = help.NextMatch();
            }

            Command matchingCommand = null;
            string  matchingTrigger = "";

            foreach (KeyValuePair <List <string>, Command> command in Common.Commands)
            {
                int numberOfTriggerWordsMatched = 0;
                foreach (string trigger in command.Key) //This might be able to be simplified in the future
                {
                    if (Regex.Match(input, @"(^|\s+)" + trigger + @"(\s+|$)").Success&& Regex.Matches(trigger, @"\w+").Count > numberOfTriggerWordsMatched)
                    {
                        matchingCommand             = command.Value;
                        matchingTrigger             = trigger;
                        numberOfTriggerWordsMatched = Regex.Matches(trigger, @"\w+").Count;
                    }
                }
            }

            if (matchingCommand != null)
            {
                if (helpRequested)
                {
                    Console.WriteLine(matchingCommand.CommandHelp);
                }
                else
                {
                    matchingCommand.RunCommand(input.Replace(matchingTrigger, "").Trim());

                    if (matchingCommand.ExitAfterExecution)
                    {
                        Environment.Exit(0);
                    }
                }

                return;
            }

            if (helpRequested)
            {
                foreach (KeyValuePair <List <string>, Command> command in Common.Commands)
                {
                    Console.Write(string.Join(",", command.Key));

                    if (!Common.Commands.Last().Equals(command))
                    {
                        Console.Write(',');
                    }
                }

                Console.Write(Environment.NewLine);
            }
            else
            {
                Console.WriteLine("No matching command");
            }
        }