Skip to content

Commit

Permalink
merge args.cc into chai.cc for slightly faster builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
shsms committed Jan 10, 2021
1 parent ca10e98 commit 7ea5437
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 87 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions include/mime/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <vector>

namespace mime {

class args_parser {
public:
args_parser(int argc, char **argv);
Expand Down Expand Up @@ -34,4 +36,5 @@ class args_parser {
char **argv;
};

} // namespace mime
#endif /* MIME_ARGS_HH */
84 changes: 0 additions & 84 deletions src/cmd/args.cc

This file was deleted.

85 changes: 85 additions & 0 deletions src/cmd/chai.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &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<bool>()->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<int>()->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<float>()->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<std::string>()->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<std::string> positional;
options.add_options()("positional", "positional arguments",
cxxopts::value<std::vector<std::string>>(positional));
options.parse_positional("positional");
auto results = options.parse(argc, argv);

std::vector<chaiscript::Boxed_Value> 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<bool>());
}
for (auto attr : int_attr) {
ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as<int>());
}
for (auto attr : float_attr) {
ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as<float>());
}
for (auto attr : string_attr) {
ret.get_attr(attr) = chaiscript::Boxed_Value(results[attr].as<std::string>());
}

ret.set_explicit(true);
return ret;
}

} // namespace mime
2 changes: 1 addition & 1 deletion unittests/args_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 7ea5437

Please sign in to comment.