ECC is a simple, self-scaling command-based system for console applications that comes with some simple base-commands and allows creation of custom commands. It also has a basic scripting ability with permanent variables etc. The projects source code is on Github.
The user can interact with the system by entering commands into the console. These commands always follow the same format. A full list of all registered commands (including your custom ones) is available with the help
command.
Example
// Command with no variables, e.g. "help"
help
// Commands with variables, e.g. "color" (sets font color, takes valid ConsoleColor as parameter)
// Parameters are given behind a "-"
color-red
// Commands with multiple parameters, e.g. "alias" (sets alias for a command, takes current command name and new name as input)
// Multiple parameters are also separated by "-"
alias-color-changeFontColor
// Commands that take raw user input take it the same way as a parameter
When using ECC in your framework you can also write simple scripts with it (An example script is in the Example.txt). It basically works by writing the commands that you would use in the console line by line in a .txt file and then executing it with the script
command.
Example
// This is your script in a .txt
print-Hello World
color-red
print-This is printed in red!
color-white
print-This is printed in white again!
// This will be the output
script-path/to/your/script.txt
Hello World
This is printed in red!
This is printed in white again!
Scripting can be useful when you want to execute the same commands over and over again (e.g., to set up your environment with font color, variables etc.) or even to write small programs like a calculator or similar.
To create a custom command you need to make a class that inherits from the Interface ICommand
provided by ECC. The Interface has the following structure:
class CommandExample : ICommand
{
// This is the keyword for your command
public string Name => "example";
// This should explain your command briefly and mention all the needed parameters
public string HelpText => "It's just an example.";
// This list should contain all the needed parameters as StringInfo, IntInfo or BoolInfo (Types provided by ECC). Here it takes two strings as parameters.
public List<Type> ParameterTypes => new List<Type> (typeof(IntInfo), typeof(IntInfo));
// The Execute function contains the logic for your command
// Set List<VariableInfo> as a parameter, to include your parameters
public void Execute(List<VariableInfo> inputParameters)
{
// To use a parameter, get it from the list. The first variable is Position 0, the second one is 1 and so on...
IntInfo firstParameter = inputParameters[0] as IntInfo;
IntInfo secondParameter = inputParameters[1] as IntInfo;
// Use it as (your variable name)firstParameter.Value to get the int value
int aNewNumber = firstParameter.Value + secondParameter.Value;
Console.WriteLine("Your new number is: " + aNewNumber);
}
}
Then, after creating your command class, register it with the framework.
Commands.Add(new CommandExample());
To interact with the framework or pass any objects, write a constructor for our command class that takes the object and pass it with the registration.
// In your command class, put it into a constructor
class CommandExample : ICommand
{
public string Name => "example";
public string HelpText => "It's just an example.";
public List<Type> ParameterTypes => new List<Type> (typeof(IntInfo), typeof(IntInfo));
private HttpClient client;
public CommandExample(HttpClient client)
{
this.client = client;
}
public void Execute(List<VariableInfo> inputParameters)
{
// Do something with it...
}
}
// At the registration pass the object
HttpClient client = new HttpClient();
Commands.Add(new CommandExample(client));
Idea, Design and Programming by Mocbuilder (Mocbuilder Coding Creations) aka Me
Base-Code of the "hacker"-command by CollegeCode
Special Thanks to rmoc81 for helping me with the programming.
If you want to use this in any public way, a little credit would be appreciated.