You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainclass.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "mainclass.hh"
  2. #include <iostream>
  3. #include <libgen.h>
  4. #include <sysexits.h>
  5. #include <climits>
  6. #include <getopt.h>
  7. MainClass::MainClass(int argc, char* argv[])
  8. : argc_(argc)
  9. , argv_(argv)
  10. {
  11. }
  12. int MainClass::usage()
  13. {
  14. std::cerr << "Usage: " << basename(argv_[0]) << std::endl <<
  15. " [--device DEVICE|--device=DEVICE|-e DEVICE]" << std::endl <<
  16. " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
  17. " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
  18. " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
  19. " [--help|-h]" << std::endl;
  20. std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
  21. << std::endl << std::endl;
  22. std::cerr << "--device \tSwitch to the provided device."
  23. << std::endl << "--up \t\tEnable the provided ouput pin."
  24. << std::endl << "--down \t\tDisable the provided output pin."
  25. << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
  26. << std::endl << "--help \t\tDisplay this message."
  27. << std::endl << std::endl;
  28. std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
  29. << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
  30. << UINT_MAX << "."
  31. << std::endl << "DEVICE \t: The device number. Default is 0. "
  32. "0 <= DEVICE <= 4." << std::endl;
  33. return EX_USAGE;
  34. }
  35. bool MainClass::bad_value_()
  36. {
  37. extern int optind;
  38. extern char* optarg;
  39. std::cerr << "Bad value " << optarg <<
  40. " for option " << argv_[optind - 2] << std::endl << std::endl;
  41. return false;
  42. }
  43. bool MainClass::build_actions_()
  44. {
  45. option opts[] = {
  46. {"device", 1, 0, 'e'},
  47. {"up", 1, 0, 'u'},
  48. {"down", 1, 0, 'd'},
  49. {"sleep", 1, 0, 's'},
  50. {"help", 0, 0, 'h'},
  51. {0, 0, 0, 0}
  52. };
  53. int opt;
  54. extern int optind;
  55. extern char* optarg;
  56. char* endptr;
  57. while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
  58. {
  59. if (!optarg)
  60. return false;
  61. long int arg = strtol(optarg, &endptr, 10);
  62. if (arg < 0)
  63. return bad_value_();
  64. if (opt == 'e')
  65. {
  66. if (arg > 4)
  67. return bad_value_();
  68. actions_.push_back([arg]() -> bool
  69. {
  70. return true;
  71. });
  72. }
  73. else if (opt == 'u')
  74. {
  75. if (arg > 7)
  76. return bad_value_();
  77. }
  78. else if (opt == 'd')
  79. {
  80. if (arg > 7)
  81. return bad_value_();
  82. }
  83. else if (opt == 's')
  84. {
  85. if (arg > UINT_MAX)
  86. return bad_value_();
  87. }
  88. else
  89. return false;
  90. }
  91. return optind == argc_;
  92. }
  93. int MainClass::execute()
  94. {
  95. if (argc_ <= 2 || !build_actions_())
  96. return usage();
  97. for (auto f : actions_)
  98. if (!f())
  99. return 1;
  100. return 0;
  101. }