Created
July 16, 2022 09:44
-
-
Save wulfgarpro/2e37329c76133ac0dc8314df53274add to your computer and use it in GitHub Desktop.
Basic cli interface for cpp
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
#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