-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
95 lines (91 loc) · 2.66 KB
/
example.cpp
File metadata and controls
95 lines (91 loc) · 2.66 KB
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
#include "dh_validator_cpp.hpp"
#include <any>
#include <iostream>
static void
test1 ()
{
dh::VectorValidator<int64_t> validator (false);
validator.add_range (0, 100);
validator.add_range (-1, 200);
dh::Arg arg;
arg.add_arg ('t', "test", "Test for readline");
std::any ret = dh::GetOutput::get_output (&validator, &arg, "test: ");
try
{
auto ret_val = std::any_cast<std::vector<int64_t>> (ret);
for (auto val : ret_val)
std::cout << val << std::endl;
}
catch (const std::bad_any_cast &e)
{
}
}
static void
test2 ()
{
std::vector<std::string> string_list;
/* This is a sample of the string add & view func
* Showing in Chinese */
setlocale (LC_ALL, "");
while (true)
{
dh::Arg arg;
std::vector<std::string> vstr = { "add", "添加" };
arg.add_arg ('a', vstr, "添加字符串");
arg.add_arg ('v', "查看字符串", "view");
std::cout << "[0] 添加字符串\n" << "[1] 查看字符串\n";
char val = dh::GetOutput::get_output (&arg, "选择选项: ", true);
if (val == 'a')
{
std::any str
= dh::GetOutput::get_output ("请输入字符串: ");
try
{
auto str_val = std::any_cast<std::string> (str);
string_list.push_back (str_val);
}
catch (const std::bad_any_cast &e)
{
break;
}
}
else if (val == 'v')
{
for (auto str : string_list)
std::cout << str << std::endl;
}
else if (val == 0)
{
break;
}
}
}
void
test3 ()
{
dh::GetOutput::get_output ((DhReadlineFns *)nullptr, nullptr);
}
int
main ()
{
std::cout << "[0] Regular test\n";
std::cout << "[1] Test with Chinese\n";
std::cout << "[2] No readline excpetion\n";
dh::RangeValidator<int64_t> validator (0, 2);
std::any ret = dh::GetOutput::get_output (&validator, nullptr,
"Choose a example to show: ");
try
{
int64_t val = std::any_cast<int64_t> (ret);
if (val == 0)
test1 ();
if (val == 1)
test2 ();
if (val == 2)
test3 ();
}
catch (const std::bad_any_cast &e)
{
return -1;
}
}