args.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #pragma once
  24. #include "assert.h"
  25. #include "types.h"
  26. #include "string_utils.h"
  27. namespace crown
  28. {
  29. enum ArgsOptionType
  30. {
  31. AOT_SHORT,
  32. AOT_LONG,
  33. AOT_NOT_OPTION
  34. };
  35. enum ArgsOptionArgument
  36. {
  37. AOA_NO_ARGUMENT,
  38. AOA_REQUIRED_ARGUMENT
  39. };
  40. struct ArgsOption
  41. {
  42. const char* name;
  43. int32_t has_arg;
  44. int32_t* flag;
  45. int32_t val;
  46. };
  47. /// Parses the command line arguments in a way very similar to GNU getopt.
  48. class Args
  49. {
  50. public:
  51. Args(int argc, char** argv, const char* shortopts, const ArgsOption* longopts)
  52. : m_argc(argc)
  53. , m_argv(argv)
  54. , m_shortopts(shortopts)
  55. , m_longopts(longopts)
  56. , m_optind(1) // Do not consider argv[0]
  57. , m_scope(argc)
  58. , m_optarg(NULL)
  59. {
  60. CE_ASSERT(argv != NULL, "Argument vector must be != NULL");
  61. CE_ASSERT(shortopts != NULL, "Short argument list must be != NULL");
  62. // longopts could be NULL
  63. }
  64. /// Finds the next option character and returns it.
  65. /// If there are no more option characters, it returns -1 and optind()
  66. /// returns the index in argv[] of the first argv-element that is not
  67. /// an option.
  68. /// If it finds an option that was not included in shortopts or longopts,
  69. /// or if it finds a missing option argument, it returns '?' character.
  70. int32_t getopt()
  71. {
  72. // Always reset optarg
  73. m_optarg = NULL;
  74. // End of arguments
  75. if (m_optind >= m_scope)
  76. {
  77. return -1;
  78. }
  79. switch (option_type(m_argv[m_optind]))
  80. {
  81. case AOT_SHORT:
  82. {
  83. return short_option(m_argv[m_optind]);
  84. }
  85. case AOT_LONG:
  86. {
  87. return long_option(m_argv[m_optind]);
  88. }
  89. case AOT_NOT_OPTION:
  90. {
  91. not_option();
  92. return getopt();
  93. }
  94. default:
  95. {
  96. return '?';
  97. }
  98. }
  99. }
  100. /// Returns the index of the next argument to be processed.
  101. int32_t optind() const
  102. {
  103. return m_optind;
  104. }
  105. /// Returns the text of the following argv-element in respect
  106. /// to the current optind().
  107. const char* optarg() const
  108. {
  109. return m_optarg;
  110. }
  111. /// Sets the @a index into argv[] from where to start option scanning.
  112. /// If @a index >= argc nothing will be scanned.
  113. void set_optind(int32_t index)
  114. {
  115. m_optind = index;
  116. }
  117. private:
  118. // Returns the @a option type
  119. // Returns AOT_SHORT if option is of the form "-x" where 'x' is the option.
  120. // Returns AOT_LONG if option is of the form "--option" where "option" is the option.
  121. // Returns AOT_NOT_OPTION in all other cases.
  122. ArgsOptionType option_type(const char* option)
  123. {
  124. const size_t option_len = string::strlen(option);
  125. if (option_len == 2 && option[0] == '-' && option[1] != '-')
  126. {
  127. return AOT_SHORT;
  128. }
  129. else if (option_len > 2 && option[0] == '-' && option[1] == '-')
  130. {
  131. return AOT_LONG;
  132. }
  133. return AOT_NOT_OPTION;
  134. }
  135. // Parses a long option
  136. int32_t long_option(const char* option)
  137. {
  138. const ArgsOption* current_option = m_longopts;
  139. // Loop through all the long options
  140. while (!end_of_longopts(current_option))
  141. {
  142. if (string::strcmp(current_option->name, &option[2]) == 0)
  143. {
  144. // If the option requires an argument
  145. if (current_option->has_arg == AOA_REQUIRED_ARGUMENT)
  146. {
  147. // Read the argument if it exists
  148. if ((m_optind + 1) < m_scope)
  149. {
  150. // Read the argument and skip the following parameter
  151. m_optarg = m_argv[m_optind + 1];
  152. m_optind += 2;
  153. }
  154. else
  155. {
  156. // CE_LOGE("%s: option requires an argument -- '%s'", m_argv[0], current_option->name);
  157. // Missing option
  158. m_optind += 1;
  159. return '?';
  160. }
  161. }
  162. // If the option does not require an argument
  163. else
  164. {
  165. m_optind++;
  166. }
  167. if (current_option->flag == NULL)
  168. {
  169. return current_option->val;
  170. }
  171. else
  172. {
  173. (*current_option->flag) = current_option->val;
  174. return 0;
  175. }
  176. }
  177. current_option++;
  178. }
  179. // Found a long option but was not included in longopts
  180. // CE_LOGE("%s: invalid option -- '%s'", m_argv[0], &option[2]);
  181. m_optind++;
  182. return '?';
  183. }
  184. // Parses a short option
  185. int32_t short_option(const char* option)
  186. {
  187. (void)option;
  188. // CE_LOGE("%s: invalid option -- '%s'", m_argv[0], &option[1]);
  189. m_optind++;
  190. return '?';
  191. }
  192. void not_option()
  193. {
  194. char* current_option = m_argv[m_optind];
  195. for (int32_t i = m_optind; i < (m_argc - 1); i++)
  196. {
  197. m_argv[i] = m_argv[i + 1];
  198. }
  199. m_argv[m_argc - 1] = current_option;
  200. // Reduce the number of true arguments
  201. m_scope--;
  202. }
  203. // Returns whether the given option is the last one
  204. bool end_of_longopts(const ArgsOption* option) const
  205. {
  206. return (option->name == NULL && option->has_arg == 0 && option->flag == NULL && option->val == 0);
  207. }
  208. private:
  209. int m_argc;
  210. char** m_argv;
  211. const char* m_shortopts;
  212. const ArgsOption* m_longopts;
  213. // Index of the next argument to be processed
  214. int32_t m_optind;
  215. // Number of "true" arguments
  216. int32_t m_scope;
  217. // The text of the following argv-element to argv[optind]
  218. char* m_optarg;
  219. };
  220. } // namespace crown