Exemple #1
0
        public void TestGetCommandSuggestions()
        {
            VerbToken           token1       = new VerbToken(new Name("verb1", "a"));
            Mock <ICommandRoot> commandRoot1 = new Mock <ICommandRoot>();

            VerbToken           token2       = new VerbToken(new Name("verb11", "b"));
            Mock <ICommandRoot> commandRoot2 = new Mock <ICommandRoot>();

            VerbToken           token3       = new VerbToken(new Name("verb111", "c"));
            Mock <ICommandRoot> commandRoot3 = new Mock <ICommandRoot>();

            commandRoot1.SetupGet(x => x.CommonTokens).Returns(new [] { token1 });
            commandRoot2.SetupGet(x => x.CommonTokens).Returns(new [] { token2 });
            commandRoot3.SetupGet(x => x.CommonTokens).Returns(new [] { token3 });

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(commandRoot1.Object);
            library.AddCommand(commandRoot2.Object);
            library.AddCommand(commandRoot3.Object);

            var suggestions = library.GetCommandSuggestions("verb");

            var orderedCommands = suggestions.OrderByDescending(x => x.Value).Select(x => x.Key);

            Assert.Equal(commandRoot1.Object, orderedCommands.First());
            Assert.Equal(commandRoot2.Object, orderedCommands.Skip(1).First());
            Assert.Equal(commandRoot3.Object, orderedCommands.Skip(2).First());
        }
Exemple #2
0
        public void TestParseWithStringArg()
        {
            VerbToken token1 = new VerbToken(new Name("verb1", "a"));

            ArgumentToken token2 = TokenUtils.BuildArgString("arg1");

            CommandRoot command1 = new CommandRoot.Builder()
                                   .Id(0)
                                   .Description("command")
                                   .WithToken(token1)
                                   .WithUsage(new CommandUsage.Builder()
                                              .Description("usage1")
                                              .WithToken(token2)
                                              .Build())
                                   .Build();

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(command1);

            CommandUsageMatchData matchData = library.Parse("verb1 \"the entire string\"");

            Assert.True(matchData.IsSuccessful);
            Assert.True(matchData.HasToken(token1));
            Assert.True(matchData.HasToken(token2));

            bool hasArg1Val = matchData.TryGetArgValue(token2, out string arg1Val);

            Assert.True(hasArg1Val);
            Assert.Equal("the entire string", arg1Val);
        }
Exemple #3
0
        public void TestParse()
        {
            VerbToken token1 = new VerbToken(new Name("verb1", "a"));

            ArgumentToken token2 = new ArgumentToken <int> .Builder().Name("arg").Parser(int.TryParse).IsOptional(false).Build();

            CommandRoot command1 = new CommandRoot.Builder()
                                   .Id(0)
                                   .Description("command")
                                   .WithToken(token1)
                                   .WithUsage(new CommandUsage.Builder()
                                              .Description("usage1")
                                              .WithToken(token2)
                                              .Build())
                                   .Build();

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(command1);

            CommandUsageMatchData matchData = library.Parse("verb1 123");

            Assert.True(matchData.IsSuccessful);
            Assert.True(matchData.HasToken(token1));
            Assert.True(matchData.HasToken(token2));

            bool hasArgVal = matchData.TryGetArgValue(token2, out int argVal);

            Assert.True(hasArgVal);
            Assert.Equal(123, argVal);
        }
Exemple #4
0
        public ServiceCollectionFixture()
        {
            var services = new ServiceCollection();

            services.AddSingleton <ICommandLibrary, CommandLibrary>(provider =>
            {
                var library = new CommandLibrary();

                library.RegisterCommand <SetCommand>();
                library.RegisterCommand <GetCommand>();
                library.RegisterCommand <DelCommand>();
                library.RegisterCommand <DbSizeCommand>();
                library.RegisterCommand <IncrCommand>();
                library.RegisterCommand <ZAddCommand>();
                library.RegisterCommand <ZCardCommand>();
                library.RegisterCommand <ZRankCommand>();
                library.RegisterCommand <ZRangeCommand>();

                return(library);
            });

            services.AddSingleton <ICommandResolver, CommandResolver>();

            services.AddSingleton <IDatabase>(provider => {
                return(new InMemoryDatabase(
                           new MemoryCache(
                               new MemoryCacheOptions())));
            });

            ServiceProvider = services.BuildServiceProvider();
        }
Exemple #5
0
        public IScriptCommand GetBlankCommand(Type commandType)
        {
            var command       = CommandLibrary.GetCommandFromType(commandType);
            var scriptCommand = command.BlankCommandCall();

            return(scriptCommand);
        }
Exemple #6
0
        public void WriteCommandList(CommandLibrary library)
        {
            List <ICommandRoot> commands = library.GetAllCommands().ToList();

            if (!commands.Any())
            {
                Console.WriteLine($"There are 0 available commands!");
                return;
            }

            Console.WriteLine($"{Environment.NewLine}Available Commands:");
            Console.WriteLine($"-------------------");


            int padLen = commands.Max(x => x.Name.Length) + 1;  //add 1 is an arbitrary number.

            //Just gives it some extra padding
            foreach (var command in commands)
            {
                Console.Write(command.Name.PadRight(padLen));
                if (!string.IsNullOrWhiteSpace(command.Description))
                {
                    Console.WriteLine($": {command.Description}", Color.Gray);
                }
                else
                {
                    Console.WriteLine();
                }
            }

            Console.WriteLine();
        }
Exemple #7
0
        public void TestTryGetCommand()
        {
            VerbToken           token1       = new VerbToken(new Name("verb1", "a"));
            Mock <ICommandRoot> commandRoot1 = new Mock <ICommandRoot>();

            VerbToken           token2       = new VerbToken(new Name("verb2", "b"));
            Mock <ICommandRoot> commandRoot2 = new Mock <ICommandRoot>();

            VerbToken           token3       = new VerbToken(new Name("verb3", "c"));
            Mock <ICommandRoot> commandRoot3 = new Mock <ICommandRoot>();

            commandRoot1.SetupGet(x => x.CommonTokens).Returns(new [] { token1 });
            commandRoot2.SetupGet(x => x.CommonTokens).Returns(new [] { token2 });
            commandRoot3.SetupGet(x => x.CommonTokens).Returns(new [] { token3 });

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(commandRoot1.Object);
            library.AddCommand(commandRoot2.Object);
            library.AddCommand(commandRoot3.Object);

            ICommandRoot cmd1;
            bool         success1 = library.TryGetCommand("verb3", out cmd1);
            ICommandRoot cmd2;
            bool         success2 = library.TryGetCommand("b", out cmd2);

            Assert.True(success1);
            Assert.Equal(commandRoot3.Object, cmd1);

            Assert.True(success2);
            Assert.Equal(commandRoot2.Object, cmd2);
        }
Exemple #8
0
        public void TestAddCommand()
        {
            Mock <ICommandRoot> commandRoot = new Mock <ICommandRoot>();
            CommandLibrary      library     = new CommandLibrary();

            library.AddCommand(commandRoot.Object);

            Assert.Contains(commandRoot.Object, library.GetAllCommands());
        }
Exemple #9
0
        public App(FileInfo dbInfo, CommandLibrary commandLibrary)
        {
            _listeners = new [] { new ConsoleCommandActionListener(this, new HelpInfoConsoleWriter(), commandLibrary) };

            ReadLine.HistoryEnabled        = true;
            ReadLine.AutoCompletionHandler = new AutoCompletionHandler();
            _continueLoop = true;
            _interpreter  = new CommandInterpreter(RepositoryBagUtil.GetRuntimeRepositoryBag(dbInfo, null), commandLibrary);
        }
Exemple #10
0
        public void ParseNewAccount(string newAccountStr)
        {
            string fullInput = newAccountStr + " \"account name\"";

            CommandLibrary library = BudgetCliCommands.BuildCommandLibrary();

            CommandUsageMatchData matchData = library.Parse(fullInput);

            Assert.True(matchData.IsSuccessful);
        }
Exemple #11
0
 public static string AddExternalAssembly(string assemblyFile)
 {
     if (File.Exists(assemblyFile))
     {
         CommandLibrary.addCommandsFromAssemblyFile(assemblyFile);
         return("added Assembly File : " + Path.GetFileName(assemblyFile));
     }
     else
     {
         return("Assembly file \'" + Path.GetFileName(assemblyFile) + "\' does not exist");
     }
 }
Exemple #12
0
        private void Interpreter(ChatSharp.Events.PrivateMessageEventArgs e)
        {
//            Console.WriteLine("Interpreting...");
            Command tempCommand = new Command();

            if (!CheckIgnore(e.PrivateMessage.User.Nick, e.PrivateMessage.User.Hostname))
            {
                tempCommand.channelMessage = e.PrivateMessage.IsChannelMessage;
                if (e.PrivateMessage.IsChannelMessage)
                {
                    tempCommand.chanOp  = CheckMode(e.PrivateMessage.User.ChannelModes[client.Channels[e.PrivateMessage.Source]].ToString());
                    tempCommand.channel = config.channels[e.PrivateMessage.Source];
                }
                tempCommand.source = e.PrivateMessage.Source;
//            Console.WriteLine(e.PrivateMessage.User.Nick);
//            Console.WriteLine(e.PrivateMessage.User.Hostname);
//            tempCommand.user.nick = e.PrivateMessage.User.Nick.ToString();
//            tempCommand.user.hostName = e.PrivateMessage.User.Hostname;
                tempCommand.nick     = e.PrivateMessage.User.Nick;
                tempCommand.message  = e.PrivateMessage.Message;
                tempCommand.hostName = e.PrivateMessage.User.Hostname;
                tempCommand.admin    = CheckAdmin(tempCommand.nick, tempCommand.hostName);

//            Console.WriteLine("blah");
                tempCommand = parseCommand(tempCommand, e.PrivateMessage.Message);
//            Console.WriteLine("Success?");
                if (tempCommand.commandFound)
                {
                    ThreadStart processTaskThread = delegate
                    {
                        ExecuteCommand(tempCommand);
                    };
                    new Thread(processTaskThread).Start();
                }
                else
                {
                    foreach (string key in prefExplain.Keys)
                    {
                        if (CommandLibrary.DyanmicCheckInline(key, tempCommand))
                        {
                            ThreadStart processTaskThread = delegate
                            {
                                ExecuteCommand(tempCommand);
                            };
                            new Thread(processTaskThread).Start();
                        }
                    }
                }
            }
            // General stuff down here, like 'doot' or youtube commands
            // Actually no, find a better way or something
        }
Exemple #13
0
        private void WriteCommand(string text)
        {
            IEnumerable <string> commands = (CurrentCommand + text).Split('\n');

            Content       += text;
            CurrentCommand = commands.Last();
            commands       = commands.Except(new[] { commands.Last() });

            foreach (var command in commands)
            {
                if (!CommandLibrary.TryExecute(command))
                {
                    WriteOutput("Wrong command\n");
                }
            }
        }
Exemple #14
0
        public void TestGetAllCommandNames()
        {
            VerbToken           token        = new VerbToken(new Name("verb", "v"));
            Mock <ICommandRoot> commandRoot1 = new Mock <ICommandRoot>();

            commandRoot1.SetupGet(x => x.CommonTokens).Returns(new [] { token });

            CommandLibrary library = new CommandLibrary();

            library.AddCommand(commandRoot1.Object);

            var names = library.GetAllCommandNames();

            Assert.Equal(1, names.Count());
            Assert.Equal("verb", names.First());
        }
        public static CommandLibrary BuildCommandLibrary()
        {
            CommandLibrary library = new CommandLibrary();

            library.AddCommand(CMD_HELP)
            .AddCommand(CMD_VERSION)
            .AddCommand(CMD_CLEAR)
            .AddCommand(CMD_EXIT)
            .AddCommand(CMD_NEW_ACCOUNT)
            .AddCommand(CMD_DETAIL_ACCOUNTS)
            .AddCommand(CMD_LS_ACCOUNTS)
            .AddCommand(CMD_REMOVE_ACCOUNTS)
            .AddCommand(CMD_SET_ACCOUNTS)
            ;

            return(library);
        }
        public ConsoleSimulator()
        {
            commandLibrary = new CommandLibrary();
            toyRobot       = new ToyRobot();;
            map            = new Map(6, 6);
            isRunning      = true;

            // Bind the ToyRobot API
            commandLibrary.Register(new ActionCommand("left", toyRobot.Left));
            commandLibrary.Register(new ActionCommand("right", toyRobot.Right));
            commandLibrary.Register(new ActionCommand("move", toyRobot.Move));
            commandLibrary.Register(new OverloadedCommand("place",
                                                          new List <ICommand>()
            {
                new ArgumentCommand <int, int>("place", (x, y) => toyRobot.Place(x, y)),
                new ArgumentCommand <int, int, Direction>("place", (x, y, dir) => toyRobot.Place(map, x, y, dir))
            }));

            // Bind the Simulator API
            commandLibrary.Register(new ActionCommand("quit", Quit));
            commandLibrary.Register(new ActionCommand("report", Report));
        }
Exemple #17
0
        private void Initialize_Command_Libraries()
        {
            List <Command> standby_commands = new List <Command> {
                save_temp
            };

            standby_library = new CommandLibrary(standby_commands);

            List <Command> dictate_commands = new List <Command> {
                stop, clear, send
            };

            foreach (Command cmd in usernames)
            {
                dictate_commands.Add(cmd);
            }
            foreach (Command cmd in temperatures)
            {
                dictate_commands.Add(cmd);
            }
            dictate_library = new CommandLibrary(dictate_commands);
        }
Exemple #18
0
 public override void UpdateStrings()
 {
     base.UpdateStrings();
     if (IsLoaded)
     {
         CommandLibrary.BuildCommands();
         this.BindCommands();
         menu.BindCommands();
         tbUserName.Text = string.Format(MainResources.Welcome, AppContext.User.GetScreenName());
         hlShowNotificationsWindow.Command     =
             miShowNotificationsWindow.Command = CommandLibrary.ShowNotificationsWindow;
         hlShutdownPhoenix.Command             = miShutdownPhoenix.Command = CommandLibrary.ShutdownPhoenix;
         hlOpenAccountWindow.Command           = miOpenAccountWindow.Command = CommandLibrary.OpenAccountWindow;
         miOpenRestoreWindow.Command           = CommandLibrary.OpenRestoreWindow;
         miOpenBackupWindow.Command            = CommandLibrary.OpenBackupWindow;
         miOpenCategoriesWindow.Command        = CommandLibrary.OpenCategoriesWindow;
         miOpenPropsWindow.Command             = CommandLibrary.OpenPropsWindow;
         miOpenPropsStatusWindow.Command       = CommandLibrary.OpenPropsStatusWindow;
         miOpenLabsWindow.Command      = CommandLibrary.OpenLabsWindow;
         miOpenLabPropsWindow.Command  = CommandLibrary.OpenLabPropsWindow;
         miOpenMaterialsWindow.Command = CommandLibrary.OpenMaterialsWindow;
         miOpenItemsWindow.Command     = CommandLibrary.OpenItemsWindow;
         miOpenRepositoryMaterialsAndItemsWindow.Command = CommandLibrary.OpenRepositoryMaterialsAndItemsWindow;
         miOpenSearchWindow.Command                      = CommandLibrary.OpenSearchWindow;
         miOpenSearchWindowInProps.Command               = CommandLibrary.OpenSearchWindowInProps;
         miOpenSearchWindowInMaterials.Command           = CommandLibrary.OpenSearchWindowInMaterials;
         miOpenSearchWindowInRepositoryMaterials.Command = CommandLibrary.OpenSearchWindowInRepositoryMaterials;
         miOpenSearchWindowInItems.Command               = CommandLibrary.OpenSearchWindowInItems;
         miOpenSearchWindowInRepositoryItems.Command     = CommandLibrary.OpenSearchWindowInRepositoryItems;
         miOpenSearchWindowInLabs.Command                = CommandLibrary.OpenSearchWindowInLabs;
         miOpenSearchWindowInLabProps.Command            = CommandLibrary.OpenSearchWindowInLabProps;
         miOpenSearchWindowInLogs.Command                = CommandLibrary.OpenSearchWindowInLogs;
         miOpenRolesWindow.Command    = CommandLibrary.OpenRolesWindow;
         miOpenUsersWindow.Command    = CommandLibrary.OpenUsersWindow;
         miOpenLogsWindow.Command     = CommandLibrary.OpenLogsWindow;
         miOpenSettingsWindow.Command = CommandLibrary.OpenSettingsWindow;
         miOpenAboutWindow.Command    = CommandLibrary.OpenAboutWindow;
     }
 }
Exemple #19
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <ICommandLibrary, CommandLibrary>(provider =>
            {
                var library = new CommandLibrary();

                library.RegisterCommand <SetCommand>();
                library.RegisterCommand <GetCommand>();
                library.RegisterCommand <DelCommand>();
                library.RegisterCommand <DbSizeCommand>();
                library.RegisterCommand <IncrCommand>();
                library.RegisterCommand <ZAddCommand>();
                library.RegisterCommand <ZCardCommand>();
                library.RegisterCommand <ZRankCommand>();
                library.RegisterCommand <ZRangeCommand>();

                return(library);
            });

            services.AddSingleton <ICommandResolver, CommandResolver>();

            services.AddSingleton <IDatabase, InMemoryDatabase>();
        }
 private bool HasCommand(CommandLibrary library, params VerbToken[] tokens)
 {
     return(library.TryGetCommand(TokenUtils.GetMatchText(tokens.Select(x => x.Name.Preferred).ToArray(), 0, tokens.Length), out _));
 }
 public CommandInterpreter(RepositoryBag repositories, CommandLibrary library)
 {
     Repositories = repositories;
     Commands     = library;
 }
Exemple #22
0
 public Mode(CommandLibrary cmd_library)
 {
     this.Cmd_library = cmd_library;
 }
 public ConsoleCommandActionListener(IExitListener exitListener, HelpInfoConsoleWriter helpInfoWriter, CommandLibrary commandLibrary)
 {
     ExitListener   = exitListener;
     HelpInfoWriter = helpInfoWriter;
     CommandLibrary = commandLibrary;
 }
Exemple #24
0
 public static void Init()
 {
     Dicts.InitArgDict(CommandLibrary.Commands);
     Dicts.InitCommandDict(CommandLibrary.CommandMethods, CommandLibrary.CommandMethodsTokens);
     CommandLibrary.Validate();
 }
Exemple #25
0
 public MainMenuHandler()
 {
     lib = new CommandLibrary();
     validCommands.Add(lib.options);
     validCommands.Add(lib.create);
 }
Exemple #26
0
 public static void RefreshCulture(CultureInfo culture)
 {
     if (string.Compare(culture.Name, Dispatcher.CurrentDispatcher.Thread.CurrentCulture.Name) == 0)
     {
         return;
     }
     Dispatcher.CurrentDispatcher.Thread.CurrentCulture       =
         Dispatcher.CurrentDispatcher.Thread.CurrentUICulture = culture;
     AccountResources.Culture                                                                   =
         CategoriesResources.Culture                                                            =
             AboutResources.Culture                                                             =
                 InputResources.Culture                                                         =
                     LabPropsResources.Culture                                                  =
                         LabsResources.Culture                                                  =
                             LoginResources.Culture                                             =
                                 LogsResources.Culture                                          =
                                     MainResources.Culture                                      =
                                         MaterialsResources.Culture                             =
                                             MessagesResources.Culture                          =
                                                 ProgressResources.Culture                      =
                                                     PropsResources.Culture                     =
                                                         PropsSelectorResources.Culture         =
                                                             PropStatusResources.Culture        =
                                                                 ReportPreviewResources.Culture =
                                                                     RepositoryMaterialsAndItemsResources.Culture =
                                                                         RolesResources.Culture                  =
                                                                             SearchResources.Culture             =
                                                                                 SettingsResources.Culture       =
                                                                                     SharedResources.Culture     =
                                                                                         SplashResources.Culture = NotificationsResources.Culture = UsersResources.Culture = culture;
     AccountResourceProvider.Refresh();
     CategoriesResourceProvider.Refresh();
     InputResourceProvider.Refresh();
     LabPropsResourceProvider.Refresh();
     LabsResourceProvider.Refresh();
     LoginResourceProvider.Refresh();
     LogsResourceProvider.Refresh();
     MainResourceProvider.Refresh();
     MaterialsResourceProvider.Refresh();
     ItemsResourceProvider.Refresh();
     PropsResourceProvider.Refresh();
     PropsSelectorResourceProvider.Refresh();
     PropStatusResourceProvider.Refresh();
     ReportPreviewResourceProvider.Refresh();
     RepositoryMaterialsAndItemsResourceProvider.Refresh();
     RolesResourceProvider.Refresh();
     SearchResourceProvider.Refresh();
     SettingsResourceProvider.Refresh();
     SharedResourceProvider.Refresh();
     SplashResourceProvider.Refresh();
     NotificationsResourceProvider.Refresh();
     UsersResourceProvider.Refresh();
     AboutResourceProvider.Refresh();
     RestoreResourceProvider.Refresh();
     BackupResourceProvider.Refresh();
     foreach (var window in Application.Current.Windows.Cast <WindowBase>())
     {
         window.UpdateStrings();
     }
     CommandLibrary.BuildCommands();
 }
Exemple #27
0
        private void ExecuteCommand(Command command)
        {
            if (!command.commandFound) // Inline stuff, like youtube titles
            {
                foreach (string key in prefExplain.Keys)
                {
                    CommandLibrary.DynamicRunInline(key, command);
                }
            }

            if (command.command == "help")
            {
                if (command.firstParameter == "")
                {
                    StringBuilder output = new StringBuilder();

                    if (command.admin)
                    {
                        output.AppendFormat("Admin commands: {0}",
                                            CommandLibrary.admin.ListHelp(command.firstParameter));
                    }

                    if (command.chanOp || command.admin)
                    {
                        if (output.ToString() != "")
                        {
                            output.Append(" | ");
                        }

                        output.AppendFormat("OP commands: {0}",
                                            CommandLibrary.op.ListHelp(command.firstParameter));
                    }

                    foreach (string key in prefExplain.Keys)
                    {
                        if ((bool)command.channel.CheckPreference(key))
                        {
                            if (output.ToString() != "")
                            {
                                output.Append(" | ");
                            }

                            output.AppendFormat("{0} commands: {1}",
                                                prefDescrip[key], CommandLibrary.DynamicListHelp(key, command.firstParameter));
                        }
                    }

                    if (output.ToString() != "")
                    {
                        SendMessage(output.ToString(), command, sendType.message);
                    }
                    else
                    {
                        SendMessage("You have no commands available to you.", command, sendType.message);
                    }
                }
                else
                {
                    string output = "";

                    if (command.admin)
                    {
                        output = CommandLibrary.admin.GetDescription(command.firstParameter);
                    }

                    if (output == "" && (command.chanOp || command.admin))
                    {
                        output = CommandLibrary.op.GetDescription(command.firstParameter);
                    }


                    foreach (string key in prefExplain.Keys)
                    {
                        if (output == "" && ((bool)command.channel.CheckPreference(key)))
                        {
                            output = CommandLibrary.DynamicGetDescription(key, command.firstParameter);
                        }
                    }

                    if (output != "")
                    {
                        output = command.firstParameter + ": " + output;
                        SendMessage(output, command, sendType.message);
                    }
                    else
                    {
                        SendMessage("Not a valid command!", command, sendType.message);
                    }
                }
            }
            else
            {
                bool commandRun = false;
                if (command.admin)
                {
                    // Admin commands here
                    commandRun = CommandLibrary.admin.Run(command);
                }

                if (!commandRun && (command.chanOp || command.admin))
                {
                    // Channel op commands
                    // Stuff here: mode change, some other stuff

                    commandRun = CommandLibrary.op.Run(command);
                }

                // All other commands here

                foreach (string key in prefExplain.Keys)
                {
                    if (!commandRun && (bool)command.channel.CheckPreference(key))
                    {
                        commandRun = CommandLibrary.DynamicRun(key, command);
                    }
                }
            }
        }