Skip to content

Instantly share code, notes, and snippets.

@wulfgarpro
Created July 16, 2022 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wulfgarpro/2e37329c76133ac0dc8314df53274add to your computer and use it in GitHub Desktop.
Save wulfgarpro/2e37329c76133ac0dc8314df53274add to your computer and use it in GitHub Desktop.
Basic cli interface for cpp
#include <iostream>
void help() {
std::cout << "Help" << std::endl;
}
void option1(std::string arg) {
std::cout << "option1: " << arg << std::endl;
}
void option2(std::string arg1, std::string arg2) {
std::cout << "option2: " << arg1 << " " << arg2 << std::endl;
}
int main(int argc, char * argv[]) {
// ./bin 1 arg
// ./bin 2 arg arg
if (argc >= 3) {
char * option = argv[1];
if (option == std::string("1") && argc == 3) {
option1(argv[2]);
} else if (option == std::string("2") && argc == 4) {
option2(argv[2], argv[3]);
} else {
help();
}
} else {
help();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment