arguments.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "config.h"
  38. #include <cstdlib>
  39. #include <cstring>
  40. #include <string>
  41. #include <vector>
  42. #include <iostream>
  43. #include <unistd.h>
  44. #ifdef HAVE_GETOPT_H
  45. #include <getopt.h>
  46. #endif
  47. #include <libgearman/gearman.h>
  48. #include "arguments.h"
  49. namespace gearman_client
  50. {
  51. Args::Args(int p_argc, char *p_argv[]) :
  52. _host(NULL),
  53. _port(0),
  54. _count(0),
  55. _unique(NULL),
  56. _job_per_newline(false),
  57. _strip_newline(false),
  58. _worker(false),
  59. _suppress_input(false),
  60. _prefix(false),
  61. _background(false),
  62. _daemon(false),
  63. _usage(false),
  64. _is_error(false),
  65. _priority(GEARMAN_JOB_PRIORITY_NORMAL),
  66. _timeout(-1),
  67. argv(p_argv),
  68. _error(0)
  69. {
  70. init(p_argc);
  71. }
  72. Args::~Args()
  73. {
  74. }
  75. void Args::init(int argc)
  76. {
  77. int c;
  78. opterr= 0;
  79. while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:wi:d")) != -1)
  80. {
  81. switch(c)
  82. {
  83. case 'b':
  84. _background= true;
  85. break;
  86. case 'c':
  87. _count= static_cast<uint32_t>(atoi(optarg));
  88. break;
  89. case 'd':
  90. _daemon= true;
  91. break;
  92. case 'f':
  93. add(optarg);
  94. break;
  95. case 'h':
  96. _host= optarg;
  97. break;
  98. case 'i':
  99. _pid_file= optarg;
  100. break;
  101. case 'I':
  102. _priority= GEARMAN_JOB_PRIORITY_HIGH;
  103. break;
  104. case 'L':
  105. _priority= GEARMAN_JOB_PRIORITY_LOW;
  106. break;
  107. case 'n':
  108. _job_per_newline= true;
  109. break;
  110. case 'N':
  111. _job_per_newline= true;
  112. _strip_newline= true;
  113. break;
  114. case 'p':
  115. _port= static_cast<in_port_t>(atoi(optarg));
  116. break;
  117. case 'P':
  118. _prefix= true;
  119. break;
  120. case 's':
  121. _suppress_input= true;
  122. break;
  123. case 't':
  124. _timeout= atoi(optarg);
  125. break;
  126. case 'u':
  127. _unique= optarg;
  128. break;
  129. case 'w':
  130. _worker= true;
  131. break;
  132. case 'H':
  133. _usage= true;
  134. return;
  135. default:
  136. _is_error= true;
  137. return;
  138. }
  139. }
  140. argv+= optind;
  141. }
  142. } // namespace gearman_client