-
Notifications
You must be signed in to change notification settings - Fork 6
/
ephemerand.cpp
71 lines (52 loc) · 1.96 KB
/
ephemerand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <docopt/docopt.h>
#include "ephemerand/gps_utils.h"
#include "ephemerand/util.h"
namespace ephemerand {
void cmd_run(std::string device, bool verbose, bool loop);
void cmd_reset(std::string device, bool verbose);
};
static const char USAGE[] =
R"(ephemerand - global randomness beacon
Usage:
ephemerand run [--device=<device>] [--verbose] [--loop]
ephemerand reset [--device=<device>] [--verbose]
ephemerand decode-gps-time <week> <time_of_week>
ephemerand (-h | --help)
ephemerand --version
Options:
-h --help
--version
--device=<device> Serial device to use (default: /dev/ttyACM0)
--verbose Print extra information (default: false)
--loop Don't exit. Print new random values as they are generated (default: false)
)";
int parse_command_line(int argc, char **argv) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE, { argv + 1, argv + argc }, true, "ephemerand 0.0.1");
std::string device = "/dev/ttyACM0";
if (args["--device"]) device = args["--device"].asString();
bool verbose = false;
if (args["--verbose"]) verbose = args["--verbose"].asBool();
if (args["run"].asBool()) {
bool loop = false;
if (args["--loop"]) loop = args["--loop"].asBool();
ephemerand::cmd_run(device, verbose, loop);
} else if (args["reset"].asBool()) {
ephemerand::cmd_reset(device, verbose);
} else if (args["decode-gps-time"].asBool()) {
time_t t = decode_gps_time(args["<week>"].asLong(), args["<time_of_week>"].asLong());
std::cout << t << std::endl;
} else {
throw ephemerand::error("unrecognized command");
}
return 0;
}
int main(int argc, char **argv) {
try {
parse_command_line(argc, argv);
} catch (std::exception &e) {
std::cerr << "CAUGHT EXCEPTION, ABORTING: " << e.what() << std::endl;
::exit(1);
}
return 0;
}