Quellcode durchsuchen

opt parser

tags/v1.0
Robin Thoni vor 10 Jahren
Ursprung
Commit
ab2f2f6e18
2 geänderte Dateien mit 54 neuen und 2 gelöschten Zeilen
  1. 53
    2
      src/mainclass.cpp
  2. 1
    0
      src/mainclass.hh

+ 53
- 2
src/mainclass.cpp Datei anzeigen

@@ -2,6 +2,8 @@
2 2
 #include <iostream>
3 3
 #include <libgen.h>
4 4
 #include <sysexits.h>
5
+#include <climits>
6
+#include <getopt.h>
5 7
 
6 8
 MainClass::MainClass(int argc, char* argv[])
7 9
   : argc_(argc)
@@ -12,13 +14,62 @@ MainClass::MainClass(int argc, char* argv[])
12 14
 int MainClass::usage()
13 15
 {
14 16
   std::cerr << "Usage: " << basename(argv_[0]) <<
15
-    " [--up 0-7] [--down 0-7] [--sleep ms]" << std::endl;
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]" <<
21
+    " [--help|-h]" << std::endl;
22
+  std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
23
+    << std::endl << std::endl;
24
+  std::cerr << "--device \tSwitch to the provided device."
25
+    << std::endl << "--up \t\tEnable the provided ouput pin."
26
+    << std::endl << "--down \t\tDisable the provided output pin."
27
+    << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
28
+    << std::endl << "--help \t\tDisplay this message."
29
+    << std::endl << std::endl;
30
+  std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
31
+    << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
32
+    << UINT_MAX << "."
33
+    << std::endl << "DEVICE \t: The device number. Default is 0. "
34
+    "0 <= DEVICE <= 4" << std::endl;
16 35
   return EX_USAGE;
17 36
 }
18 37
 
38
+bool MainClass::build_actions_()
39
+{
40
+  option opts[] = {
41
+    {"device", 1, 0, 'e'},
42
+    {"up", 1, 0, 'u'},
43
+    {"down", 1, 0, 'd'},
44
+    {"sleep", 1, 0, 's'},
45
+    {"help", 0, 0, 'h'},
46
+    {0, 0, 0, 0}
47
+  };
48
+  int opt;
49
+  extern int optind;
50
+  while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
51
+  {
52
+    if (opt == 'e')
53
+    {
54
+    }
55
+    else if (opt == 'u')
56
+    {
57
+    }
58
+    else if (opt == 'd')
59
+    {
60
+    }
61
+    else if (opt == 's')
62
+    {
63
+    }
64
+    else
65
+      return false;
66
+  }
67
+  return optind == argc_;
68
+}
69
+
19 70
 int MainClass::execute()
20 71
 {
21
-  if (argc_ <= 2)
72
+  if (argc_ <= 2 || !build_actions_())
22 73
     return usage();
23 74
   return 0;
24 75
 }

+ 1
- 0
src/mainclass.hh Datei anzeigen

@@ -9,6 +9,7 @@ class MainClass
9 9
     int execute();
10 10
 
11 11
   private:
12
+    bool build_actions_();
12 13
     int argc_;
13 14
     char** argv_;
14 15
 };

Laden…
Abbrechen
Speichern