use positional arg to specify build target

master
Daniel Kolesa 2017-05-21 19:35:32 +02:00
parent f3e367aff8
commit d8aee86526
2 changed files with 24 additions and 19 deletions

View File

@ -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);

View File

@ -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<typename OutputRange>
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<typename OutputRange>
OutputRange &&print_help(OutputRange &&range) {
p_helpfmt.format_usage(range);