diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b105814..14af7a7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,4 +8,11 @@ FetchContent_Declare( FetchContent_MakeAvailable(json) -target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json dal library) \ No newline at end of file +FetchContent_Declare( + Boost + GIT_REPOSITORY https://github.com/boostorg/boost.git + GIT_TAG boost-1.82.0 +) +FetchContent_MakeAvailable(Boost) + +target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json Boost::program_options dal library) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b450e15..7e41748 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,116 @@ #include "cppScratch/test.hpp" #include "cppScratch/dal/dal.hpp" +#include +#include + #include +#include using namespace std; +namespace po = boost::program_options; -int main(void) { - cout << "hello world" << endl; - dal_func(); - example_func(); +void on_age(int age) +{ + std::cout << "On age: " << age << '\n'; +} + +int main(int argc, const char *argv[]) { + //////////// + // DECLARE + //////////// + + try{ + + int def; + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "produce help message") + ("compression,c", po::value(), "set compression level") + ("def,d", po::value(&def)->default_value(10), "set def level") + ("abc", po::value()->default_value(25), "set abc level") + ("phone", po::value>()->multitoken()-> + zero_tokens()->composing(), "Phone") + ("age,a", po::value()->notifier(on_age), "age") + ("config", po::value(), "Config file") + ; + + po::options_description other("Other"); + desc.add_options() + ("other-option,o", po::value(), "another option") + ; + + // Hidden options, will be allowed both on command line and + // in config file, but will not be shown to the user. + po::options_description hidden("Hidden options"); + hidden.add_options() + ("input-file", po::value< vector >(), "input file") + ; + + po::positional_options_description p; + p.add("phone", -1); + + po::options_description cmdline_options; + cmdline_options.add(desc).add(other).add(hidden); + + po::options_description config_file_options; + config_file_options.add(desc).add(hidden); + + po::options_description visible("Allowed options"); + visible.add(desc).add(other); + + //////////// + // PARSE + //////////// + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv) + .options(desc) + .positional(p) + .run(), + vm); + if (vm.count("config")) + { + cout << "Attempting file config load" << endl; + std::ifstream ifs{vm["config"].as().c_str()}; + if (ifs) { + cout << "File opened, loading..." << endl; + po::store(po::parse_config_file(ifs, config_file_options), vm); + } + } + po::notify(vm); + + if (vm.count("help")) { + cout << desc << "\n"; + return 1; + } + + if (vm.count("compression")) { + cout << "Compression level was set to " + << vm["compression"].as() << ".\n"; + } else { + cout << "Compression level was not set.\n"; + } + + if (vm.count("def")) { + cout << "def level was set to " + << vm["def"].as() << ".\n"; + } else { + cout << "def level was not set.\n"; + } + + // if (vm.count("phone")) + // { + // cout << "phone are: " + // << vm["phone"].as< vector >() << "\n"; + // } + + cout << "hello world" << endl; + dal_func(); + example_func(); + } + catch (const po::error &ex) + { + std::cerr << ex.what() << '\n'; + } } \ No newline at end of file