Args.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "StringUtils.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 CE_EXPORT Args
  49. {
  50. public:
  51. Args(int argc, char** argv, const char* shortopts, const ArgsOption* longopts);
  52. ~Args();
  53. /// Finds the next option character and returns it.
  54. /// If there are no more option characters, it returns -1 and optind()
  55. /// returns the index in argv[] of the first argv-element that is not
  56. /// an option.
  57. /// If it finds an option that was not included in shortopts or longopts,
  58. /// or if it finds a missing option argument, it returns '?' character.
  59. int32_t getopt();
  60. /// Returns the index of the next argument to be processed.
  61. int32_t optind() const;
  62. /// Returns the text of the following argv-element in respect
  63. /// to the current optind().
  64. const char* optarg() const;
  65. /// Sets the @a index into argv[] from where to start option scanning.
  66. /// If @a index >= argc nothing will be scanned.
  67. void set_optind(int32_t index);
  68. private:
  69. // Returns the @a option type
  70. // Returns AOT_SHORT if option is of the form "-x" where 'x' is the option.
  71. // Returns AOT_LONG if option is of the form "--option" where "option" is the option.
  72. // Returns AOT_NOT_OPTION in all other cases.
  73. ArgsOptionType option_type(const char* option);
  74. // Parses a long option
  75. int32_t long_option(const char* option);
  76. // Parses a short option
  77. int32_t short_option(const char* option);
  78. void not_option();
  79. // Returns whether the given option is the last one
  80. bool end_of_longopts(const ArgsOption* option) const;
  81. private:
  82. int m_argc;
  83. char** m_argv;
  84. const char* m_shortopts;
  85. const ArgsOption* m_longopts;
  86. // Index of the next argument to be processed
  87. int32_t m_optind;
  88. // Number of "true" arguments
  89. int32_t m_scope;
  90. // The text of the following argv-element to argv[optind]
  91. char* m_optarg;
  92. };
  93. } // namespace crown