arguments.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. const char *argument(size_t arg)
  136. {
  137. return argv[arg];
  138. }
  139. char **argumentv()
  140. {
  141. return argv;
  142. }
  143. bool is_error()
  144. {
  145. if (_functions.empty())
  146. {
  147. _arg_error= strdup("No Functions were provided");
  148. return true;
  149. }
  150. return _is_error;
  151. }
  152. const char* arg_error() const
  153. {
  154. return _arg_error;
  155. }
  156. bool is_valid() const
  157. {
  158. return _functions.size();
  159. }
  160. private:
  161. Function::vector _functions;
  162. char *_host;
  163. in_port_t _port;
  164. uint32_t _count;
  165. char *_unique;
  166. bool _job_per_newline;
  167. bool _strip_newline;
  168. bool _worker;
  169. bool _suppress_input;
  170. bool _verbose;
  171. bool _prefix;
  172. bool _background;
  173. bool _daemon;
  174. bool _usage;
  175. bool _is_error;
  176. char *_arg_error;
  177. gearman_job_priority_t _priority;
  178. int _timeout;
  179. char **argv;
  180. mutable int _error;
  181. std::string _pid_file;
  182. void init(int argc);
  183. void add(const char *name)
  184. {
  185. _functions.push_back(Function(name));
  186. }
  187. };
  188. } // namespace gearman_client