diff --git a/build.cc b/build.cc index 16d3421..9e6c816 100644 --- a/build.cc +++ b/build.cc @@ -146,7 +146,9 @@ int main(int argc, char **argv) { ostd::arg_parser ap; - auto &help = ap.add_help("print this message and exit"); + auto &help = ap.add_optional("-h", "--help", 0) + .help("print this message and exit") + .action(ostd::arg_print_help(ap)); ap.add_optional("--no-examples", 0) .help("do not build examples") @@ -172,9 +174,17 @@ int main(int argc, char **argv) { .help("print entire commands") .action(ostd::arg_store_true(verbose)); - ap.add_optional("--clean", 0) - .help("remove generated files and exit") - .action(ostd::arg_store_true(clean)); + ap.add_positional("target", ostd::arg_value::OPTIONAL) + .help("the action to perform") + .action([&clean](auto vals) { + if (!vals.empty()) { + if (vals.front() == "clean") { + clean = true; + } else if (vals.front() != "build") { + throw ostd::arg_error{"invalid build action"}; + } + } + }); try { ap.parse(argc, argv); diff --git a/ostd/argparse.hh b/ostd/argparse.hh index 3c4f188..1643388 100644 --- a/ostd/argparse.hh +++ b/ostd/argparse.hh @@ -250,6 +250,16 @@ struct arg_positional: arg_argument { return *this; } + arg_positional &help(string_range str) { + arg_argument::help(str); + return *this; + } + + arg_positional &metavar(string_range str) { + arg_argument::metavar(str); + return *this; + } + bool used() const { return p_used; } @@ -434,21 +444,6 @@ public: } } - template - arg_optional &add_help(OutputRange out, string_range msg) { - auto &opt = add_optional("-h", "--help", 0); - opt.help(msg); - opt.action([this, out = std::move(out)](auto) mutable { - this->print_help(out); - this->stop_parsing(); - }); - return opt; - } - - arg_optional &add_help(string_range msg) { - return add_help(cout.iter(), msg); - } - template OutputRange &&print_help(OutputRange &&range) { p_helpfmt.format_usage(range);