| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,4 +3,4 @@ cmake_minimum_required(VERSION 3.5) | |||
| 3 | 3 | include(Sanitizers) | |
| 4 | 4 | ||
| 5 | 5 | add_subdirectory(parser) | |
| 6 | - add_subdirectory(mode_chooser) | ||
| 6 | + add_subdirectory(command_parser) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | cmake_minimum_required(VERSION 3.5) | |
| 2 | 2 | ||
| 3 | - project(cpparg-examples-mode-chooser CXX) | ||
| 3 | + project(cpparg-examples-command-parser CXX) | ||
| 4 | 4 | ||
| 5 | 5 | set(CMAKE_CXX_STANDARD 17) | |
| 6 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,64 @@ | |||
| 1 | + #include <cpparg/cpparg.h> | ||
| 2 | + #include <iostream> | ||
| 3 | + | ||
| 4 | + int run_handler(int argc, const char* argv[]) { | ||
| 5 | + cpparg::parser parser("run command"); | ||
| 6 | + | ||
| 7 | + parser.add('e', "executable").required().description("Executable to run").handle([](auto s) { | ||
| 8 | + std::cout << "Executable: " << s << std::endl; | ||
| 9 | + }); | ||
| 10 | + | ||
| 11 | + parser.parse(argc, argv); | ||
| 12 | + | ||
| 13 | + return 0; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + int qmain(int argc, const char* argv[]) { | ||
| 17 | + cpparg::command_parser cmds("./program"); | ||
| 18 | + cmds | ||
| 19 | + .command("run").description("Run executable").handle(run_handler); | ||
| 20 | + cmds | ||
| 21 | + .command("test").description("Manage tests").handle([](int argc, const char* argv[]) { | ||
| 22 | + cpparg::command_parser commands("test commands"); | ||
| 23 | + commands.command("run").description("Run tests").handle([](auto, auto) { | ||
| 24 | + std::cout << "test run called" << std::endl; | ||
| 25 | + return 0; | ||
| 26 | + }); | ||
| 27 | + | ||
| 28 | + commands.command("add").description("Add new test").handle([](auto, auto) { | ||
| 29 | + std::cout << "test add called" << std::endl; | ||
| 30 | + return 0; | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + return commands.parse(argc, argv); | ||
| 34 | + }); | ||
| 35 | + | ||
| 36 | + return cmds.parse(argc, argv); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + int main(int argc, const char* argv[]) { | ||
| 40 | + cpparg::command_parser parser("./path-to-program"); | ||
| 41 | + parser.title("Test command parser"); | ||
| 42 | + parser | ||
| 43 | + .command("test") | ||
| 44 | + .description("Manage tests") | ||
| 45 | + .handle([](int argc, const char* argv[]) { | ||
| 46 | + cpparg::parser args("./path-to-program init"); | ||
| 47 | + | ||
| 48 | + int i; | ||
| 49 | + std::string s; | ||
| 50 | + | ||
| 51 | + args.add('i', "int").default_value(123).store(i); | ||
| 52 | + args.add("string").default_value("qwe").store(s); | ||
| 53 | + | ||
| 54 | + args.parse(argc, argv, cpparg::parsing_error_policy::rethrow); | ||
| 55 | + }); | ||
| 56 | + parser | ||
| 57 | + .default_command("commit") | ||
| 58 | + .description("Commit files") | ||
| 59 | + .handle([](int, const char*[]) { | ||
| 60 | + std::cout << "commit" << std::endl; | ||
| 61 | + }); | ||
| 62 | + | ||
| 63 | + return parser.parse(argc, argv, cpparg::parsing_error_policy::rethrow); | ||
| 64 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments