-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
58 lines (49 loc) · 1.54 KB
/
driver.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
#include <iostream>
#include <unordered_map>
#include "FF.h"
namespace {
void printUsage() {
std::cout << "Usage: ff [-f <float> | -h <half> | -d <double>]\n";
std::cout << "Floats can be entered in hex integer, hex float, "
<< "fixed point float, or scientific float formats\n";
std::cout << " -h, --half <half> 16-bit half precision\n";
std::cout << " -f, --float <float> 32-bit single precision\n";
std::cout << " -d, --double <double> 64-bit double precision\n";
std::cout << " --help Displays this message and exit\n";
}
using ParseFunctorType = std::add_pointer<decltype(FF::parseHalf)>::type;
const std::unordered_map<std::string, ParseFunctorType> functor_map = {
{"--half", FF::parseHalf}, {"-h", FF::parseHalf},
{"--float", FF::parseSingle}, {"-f", FF::parseSingle},
{"--double", FF::parseDouble}, {"-d", FF::parseDouble},
};
}
int main(int argc, char* argv[]) {
if (argc == 1) {
printUsage();
return -1;
}
for (int i = 1; i < argc; i++) {
const std::string arg(argv[i]);
if (arg == "--help") {
printUsage();
return 0;
}
ParseFunctorType parser;
try {
parser = functor_map.at(arg);
} catch (std::out_of_range&) {
std::cerr << "Unkown argument " << arg << "\n";
return -1;
}
if (i == (argc - 1)) {
std::cerr << "Expected another argument after " << arg << "\n";
return -1;
}
parser(argv[i + 1]);
return 0;
}
std::cerr << "Incorrect Usage.\n";
printUsage();
return -1;
}