forked from OctaForge/libostd
use positional arg to specify build target
parent
f3e367aff8
commit
d8aee86526
18
build.cc
18
build.cc
|
@ -146,7 +146,9 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
ostd::arg_parser ap;
|
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)
|
ap.add_optional("--no-examples", 0)
|
||||||
.help("do not build examples")
|
.help("do not build examples")
|
||||||
|
@ -172,9 +174,17 @@ int main(int argc, char **argv) {
|
||||||
.help("print entire commands")
|
.help("print entire commands")
|
||||||
.action(ostd::arg_store_true(verbose));
|
.action(ostd::arg_store_true(verbose));
|
||||||
|
|
||||||
ap.add_optional("--clean", 0)
|
ap.add_positional("target", ostd::arg_value::OPTIONAL)
|
||||||
.help("remove generated files and exit")
|
.help("the action to perform")
|
||||||
.action(ostd::arg_store_true(clean));
|
.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 {
|
try {
|
||||||
ap.parse(argc, argv);
|
ap.parse(argc, argv);
|
||||||
|
|
|
@ -250,6 +250,16 @@ struct arg_positional: arg_argument {
|
||||||
return *this;
|
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 {
|
bool used() const {
|
||||||
return p_used;
|
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>
|
template<typename OutputRange>
|
||||||
OutputRange &&print_help(OutputRange &&range) {
|
OutputRange &&print_help(OutputRange &&range) {
|
||||||
p_helpfmt.format_usage(range);
|
p_helpfmt.format_usage(range);
|
||||||
|
|
Loading…
Reference in New Issue