Console IO - mahilab/MEL GitHub Wiki

Since your MEL applications will most likely be driven from the command line, MEL provides user friendly console I/O functions in the header:

#include <MEL/Core/Console.hpp>

Here's a few ways you can display information:

print("Hello, World!");
print("Data:", std::vector<double>({1,2,3,4,5}));
color_print("This message has red text on a green background", Color::Red, Color::Green);

Here's a few ways you can get user input:

prompt("Press Enter to Advance the Program");
char key;
while (key != 'x') {		
    key = get_key();
    print(key);
} 

Users often spam Ctrl-C to kill an application. Pressing Ctrl-C is considered a "control event" and unless you catch it, your program will automatically abort by default. This may have devastating effects if your app is in the middle of controlling real-hardware. MEL provides the ability to register control event handlers:

ctrl_bool g_stop_flag(false);

bool my_handler(CtrlEvent event) {
    if (event == CtrlEvent::CtrlC) {
        print("Ctrl-C Pressed! Aborting Application!");
		g_stop_flag = true;
    }
    return true;
}

int main() {
    register_ctrl_handler(my_handler);

    while (!g_stop_flag) {
        my_really_dangerous_code.process();
        // code that would cause catastrophic
        // failure if not gracefully ended
    }

    my_really_dangerous_code.shut_down();        
}

Finally, MEL provides a class Options to parse user input from the command line at start-up:

Options options("options.exe", "Simple Program Demonstrating Options");
options.add_options()
    ("i,integer", "Gets an integer from user.", value<int>())
    ("d,double", "Gets a double from user.", value<double>())
    ("h,help", "Prints helpful information.");

which could then be invoked from the console in ways such as:

> options.exe --help
> options.exe -h
> options.exe --integer 7
> options.exe -d 3.14

The options are then parsed and the values entered can be retrieved and used later on.

Relevant Examples

Example Description
ex_hello_world.cpp Prints "Hello, World!" using MEL print()
ex_chat.cpp A console based chat room over a network
ex_ctrl_c_handling.cpp Overview of catching control events
ex_options.cpp Overview of the Options class
ex_keyboard.cpp Windows only Keyboard input
⚠️ **GitHub.com Fallback** ⚠️