Args.cpp 5.0 KB

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