public void RegisteredLambdaCommandShouldWorkOutOfTheBox()
        {
            var mul = new Func <double, double, double>((a, b) => a * b);

            _commands.Register(new ConsoleCommand(mul.Target !, mul.Method, "mul"));

            var    result    = _commands.Execute("mul(0.5,0.5)");
            string converted = result.ConvertOrError();

            Assert.AreEqual("0.25", converted);
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            // For this demo I've written ExchangeRatesApi class, which provides several methods
            // representing requests to https://exchangeratesapi.io/.
            // These return various forex currency rates at the given day.
            // So let's allow user to call them from console via text input,
            // passing function parameters as JSON objects:

            // First, create instance containing several commands which call ExchangeRates API synchronously.
            var exchangeRatesApi = new ExchangeRatesApi();

            // Customize 'command result to string' converter a bit for better output printing,
            // since exchangeratesapi.io returns ISO dates without time part.
            var commandReturnsConverter = new CommandReturnedObjectJsonConverter(true, Formatting.Indented,
                                                                                 new JsonSerializerSettings {
                DateFormatString = "yyyy'-'MM'-'dd"
            });

            // Create command manager. It will contain a set of commands, parse given user input into a proper command,
            // convert its parameters from a string to typed objects, execute the command with converted parameters
            // and return a result.
            var consoleCommands = new ConsoleCommands(ConsoleCommandParser.Default,
                                                      ConsoleCommandParameterConverter.Default);

            // Argument JSON handling can be customized by passing 'new ConsoleCommandParameterConverter(yourJsonDeserializerSettings)' here.

            // Methods marked with [CommandExecutable] will be added from the given exchangeRatesApi instance:
            consoleCommands.RegisterAllFromInstance(exchangeRatesApi);

            // Example of the manually created commands:
            var help = new Func <string>(() => {
                Console.WriteLine($"Available commands:{Environment.NewLine}");
                return(consoleCommands.ToString());
            });

            consoleCommands.Register(new ConsoleCommand(help.Target !, help.Method, "help",
                                                        "Prints available commands.", new[] { "commands" }));

            var clearScreen = new Action(Console.Clear);

            consoleCommands.Register(new ConsoleCommand(clearScreen.Target !, clearScreen.Method, "clear",
                                                        "Clears console.", new[] { "cls" }));

            var quit = new Action(() => Environment.Exit(0));

            consoleCommands.Register(new ConsoleCommand(quit.Target !, quit.Method, "quit",
                                                        "Quits the program.", new[] { "q" }));

            Console.WriteLine("Input \"help\" without quotes to see available commands. Pass command parameters as " +
                              "JSON, for example: d(\"2019-12-31\", \"USD\", [\"EUR\", \"RUB\"])");

            // Continue to execute user imput until the 'quit' command:
            while (true)
            {
                Console.Write("Command prompt: ");
                string commandRepr = Console.ReadLine();
                string evaluated   = consoleCommands.Execute(commandRepr).ConvertOrError(commandReturnsConverter);
                Console.WriteLine(evaluated);
            }
        }
Ejemplo n.º 3
0
    private void HandleInputs()
    {
        if (btnConsole.Check() == InputStates.InputState.JustPressed)
        {
            if (visible)
            {
                Hide();
            }
            else
            {
                Show();
            }
        }

        if (btnEnter.Check() == InputStates.InputState.JustPressed)
        {
            string   command = inputText.text;
            string[] tokens  = command.Split(' ', '\t', '\n');
            string   output  = "";
            if (command.Length > 0)
            {
                ConsoleCommands.Execute(tokens.Length, tokens, ref output, this);
                if (!storedCommands.Contains(command))
                {
                    storedCommands.Add(command);
                }
                if (storedCommands.Count > 16)
                {
                    storedCommands.RemoveAt(0);
                }
            }
            outputText.text += ">>> " + output + "\n";
        }

        if (btnDown.Check() == InputStates.InputState.JustPressed)
        {
            if (storedCommands.Count > 0)
            {
                instrIdx -= 1;
                if (instrIdx < 0)
                {
                    instrIdx = storedCommands.Count - 1;
                }
                string command = storedCommands[instrIdx];
                inputText.text = command;
            }
        }

        if (btnUp.Check() == InputStates.InputState.JustPressed)
        {
            if (storedCommands.Count > 0)
            {
                instrIdx = (instrIdx + 1) % storedCommands.Count;
                string command = storedCommands[instrIdx];
                inputText.text = command;
            }
        }
    }
Ejemplo n.º 4
0
        public static void ExecuteCommand(string Command)
        {
            string[] args = ParseCommand(Command);
            switch (Command.Split(' ').First())
            {
            case "analysis":
                break;

            case "console":
                ConsoleCommands.Execute(args);
                break;
            }
        }