|
@@ -13,11 +13,11 @@ MainClass::MainClass(int argc, char* argv[])
|
13
|
13
|
|
14
|
14
|
int MainClass::usage()
|
15
|
15
|
{
|
16
|
|
- std::cerr << "Usage: " << basename(argv_[0]) <<
|
17
|
|
- " [--device DEVICE|--device=DEVICE|-e DEVICE]" <<
|
18
|
|
- " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" <<
|
19
|
|
- " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" <<
|
20
|
|
- " [--sleep MS|--sleep=MS|-s MS]" <<
|
|
16
|
+ std::cerr << "Usage: " << basename(argv_[0]) << std::endl <<
|
|
17
|
+ " [--device DEVICE|--device=DEVICE|-e DEVICE]" << std::endl <<
|
|
18
|
+ " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
|
|
19
|
+ " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
|
|
20
|
+ " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
|
21
|
21
|
" [--help|-h]" << std::endl;
|
22
|
22
|
std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
|
23
|
23
|
<< std::endl << std::endl;
|
|
@@ -31,10 +31,19 @@ int MainClass::usage()
|
31
|
31
|
<< std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
|
32
|
32
|
<< UINT_MAX << "."
|
33
|
33
|
<< std::endl << "DEVICE \t: The device number. Default is 0. "
|
34
|
|
- "0 <= DEVICE <= 4" << std::endl;
|
|
34
|
+ "0 <= DEVICE <= 4." << std::endl;
|
35
|
35
|
return EX_USAGE;
|
36
|
36
|
}
|
37
|
37
|
|
|
38
|
+bool MainClass::bad_value_()
|
|
39
|
+{
|
|
40
|
+ extern int optind;
|
|
41
|
+ extern char* optarg;
|
|
42
|
+ std::cerr << "Bad value " << optarg <<
|
|
43
|
+ " for option " << argv_[optind - 2] << std::endl << std::endl;
|
|
44
|
+ return false;
|
|
45
|
+}
|
|
46
|
+
|
38
|
47
|
bool MainClass::build_actions_()
|
39
|
48
|
{
|
40
|
49
|
option opts[] = {
|
|
@@ -47,19 +56,38 @@ bool MainClass::build_actions_()
|
47
|
56
|
};
|
48
|
57
|
int opt;
|
49
|
58
|
extern int optind;
|
|
59
|
+ extern char* optarg;
|
|
60
|
+ char* endptr;
|
50
|
61
|
while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
|
51
|
62
|
{
|
|
63
|
+ if (!optarg)
|
|
64
|
+ return false;
|
|
65
|
+ long int arg = strtol(optarg, &endptr, 10);
|
|
66
|
+ if (arg < 0)
|
|
67
|
+ return bad_value_();
|
52
|
68
|
if (opt == 'e')
|
53
|
69
|
{
|
|
70
|
+ if (arg > 4)
|
|
71
|
+ return bad_value_();
|
|
72
|
+ actions_.push_back([arg]() -> bool
|
|
73
|
+ {
|
|
74
|
+ return true;
|
|
75
|
+ });
|
54
|
76
|
}
|
55
|
77
|
else if (opt == 'u')
|
56
|
78
|
{
|
|
79
|
+ if (arg > 7)
|
|
80
|
+ return bad_value_();
|
57
|
81
|
}
|
58
|
82
|
else if (opt == 'd')
|
59
|
83
|
{
|
|
84
|
+ if (arg > 7)
|
|
85
|
+ return bad_value_();
|
60
|
86
|
}
|
61
|
87
|
else if (opt == 's')
|
62
|
88
|
{
|
|
89
|
+ if (arg > UINT_MAX)
|
|
90
|
+ return bad_value_();
|
63
|
91
|
}
|
64
|
92
|
else
|
65
|
93
|
return false;
|
|
@@ -71,5 +99,8 @@ int MainClass::execute()
|
71
|
99
|
{
|
72
|
100
|
if (argc_ <= 2 || !build_actions_())
|
73
|
101
|
return usage();
|
|
102
|
+ for (auto f : actions_)
|
|
103
|
+ if (!f())
|
|
104
|
+ return 1;
|
74
|
105
|
return 0;
|
75
|
106
|
}
|