Skip to content

A wrapper for organizing tools, tests, examples which run in the command line

License

Notifications You must be signed in to change notification settings

dnau-ab/CLProgram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CLProgram

What is CLProgram?

CLProgram is simply a wrapper class for organizing tools, tests, examples which run in the command line


How to use

#include "CLProgram.h" // include the header file

// inherit from CLProgram
class CLDerived : public CLProgram {
public:
    CLDerived() : CLProgram("Derived Program") {
        // ...
    }

    // override the run function
    void run() override {
        // program code
    }
    
};

int main() {

    // create and add subprograms to program
    CLProgram* submenu = new CLProgram("My Programs", "Description");
    submenu->addSubprogram(new CLDerived());

    // add subprograms to root program
    CLProgram* root = new CLProgram("Main Menu");
    root->addSubprogram(new CLProgram("About", "This is an example"));
    root->addSubprogram(submenu);

    // run the root program
    root->run();

    return 0;
}

Check out the CLFunction, CLEcho, and CLBanking examples for how to wrap your program


What does it look like?

Here is what the above example prompt would look like

----[[ Main Menu ]]----
[a]     About
[b]     My Programs
[<]     Exit Main Menu

Selecting option [a] results in:

----[[ About ]]----
This is an example
[<]     Exit About

Selecting option [b] results in:

----[[ My Programs ]]----
Description
[a]     Derived Program
[<]     Exit My Programs

The prompt function is also overridable so you can change this to look however you like!


Examples

CLEcho

This class demonstrates inheriting from CLProgram and overriding it's run function to run a simple program.

Things to note: When overriding CLProgram::run, the program will immediately exit to the last CLProgram and not pause if CLProgram::prompt is not called at the end of the function

CLFunction may be a better option if the code you are wrapping is rather simple

----[[ Echo ]]----
This program echos to the console the text that you type!
Press <Ctrl-Z> then <Enter> on a new line to finish
Enter text:
This is a line!
And another
^Z
You typed:
This is a line!
And another

[<]     Exit Echo

CLFunction

This class demonstrates a CLProgram which wraps a function allowing any function to be added as an option in a CLProgram menu

Here we wrap this simple add function:

void add() {
    int a, b;

    std::cout << "Enter first number: ";
    std::cin >> a;

    std::cout << "Enter second number: ";
    std::cin >> b;

    std::cout << a << " + " << b << " = " << (a + b) << "\n";
}

The output:

----[[ Simple add ]]----
Enter first number: 2
Enter second number: 2
2 + 2 = 4
[<]     Exit Simple add

CLBanking

This class demonstrates a CLProgram which saves other runtime-persistant state information such as a map of accounts and balances.

Check out the source for details

About

A wrapper for organizing tools, tests, examples which run in the command line

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages