소스 검색

parse opt finished

tags/v1.0
Robin Thoni 9 년 전
부모
커밋
30749e20a1
2개의 변경된 파일41개의 추가작업 그리고 6개의 파일을 삭제
  1. 37
    6
      src/mainclass.cpp
  2. 4
    0
      src/mainclass.hh

+ 37
- 6
src/mainclass.cpp 파일 보기

13
 
13
 
14
 int MainClass::usage()
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
     " [--help|-h]" << std::endl;
21
     " [--help|-h]" << std::endl;
22
   std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
22
   std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
23
     << std::endl << std::endl;
23
     << std::endl << std::endl;
31
     << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
31
     << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
32
     << UINT_MAX << "."
32
     << UINT_MAX << "."
33
     << std::endl << "DEVICE \t: The device number. Default is 0. "
33
     << std::endl << "DEVICE \t: The device number. Default is 0. "
34
-    "0 <= DEVICE <= 4" << std::endl;
34
+    "0 <= DEVICE <= 4." << std::endl;
35
   return EX_USAGE;
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
 bool MainClass::build_actions_()
47
 bool MainClass::build_actions_()
39
 {
48
 {
40
   option opts[] = {
49
   option opts[] = {
47
   };
56
   };
48
   int opt;
57
   int opt;
49
   extern int optind;
58
   extern int optind;
59
+  extern char* optarg;
60
+  char* endptr;
50
   while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
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
     if (opt == 'e')
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
     else if (opt == 'u')
77
     else if (opt == 'u')
56
     {
78
     {
79
+      if (arg > 7)
80
+        return bad_value_();
57
     }
81
     }
58
     else if (opt == 'd')
82
     else if (opt == 'd')
59
     {
83
     {
84
+      if (arg > 7)
85
+        return bad_value_();
60
     }
86
     }
61
     else if (opt == 's')
87
     else if (opt == 's')
62
     {
88
     {
89
+      if (arg > UINT_MAX)
90
+        return bad_value_();
63
     }
91
     }
64
     else
92
     else
65
       return false;
93
       return false;
71
 {
99
 {
72
   if (argc_ <= 2 || !build_actions_())
100
   if (argc_ <= 2 || !build_actions_())
73
     return usage();
101
     return usage();
102
+  for (auto f : actions_)
103
+    if (!f())
104
+      return 1;
74
   return 0;
105
   return 0;
75
 }
106
 }

+ 4
- 0
src/mainclass.hh 파일 보기

1
 #ifndef MAINCLASS_HH
1
 #ifndef MAINCLASS_HH
2
 # define MAINCLASS_HH
2
 # define MAINCLASS_HH
3
+# include <vector>
4
+# include <memory>
3
 class MainClass
5
 class MainClass
4
 {
6
 {
5
   public:
7
   public:
10
 
12
 
11
   private:
13
   private:
12
     bool build_actions_();
14
     bool build_actions_();
15
+    bool bad_value_();
13
     int argc_;
16
     int argc_;
14
     char** argv_;
17
     char** argv_;
18
+    std::vector<std::function<bool()>> actions_;
15
 };
19
 };
16
 #endif /* !MAINCLASS_HH */
20
 #endif /* !MAINCLASS_HH */

Loading…
취소
저장