1 //
2 // Copyright (c) 2010-2023 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Collections.Generic;
10 using Antmicro.Renode.UserInterface.Tokenizer;
11 using AntShell.Commands;
12 
13 namespace Antmicro.Renode.UserInterface.Commands
14 {
15     public class PythonExecuteCommand : Command
16     {
PrintHelp(ICommandInteraction writer)17         public override void PrintHelp(ICommandInteraction writer)
18         {
19             base.PrintHelp(writer);
20             writer.WriteLine();
21             writer.WriteLine("Provide a command or variable to execute.");
22         }
23 
24         [Runnable]
Run(ICommandInteraction writer, VariableToken variable)25         public void Run(ICommandInteraction writer, VariableToken variable)
26         {
27             var value = GetVariable(variable);
28 
29             if(value is StringToken stringValue)
30             {
31                 Run(writer, stringValue);
32             }
33             else
34             {
35                 writer.WriteError("Variable type has to be a string.");
36             }
37         }
38 
39         [Runnable]
Run(ICommandInteraction writer, StringToken command)40         public void Run(ICommandInteraction writer, StringToken command)
41         {
42             Execute(command.Value, writer);
43         }
44 
45         private readonly Func<VariableToken, Token> GetVariable;
46         private readonly Action<string, ICommandInteraction> Execute;
47 
PythonExecuteCommand(Monitor monitor, Func<VariableToken, Token> getVariable, Action<String, ICommandInteraction> execute)48         public PythonExecuteCommand(Monitor monitor, Func<VariableToken, Token> getVariable, Action<String, ICommandInteraction> execute)
49             :base(monitor, "python", "executes the provided python command.", "py")
50         {
51             GetVariable = getVariable;
52             Execute = execute;
53         }
54     }
55 }
56 
57