use new argparse in build.cc

This commit is contained in:
q66 2017-05-16 00:40:09 +02:00
parent 0ee5404612
commit a368311e12

View file

@ -15,6 +15,7 @@
#include "ostd/filesystem.hh" #include "ostd/filesystem.hh"
#include "ostd/thread_pool.hh" #include "ostd/thread_pool.hh"
#include "ostd/channel.hh" #include "ostd/channel.hh"
#include "ostd/argparse.hh"
namespace fs = ostd::filesystem; namespace fs = ostd::filesystem;
@ -88,23 +89,6 @@ static void add_env(std::string &val, char const *evar) {
val += varv; val += varv;
} }
static void print_help(ostd::string_range arg0) {
ostd::writef(
"%s [options]\n"
"Available options:\n"
" [no-]examples - (do not) build examples (default: yes)\n"
" [no-]test-suite - (do not) build test suite (default: yes)\n"
" [no-]static-lib - (do not) build static libostd (default: yes)\n"
" [no-]shared-lib - (do not) build shared libostd (default: no)\n"
" release - release build (strip, no -g)\n"
" debug - debug build (default, no strip, -g)\n"
" verbose - print entire commands\n"
" clean - remove generated files and exit\n"
" help - print this and exit\n",
arg0
);
}
static void exec_command(strvec const &args) { static void exec_command(strvec const &args) {
if (int ret = ostd::subprocess{nullptr, ostd::iter(args)}.close(); ret) { if (int ret = ostd::subprocess{nullptr, ostd::iter(args)}.close(); ret) {
throw std::runtime_error{ostd::format( throw std::runtime_error{ostd::format(
@ -160,39 +144,49 @@ int main(int argc, char **argv) {
std::string ldflags = DEFAULT_LDFLAGS; std::string ldflags = DEFAULT_LDFLAGS;
std::string asflags = DEFAULT_ASFLAGS; std::string asflags = DEFAULT_ASFLAGS;
#define ARG_BOOL(arg, name, var) \ ostd::arg_parser ap;
if ((arg == name) || (arg == "no-" name)) { \
var = (arg.substr(0, 3) != "-no"); \
continue; \
}
for (int i = 1; i < argc; ++i) { auto &help = ap.add_help("print this message and exit");
std::string arg = argv[i];
ARG_BOOL(arg, "examples", build_examples); ap.add_optional('\0', "no-examples", ostd::arg_value::NONE)
ARG_BOOL(arg, "test-suite", build_testsuite); .help("do not build examples")
ARG_BOOL(arg, "static-lib", build_static); .action(ostd::arg_store_false(build_examples));
ARG_BOOL(arg, "shared-lib", build_shared);
if ((arg == "release") || (arg == "debug")) { ap.add_optional('\0', "no-test-suite", ostd::arg_value::NONE)
build_cfg = arg; .help("do not build test suite")
continue; .action(ostd::arg_store_false(build_testsuite));
}
if (arg == "verbose") { ap.add_optional('\0', "no-static", ostd::arg_value::NONE)
verbose = true; .help("do not build static libostd")
continue; .action(ostd::arg_store_false(build_static));
}
if (arg == "clean") { ap.add_optional('\0', "shared", ostd::arg_value::NONE)
clean = true; .help("build shared libostd")
continue; .action(ostd::arg_store_true(build_shared));
}
if (arg == "help") { ap.add_optional('\0', "config", ostd::arg_value::REQUIRED)
print_help(argv[0]); .help("build configuration (debug/release)")
return 0; .action(ostd::arg_store_str(build_cfg));
}
ostd::writefln("unknown argument: %s", arg.data()); ap.add_optional('v', "verbose", ostd::arg_value::NONE)
.help("print entire commands")
.action(ostd::arg_store_true(verbose));
ap.add_optional('\0', "clean", ostd::arg_value::NONE)
.help("remove generated files and exit")
.action(ostd::arg_store_true(clean));
try {
ap.parse(argc, argv);
} catch (ostd::arg_error const &e) {
ostd::cerr.writefln("argument parsing failed: %s", e.what());
ap.print_help(ostd::cerr.iter());
return 1; return 1;
} }
#undef ARG_BOOL if (help.used()) {
return 0;
}
std::string default_lib = OSTD_SHARED_LIB.string(); std::string default_lib = OSTD_SHARED_LIB.string();
if (build_static) { if (build_static) {