add -h --help flag

This commit is contained in:
Carl Pearson
2019-09-30 13:23:25 -05:00
parent 46aa8c85ac
commit c28e7b0945

View File

@@ -1,7 +1,9 @@
#include <cassert> #include <cassert>
#include <cerrno> #include <cerrno>
#include <chrono>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#ifdef __linux__ #ifdef __linux__
@@ -155,6 +157,11 @@ int main(int argc, char **argv) {
std::string stdoutPath; std::string stdoutPath;
std::string stderrPath; std::string stderrPath;
int iters = 1; int iters = 1;
int sleepMs = 1000;
bool help = false;
auto helpMode = option("-h", "--help").set(help).doc("show help");
auto shieldGroup = ((option("-u").doc("number of unshielded CPUs") & auto shieldGroup = ((option("-u").doc("number of unshielded CPUs") &
value("INT", numUnshielded)) | value("INT", numUnshielded)) |
@@ -184,12 +191,15 @@ int main(int argc, char **argv) {
(option("--stderr").doc("redirect child stderr") & (option("--stderr").doc("redirect child stderr") &
value("PATH", stderrPath))); value("PATH", stderrPath)));
auto cli = ((noModMode | modMode), auto cli =
(option("-n").doc("run multiple times") & value("INT", iters)), helpMode |
// run everything after "--" ((noModMode | modMode),
required("--") & greedy(values("cmd", program)) (option("--sleep-ms").doc("sleep before run") & value("INT", sleepMs)),
(option("-n").doc("run multiple times") & value("INT", iters)), helpMode,
// run everything after "--"
required("--") & greedy(values("cmd", program))
); );
if (!parse(argc, argv, cli)) { if (!parse(argc, argv, cli)) {
auto fmt = doc_formatting{}.doc_column(31); auto fmt = doc_formatting{}.doc_column(31);
@@ -197,6 +207,12 @@ int main(int argc, char **argv) {
return -1; return -1;
} }
if (help) {
auto fmt = doc_formatting{}.doc_column(31);
std::cout << make_man_page(cli, argv[0], fmt);
return 0;
}
// open the redirect files, if needed // open the redirect files, if needed
int errf = 0; int errf = 0;
int outf = 0; int outf = 0;
@@ -333,6 +349,12 @@ int main(int argc, char **argv) {
PERFECT(perfect::drop_caches()); PERFECT(perfect::drop_caches());
} }
// sleep before each run
if (sleepMs) {
std::cerr << "sleep " << sleepMs << " ms before run\n";
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
}
std::cerr << "exec "; std::cerr << "exec ";
for (size_t i = 0; i < args.size() - 1; ++i) { for (size_t i = 0; i < args.size() - 1; ++i) {
std::cerr << args[i] << " "; std::cerr << args[i] << " ";