diff --git a/CMakeLists.txt b/CMakeLists.txt index 07226a3..a70b80e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,6 @@ add_library(mime STATIC src/lib/u32utils.cc) add_executable(mime-bin - src/cmd/args.cc src/cmd/chai.cc src/cmd/main.cc) @@ -35,7 +34,6 @@ set_target_properties(mime-bin add_subdirectory(vendor/googletest) add_executable(unittests - src/cmd/args.cc src/lib/mime.cc src/lib/u32utils.cc src/cmd/chai.cc diff --git a/include/mime/args.hh b/include/mime/args.hh index 329f968..e407bc0 100644 --- a/include/mime/args.hh +++ b/include/mime/args.hh @@ -6,6 +6,8 @@ #include #include +namespace mime { + class args_parser { public: args_parser(int argc, char **argv); @@ -34,4 +36,5 @@ class args_parser { char **argv; }; +} // namespace mime #endif /* MIME_ARGS_HH */ diff --git a/src/cmd/args.cc b/src/cmd/args.cc deleted file mode 100644 index 1062b77..0000000 --- a/src/cmd/args.cc +++ /dev/null @@ -1,84 +0,0 @@ -#include - -args_parser::args_parser(int argc, char **argv) : options(argv[1]), argc(argc - 1), argv(++argv) {} - -void add_attr(const std::string &key, std::vector &attr_vec) { - auto commapos = key.find(','); - if (commapos == std::string::npos) { - attr_vec.push_back(key); - } else { - attr_vec.push_back(key.substr(commapos + 1)); - } -} - -void args_parser::bool_opt(const std::string &key, const std::string &desc, - const std::string &default_v) { - add_attr(key, bool_attr); - options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); -} - -void args_parser::bool_opt(const std::string &key, const std::string &desc) { - bool_opt(key, desc, "false"); -} - -void args_parser::int_opt(const std::string &key, const std::string &desc, - const std::string &default_v) { - add_attr(key, int_attr); - options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); -} - -void args_parser::int_opt(const std::string &key, const std::string &desc) { - int_opt(key, desc, "0"); -} - -void args_parser::float_opt(const std::string &key, const std::string &desc, - const std::string &default_v) { - add_attr(key, float_attr); - options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); -} - -void args_parser::float_opt(const std::string &key, const std::string &desc) { - float_opt(key, desc, "0.0"); -} - -void args_parser::string_opt(const std::string &key, const std::string &desc, - const std::string &default_v) { - add_attr(key, string_attr); - options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); -} - -void args_parser::string_opt(const std::string &key, const std::string &desc) { - string_opt(key, desc, ""); -} - -std::string args_parser::help() { return options.help(); } - -chaiscript::dispatch::Dynamic_Object args_parser::parse() { - chaiscript::dispatch::Dynamic_Object ret{}; - std::vector positional; - options.add_options()("positional", "positional arguments", - cxxopts::value>(positional)); - options.parse_positional("positional"); - auto results = options.parse(argc, argv); - - std::vector boxed_positional{}; - std::transform(positional.begin(), positional.end(), std::back_inserter(boxed_positional), - [](auto &x) { return chaiscript::Boxed_Value(x); }); - - ret.get_attr("positional") = chaiscript::Boxed_Value(boxed_positional); - for (auto attr : bool_attr) { - ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); - } - for (auto attr : int_attr) { - ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); - } - for (auto attr : float_attr) { - ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); - } - for (auto attr : string_attr) { - ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); - } - - ret.set_explicit(true); - return ret; -} diff --git a/src/cmd/chai.cc b/src/cmd/chai.cc index da0d0f9..017a8f9 100644 --- a/src/cmd/chai.cc +++ b/src/cmd/chai.cc @@ -209,4 +209,89 @@ int run(int argc, char **argv) { return 0; } + + +args_parser::args_parser(int argc, char **argv) : options(argv[1]), argc(argc - 1), argv(++argv) {} + +void add_attr(const std::string &key, std::vector &attr_vec) { + auto commapos = key.find(','); + if (commapos == std::string::npos) { + attr_vec.push_back(key); + } else { + attr_vec.push_back(key.substr(commapos + 1)); + } +} + +void args_parser::bool_opt(const std::string &key, const std::string &desc, + const std::string &default_v) { + add_attr(key, bool_attr); + options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); +} + +void args_parser::bool_opt(const std::string &key, const std::string &desc) { + bool_opt(key, desc, "false"); +} + +void args_parser::int_opt(const std::string &key, const std::string &desc, + const std::string &default_v) { + add_attr(key, int_attr); + options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); +} + +void args_parser::int_opt(const std::string &key, const std::string &desc) { + int_opt(key, desc, "0"); +} + +void args_parser::float_opt(const std::string &key, const std::string &desc, + const std::string &default_v) { + add_attr(key, float_attr); + options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); +} + +void args_parser::float_opt(const std::string &key, const std::string &desc) { + float_opt(key, desc, "0.0"); +} + +void args_parser::string_opt(const std::string &key, const std::string &desc, + const std::string &default_v) { + add_attr(key, string_attr); + options.add_options()(key, desc, cxxopts::value()->default_value(default_v)); +} + +void args_parser::string_opt(const std::string &key, const std::string &desc) { + string_opt(key, desc, ""); +} + +std::string args_parser::help() { return options.help(); } + +chaiscript::dispatch::Dynamic_Object args_parser::parse() { + chaiscript::dispatch::Dynamic_Object ret{}; + std::vector positional; + options.add_options()("positional", "positional arguments", + cxxopts::value>(positional)); + options.parse_positional("positional"); + auto results = options.parse(argc, argv); + + std::vector boxed_positional{}; + std::transform(positional.begin(), positional.end(), std::back_inserter(boxed_positional), + [](auto &x) { return chaiscript::Boxed_Value(x); }); + + ret.get_attr("positional") = chaiscript::Boxed_Value(boxed_positional); + for (auto attr : bool_attr) { + ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); + } + for (auto attr : int_attr) { + ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); + } + for (auto attr : float_attr) { + ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); + } + for (auto attr : string_attr) { + ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as()); + } + + ret.set_explicit(true); + return ret; +} + } // namespace mime diff --git a/unittests/args_test.cc b/unittests/args_test.cc index f177a9d..1caa7e8 100644 --- a/unittests/args_test.cc +++ b/unittests/args_test.cc @@ -50,7 +50,7 @@ class ArgsTest : public ::testing::Test { }; TEST_F(ArgsTest, AllTypes) { - args_parser parser(args.argc(), args.argv()); + mime::args_parser parser(args.argc(), args.argv()); parser.bool_opt("bool_v", "bool value"); parser.int_opt("i,int_v", "int value"); parser.float_opt("float_v", "float value");