-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.cc
103 lines (88 loc) · 2.28 KB
/
options.cc
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <unistd.h>
#include <getopt.h>
#include <iostream>
using namespace std;
#include "options.h"
OptionManager* OptionManager::_instance = nullptr;
OptionManager::OptionManager()
{
}
void OptionManager::usage()
{
cerr << "usage: " << _progName
<< " [-c|--card card] [-m|--mode [text|scene]] [-T|--tty ttyname] [-n|--nodaemon] [-h]" << endl
<< " scene mode [-t|--theme theme] " << endl
<< " text mode [-f|--font full_ttf_font_path] " << endl
<< endl;
exit(EXIT_FAILURE);
}
void OptionManager::parse(int argc, char *argv[])
{
_progName = {argv[0]};
if (_progName.find("./") == 0) {
_progName.erase(_progName.begin(), _progName.begin()+1);
}
struct option opts[] = {
{"mode", 1, NULL, 'm'},
{"theme", 1, NULL, 't'},
{"card", 1, NULL, 'c'},
{"tty", 1, NULL, 'T'},
{"nodaemon", 0, NULL, 'n'},
{"font", 1, NULL, 'f'},
{NULL, 0, NULL, 0},
};
int c, index;
while ((c = getopt_long(argc, argv, "f:m:t:c:T:nh", opts, &index)) != -1) {
switch(c) {
case 'm': _opts["mode"] = {optarg}; break;
case 't': _opts["theme"] = {optarg}; break;
case 'f': _opts["font"] = {optarg}; break;
case 'c': _opts["card"] = {optarg}; break;
case 'T': _opts["tty"] = {optarg}; break;
case 'n': _opts["nodaemon"] = "true"; break;
case 'h': usage(); break;
default: break;
}
}
}
OptionManager* OptionManager::get(int argc, char *argv[])
{
if (!_instance) {
_instance = new OptionManager;
_instance->parse(argc, argv);
}
return _instance;
}
int OptionManager::_value_helper(std::string opt, int)
{
int i = -1;
try {
i = std::stoi(opt);
} catch(...) {
//pass
}
return i;
}
bool OptionManager::_value_helper(std::string opt, bool)
{
bool b = false;
try {
b = std::stoi(opt);
} catch(...) {
b = (opt == "true" || opt == "t");
}
return b;
}
float OptionManager::_value_helper(std::string opt, float)
{
float f = 0.0;
try {
f = std::stof(opt);
} catch(...) {
}
return f;
}
std::string OptionManager::_value_helper(std::string opt, std::string)
{
return opt;
}