Args.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Args.h"
  23. #include "Log.h"
  24. namespace crown
  25. {
  26. //-----------------------------------------------------------------------------
  27. Args::Args(int argc, char** argv, const char* shortopts, const ArgsOption* longopts) :
  28. m_argc(argc),
  29. m_argv(argv),
  30. m_shortopts(shortopts),
  31. m_longopts(longopts),
  32. m_optind(1), // Do not consider argv[0]
  33. m_scope(argc),
  34. m_optarg(NULL)
  35. {
  36. assert(argv != NULL);
  37. assert(shortopts != NULL);
  38. // longopts could be NULL
  39. }
  40. //-----------------------------------------------------------------------------
  41. Args::~Args()
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. int32_t Args::getopt()
  46. {
  47. // Always reset optarg
  48. m_optarg = NULL;
  49. // End of arguments
  50. if (m_optind >= m_scope)
  51. {
  52. return -1;
  53. }
  54. switch (option_type(m_argv[m_optind]))
  55. {
  56. case AOT_SHORT:
  57. {
  58. return short_option(m_argv[m_optind]);
  59. }
  60. case AOT_LONG:
  61. {
  62. return long_option(m_argv[m_optind]);
  63. }
  64. case AOT_NOT_OPTION:
  65. {
  66. not_option();
  67. return getopt();
  68. }
  69. default:
  70. {
  71. return '?';
  72. }
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. int32_t Args::optind() const
  77. {
  78. return m_optind;
  79. }
  80. //-----------------------------------------------------------------------------
  81. const char* Args::optarg() const
  82. {
  83. return m_optarg;
  84. }
  85. //-----------------------------------------------------------------------------
  86. void Args::set_optind(int32_t index)
  87. {
  88. m_optind = index;
  89. }
  90. //-----------------------------------------------------------------------------
  91. ArgsOptionType Args::option_type(const char* option)
  92. {
  93. const size_t option_len = string::strlen(option);
  94. if (option_len == 2 && option[0] == '-' && option[1] != '-')
  95. {
  96. return AOT_SHORT;
  97. }
  98. else if (option_len > 2 && option[0] == '-' && option[1] == '-')
  99. {
  100. return AOT_LONG;
  101. }
  102. return AOT_NOT_OPTION;
  103. }
  104. //-----------------------------------------------------------------------------
  105. int32_t Args::long_option(const char* option)
  106. {
  107. // Long argument detected, but it is not so.
  108. assert(string::strlen(option) > 2);
  109. const ArgsOption* current_option = m_longopts;
  110. // Loop through all the long options
  111. while (!end_of_longopts(current_option))
  112. {
  113. if (string::strcmp(current_option->name, &option[2]) == 0)
  114. {
  115. // If the option requires an argument
  116. if (current_option->has_arg == AOA_REQUIRED_ARGUMENT)
  117. {
  118. // Read the argument if it exists
  119. if ((m_optind + 1) < m_scope)
  120. {
  121. // Read the argument and skip the following parameter
  122. m_optarg = m_argv[m_optind + 1];
  123. m_optind += 2;
  124. }
  125. else
  126. {
  127. Log::e("%s: option requires an argument -- '%s'", m_argv[0], current_option->name);
  128. // Missing option
  129. m_optind += 1;
  130. return '?';
  131. }
  132. }
  133. // If the option does not require an argument
  134. else
  135. {
  136. m_optind++;
  137. }
  138. if (current_option->flag == NULL)
  139. {
  140. return current_option->val;
  141. }
  142. else
  143. {
  144. (*current_option->flag) = current_option->val;
  145. return 0;
  146. }
  147. }
  148. current_option++;
  149. }
  150. // Found a long option but was not included in longopts
  151. Log::e("%s: invalid option -- '%s'", m_argv[0], &option[2]);
  152. m_optind++;
  153. return '?';
  154. }
  155. //-----------------------------------------------------------------------------
  156. int32_t Args::short_option(const char* option)
  157. {
  158. (void)option;
  159. Log::e("%s: invalid option -- '%s'", m_argv[0], &option[1]);
  160. m_optind++;
  161. return '?';
  162. }
  163. //-----------------------------------------------------------------------------
  164. void Args::not_option()
  165. {
  166. char* current_option = m_argv[m_optind];
  167. for (int32_t i = m_optind; i < (m_argc - 1); i++)
  168. {
  169. m_argv[i] = m_argv[i + 1];
  170. }
  171. m_argv[m_argc - 1] = current_option;
  172. // Reduce the number of true arguments
  173. m_scope--;
  174. }
  175. //-----------------------------------------------------------------------------
  176. bool Args::end_of_longopts(const ArgsOption* option) const
  177. {
  178. return (option->name == NULL && option->has_arg == 0 && option->flag == NULL && option->val == 0);
  179. }
  180. } // namespace crown