Last active
February 7, 2022 14:01
-
-
Save rroberrt/f0e4836c06ded8f4f3a46c13ec7eec97 to your computer and use it in GitHub Desktop.
A sample void function that requires some changes...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// db-conf CLI | |
// | |
// GitHub: https://github.com/kerig-it/db-conf | |
// Libraries | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
// Namespaces | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
using std::string; | |
using std::vector; | |
// CLI name | |
string cli_name = "db-conf"; | |
// Output printer | |
void print(string str = "", bool parsing = false) { | |
// Is the `parsing` argument enabled? | |
if (parsing) { | |
// Is the supplied string "default"? | |
if (str == "default") { | |
// Print the default input prompt. | |
cout << cli_name << "> "; | |
} | |
} | |
// Is the supplied string something else? | |
else { | |
// Print supplied string. | |
cout << str << endl; | |
} | |
} | |
// Help printer | |
void help(string arg = "") { | |
// Is no argument supplied? | |
if (arg.length() == 0) { | |
// Print help. | |
print("Available commands:"); | |
print(" exit\tExit the " + cli_name + " CLI."); | |
print(" help\tPrint this message."); | |
} | |
// Is an argument supplied? | |
else if (arg.length() > 0) { | |
print("Support for help for individual commands to be added."); | |
} | |
} | |
// Processing function | |
bool process(string command = "") { | |
// Is a command specified? | |
if (command.length() > 0) { | |
// Is the command set to "exit"? | |
if (command == "exit") { | |
// Print feedback and exit. | |
print("Exiting..."); | |
std::exit(0); | |
} | |
// Is the command set to "help"? | |
else if (command == "help") { | |
// Call `help` and return status. | |
help(); | |
return true; | |
} | |
// Is a different command specicied? | |
else { | |
// Print feedback and return status. | |
print("Unknown command specified: " + command); | |
return false; | |
} | |
} | |
// Print failure message and return `false`. | |
print("No command was specified."); | |
return false; | |
} | |
// Input handler function | |
void input(vector<string> args = { cli_name }) { | |
// Is an input supplied? | |
if (args.size() > 1) { | |
// Process supplied arguments and call `input` recursively. | |
process(args[1]); | |
input(); | |
} | |
// Is nothing supplied? | |
else { | |
// Print the default input prompt. | |
print("default", true); | |
// Define and assign via user input the variable `arg`. | |
string arg; | |
cin >> arg; | |
// Call `input` recursively. | |
input({ cli_name, arg }); | |
} | |
} | |
// Main function | |
int main(int argc, char* argv[]) { | |
// Print about CLI initialisation. | |
print("\nInitialising the db-conf CLI...\n"); | |
// Define a vector that will hold supplied arguments. | |
vector<string> args; | |
// "Convert" supplied arguments to a vector. | |
for (int i = 0; i < argc; ++i) { | |
// Add item from `argv` to `args`. | |
args.push_back(argv[i]); | |
} | |
// Initialise CLI. | |
if (argc > 1) { | |
input(args); | |
} | |
else { | |
input(); | |
} | |
// Return success code. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment