arguments.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #pragma once
  38. #include <bin/function.h>
  39. namespace gearman_client
  40. {
  41. /**
  42. * Data structure for arguments and state.
  43. */
  44. class Args
  45. {
  46. public:
  47. Args(int p_argc, char *p_argv[]);
  48. ~Args();
  49. bool usage()
  50. {
  51. return _usage;
  52. }
  53. bool prefix() const
  54. {
  55. return _prefix;
  56. }
  57. bool background() const
  58. {
  59. return _background;
  60. }
  61. bool daemon() const
  62. {
  63. return _daemon;
  64. }
  65. gearman_job_priority_t priority() const
  66. {
  67. return _priority;
  68. }
  69. int timeout() const
  70. {
  71. return _timeout;
  72. }
  73. const char *unique() const
  74. {
  75. return _unique;
  76. }
  77. bool job_per_newline() const
  78. {
  79. return _job_per_newline;
  80. }
  81. Function::vector::iterator begin()
  82. {
  83. return _functions.begin();
  84. }
  85. Function::vector::iterator end()
  86. {
  87. return _functions.end();
  88. }
  89. bool strip_newline() const
  90. {
  91. return _strip_newline;
  92. }
  93. bool worker() const
  94. {
  95. return _worker;
  96. }
  97. const std::string &pid_file() const
  98. {
  99. return _pid_file;
  100. }
  101. int error() const
  102. {
  103. return _error;
  104. }
  105. void set_error() const
  106. {
  107. _error= 1;
  108. }
  109. uint32_t &count()
  110. {
  111. return _count;
  112. }
  113. const char *host() const
  114. {
  115. return _host;
  116. }
  117. in_port_t port() const
  118. {
  119. return _port;
  120. }
  121. bool arguments() const
  122. {
  123. if (argv[0])
  124. return true;
  125. return false;
  126. }
  127. bool verbose() const
  128. {
  129. return _verbose;
  130. }
  131. bool suppress_input() const
  132. {
  133. return _suppress_input;
  134. }
  135. bool use_ssl() const
  136. {
  137. return _use_ssl;
  138. }
  139. const char *argument(size_t arg)
  140. {
  141. return argv[arg];
  142. }
  143. char **argumentv()
  144. {
  145. return argv;
  146. }
  147. void reset_arg_error()
  148. {
  149. if (_arg_error)
  150. {
  151. free(_arg_error);
  152. _arg_error= NULL;
  153. }
  154. }
  155. bool is_error()
  156. {
  157. if (_functions.empty())
  158. {
  159. reset_arg_error();
  160. _arg_error= strdup("No Functions were provided");
  161. return true;
  162. }
  163. return _is_error;
  164. }
  165. const char* arg_error() const
  166. {
  167. return _arg_error;
  168. }
  169. bool is_valid() const
  170. {
  171. return _functions.size();
  172. }
  173. private:
  174. Function::vector _functions;
  175. char *_host;
  176. in_port_t _port;
  177. uint32_t _count;
  178. char *_unique;
  179. bool _job_per_newline;
  180. bool _strip_newline;
  181. bool _worker;
  182. bool _suppress_input;
  183. bool _verbose;
  184. bool _prefix;
  185. bool _background;
  186. bool _daemon;
  187. bool _usage;
  188. bool _is_error;
  189. bool _use_ssl;
  190. char *_arg_error;
  191. gearman_job_priority_t _priority;
  192. int _timeout;
  193. char **argv;
  194. mutable int _error;
  195. std::string _pid_file;
  196. void init(int argc);
  197. void add(const char *name)
  198. {
  199. _functions.push_back(Function(name));
  200. }
  201. };
  202. } // namespace gearman_client