Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 1868b25

Browse files
Merge pull request #138 from youpong/introduce-options
Introduce Options
2 parents aefc73d + 6b88578 commit 1868b25

File tree

3 files changed

+123
-33
lines changed

3 files changed

+123
-33
lines changed

src/fetch.h

+39
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,42 @@ enum class Mode
391391
EXEC_TEST,
392392
SHOW_VERSION,
393393
};
394+
395+
class Options
396+
{
397+
public:
398+
Mode mode = Mode::NORMAL;
399+
string color_name = "def"s;
400+
string distro_name = "def"s;
401+
bool show_battery = false;
402+
403+
Options(){};
404+
Options(int argc, char *argv[])
405+
{
406+
int opt;
407+
while ((opt = getopt(argc, argv, "ta:d:vb")) != -1)
408+
{
409+
410+
switch (opt)
411+
{
412+
case 't':
413+
mode = Mode::EXEC_TEST;
414+
break;
415+
case 'a':
416+
color_name = string(optarg);
417+
break;
418+
case 'd':
419+
distro_name = string(optarg);
420+
break;
421+
case 'b':
422+
show_battery = true;
423+
break;
424+
case 'v':
425+
mode = Mode::SHOW_VERSION;
426+
break;
427+
default:
428+
exit(1);
429+
}
430+
}
431+
}
432+
};

src/main.cpp

+4-33
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,15 @@ void DisplayInfo(bool show_battery);
1212
*/
1313
int main(int argc, char *argv[])
1414
{
15-
Mode mode = Mode::NORMAL;
16-
string color_name = "def"s;
17-
string distro_name = "def"s;
18-
bool show_battery = false;
19-
20-
int opt;
21-
while ((opt = getopt(argc, argv, "ta:d:vb")) != -1)
22-
{
23-
switch (opt)
24-
{
25-
case 't':
26-
mode = Mode::EXEC_TEST;
27-
break;
28-
case 'a':
29-
color_name = string(optarg);
30-
break;
31-
case 'd':
32-
distro_name = string(optarg);
33-
break;
34-
case 'b':
35-
show_battery = true;
36-
break;
37-
case 'v':
38-
mode = Mode::SHOW_VERSION;
39-
break;
40-
default:
41-
return 1;
42-
}
43-
}
44-
15+
auto options = Options(argc, argv);
4516
if (optind != argc)
4617
{
4718
cout << "Error: "s << argv[0] << ": unknown argument: "s << argv[optind]
4819
<< endl;
4920
return 1;
5021
}
5122

52-
switch (mode)
23+
switch (options.mode)
5324
{
5425
case Mode::NORMAL:
5526
// no-op
@@ -67,8 +38,8 @@ int main(int argc, char *argv[])
6738
}
6839

6940
// Mode::NORMAL
70-
print(color_name, distro_name);
71-
DisplayInfo(show_battery);
41+
print(options.color_name, options.distro_name);
42+
DisplayInfo(options.show_battery);
7243

7344
return 0;
7445
}

src/util.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,85 @@ static void test_Crayon()
102102
expect("\033[0;31mHIJIKI\033[0;m"s, style.text("HIJIKI"), ""s);
103103
}
104104

105+
static void testhelper_Options(string msg, int argc, const char *argv[],
106+
Options expect, int expect_optind)
107+
{
108+
optind = 1;
109+
auto options = Options(argc, (char **)argv);
110+
111+
expect((int)expect.mode, (int)options.mode, msg + ": Options.mode"s);
112+
expect(expect.color_name, options.color_name,
113+
msg + ": Options.color_name"s);
114+
expect(expect.distro_name, options.distro_name,
115+
msg + ": Options.distro_name"s);
116+
expect(expect.show_battery, options.show_battery,
117+
msg + ": Options.show_battery"s);
118+
expect(expect_optind, optind, msg + ": optind"s);
119+
}
120+
121+
static void test_Options_default()
122+
{
123+
int argc = 1;
124+
const char *argv[] = {"procfetch", NULL};
125+
Options expect;
126+
expect.mode = Mode::NORMAL;
127+
expect.color_name = "def"s;
128+
expect.distro_name = "def"s;
129+
expect.show_battery = false;
130+
131+
testhelper_Options("default", argc, argv, expect, 1);
132+
}
133+
134+
static void test_Options_full()
135+
{
136+
int argc = 6;
137+
const char *argv[] = {"procfetch", "-a", "cyan", "-d",
138+
"Manjaro", "-b", "arg", NULL};
139+
Options expect;
140+
expect.mode = Mode::NORMAL;
141+
expect.color_name = "cyan"s;
142+
expect.distro_name = "Manjaro"s;
143+
expect.show_battery = true;
144+
145+
testhelper_Options("full", argc, argv, expect, 6); // remains last "arg"
146+
}
147+
148+
static void test_Options_test()
149+
{
150+
int argc = 2;
151+
const char *argv[] = {"procfetch", "-t", NULL};
152+
153+
Options expect;
154+
expect.mode = Mode::EXEC_TEST;
155+
expect.color_name = "def"s;
156+
expect.distro_name = "def"s;
157+
expect.show_battery = false;
158+
159+
testhelper_Options("test", argc, argv, expect, 2);
160+
}
161+
162+
static void test_Options_version()
163+
{
164+
int argc = 2;
165+
const char *argv[] = {"procfetch", "-v", NULL};
166+
167+
Options expect;
168+
expect.mode = Mode::SHOW_VERSION;
169+
expect.color_name = "def"s;
170+
expect.distro_name = "def"s;
171+
expect.show_battery = false;
172+
173+
testhelper_Options("version", argc, argv, expect, 2);
174+
}
175+
176+
static void test_Options()
177+
{
178+
test_Options_default();
179+
test_Options_full();
180+
test_Options_test();
181+
test_Options_version();
182+
}
183+
105184
/**
106185
* Tests belows.
107186
* * class Path
@@ -115,4 +194,5 @@ void test_util()
115194
test_Command_async();
116195
test_Command_async_exception();
117196
test_Crayon();
197+
test_Options();
118198
}

0 commit comments

Comments
 (0)