cargs.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef _CARGS_H
  2. #define _CARGS_H
  3. #include <stddef.h>
  4. typedef int __cargs_int;
  5. typedef int __cargs_bool;
  6. typedef double __cargs_decimal;
  7. extern const __cargs_bool __cargs_true;
  8. extern const __cargs_bool __cargs_false;
  9. struct _IO_FILE;
  10. typedef struct _IO_FILE FILE;
  11. typedef enum OptionType
  12. {
  13. String = 1 << 0,
  14. Integer = 1 << 1,
  15. Decimal = 1 << 2,
  16. Boolean = 1 << 3,
  17. }
  18. OptionType;
  19. typedef struct OptionData
  20. {
  21. OptionType type;
  22. char shortOption;
  23. char* longOption;
  24. char* description;
  25. void* sourcePtr;
  26. }
  27. OptionData;
  28. typedef struct ParseContext
  29. {
  30. OptionData* items;
  31. size_t itemsCount;
  32. }
  33. ParseContext;
  34. typedef enum ParseErrorCategory
  35. {
  36. OptionDoesntExist,
  37. NonBooleanOptionDoesntHaveArgument,
  38. CombinedOptionsShouldBeBoolean,
  39. IntegerParsingError,
  40. DecimalParsingError,
  41. }
  42. ParseErrorCategory;
  43. typedef struct ParseError
  44. {
  45. ParseErrorCategory category;
  46. size_t argIndex;
  47. char optionShortName;
  48. }
  49. ParseError;
  50. typedef struct ParseResult
  51. {
  52. char* programName;
  53. size_t* stringOptions;
  54. size_t stringOptionsCount;
  55. char** stringValues;
  56. size_t* integerOptions;
  57. size_t integerOptionsCount;
  58. __cargs_int* integerValues;
  59. size_t* decimalOptions;
  60. size_t decimalOptionsCount;
  61. __cargs_decimal* decimalValues;
  62. size_t* booleanOptions;
  63. size_t booleanOptionsCount;
  64. char** freeValues;
  65. size_t freeValuesCount;
  66. ParseError* errors;
  67. size_t errorsCount;
  68. }
  69. ParseResult;
  70. ///
  71. /// Creates a boolean option.
  72. /// This option does not require a value in next arg.
  73. /// It is also possible to combine several boolean options in short form in one
  74. /// argument (-abc instead of -a -b -c).
  75. ///
  76. /// @param shortOption short name of the option (-o)
  77. /// @param longOption long name of the option (--option)
  78. /// @param source pointer to the boolean variable for initialization (NULL allowed)
  79. /// @param description description of the option
  80. ///
  81. const OptionData booleanArg(
  82. const char shortOption,
  83. char* longOption,
  84. __cargs_bool* source,
  85. char* description);
  86. ///
  87. /// Creates an integer option.
  88. /// Next argument will be validated: integer number is required.
  89. ///
  90. /// @param shortOption short name of the option (-o)
  91. /// @param longOption long name of the option (--option)
  92. /// @param source pointer to the integer variable for initialization (NULL allowed)
  93. /// @param description description of the option
  94. ///
  95. const OptionData integerArg(
  96. const char shortOption,
  97. char* longOption,
  98. __cargs_int* source,
  99. char* description);
  100. ///
  101. /// Creates a decimal option.
  102. /// Next argument will be validated: decimal number is required.
  103. ///
  104. /// @param shortOption short name of the option (-o)
  105. /// @param longOption long name of the option (--option)
  106. /// @param source pointer to the decimal variable for initialization (NULL allowed)
  107. /// @param description description of the option
  108. ///
  109. const OptionData decimalArg(
  110. const char shortOption,
  111. char* longOption,
  112. __cargs_decimal* source,
  113. char* description);
  114. ///
  115. /// Creates a string option.
  116. ///
  117. /// @param shortOption short name of the option (-o)
  118. /// @param longOption long name of the option (--option)
  119. /// @param source pointer to the char* variable for initialization (NULL allowed)
  120. /// @param description description of the option
  121. ///
  122. const OptionData stringArg(
  123. const char shortOption,
  124. char* longOption,
  125. char** source,
  126. char* description);
  127. ///
  128. /// Parses the arguments.
  129. ///
  130. /// @param context object with information about options
  131. /// @param argc an amount of arguments
  132. /// @param argv array of arguments
  133. ///
  134. ParseResult* parseArgs(ParseContext* context, const int argc, char* argv[]);
  135. ///
  136. /// Frees the memory from ParseResult object.
  137. ///
  138. /// @param result ParseResult object to free
  139. ///
  140. void freeResult(ParseResult* result);
  141. ///
  142. /// Function outputs an information about errors into a file
  143. ///
  144. /// @param file file to output errors data
  145. /// @param context object with information about options
  146. /// @param result result of parsing
  147. /// @param argc an amount of arguments
  148. /// @param argv array of arguments
  149. ///
  150. /// @return true - errors exist, false - no errors
  151. ///
  152. const __cargs_bool fprintErrors(
  153. FILE* file,
  154. ParseContext* context,
  155. ParseResult* result,
  156. const int argc,
  157. char* argv[]);
  158. ///
  159. /// Function outputs an information about errors into stderr
  160. ///
  161. /// @param context object with information about options
  162. /// @param result result of parsing
  163. /// @param argc an amount of arguments
  164. /// @param argv array of arguments
  165. ///
  166. /// @return true - errors exist, false - no errors
  167. ///
  168. const __cargs_bool printErrors(
  169. ParseContext* context,
  170. ParseResult* result,
  171. const int argc,
  172. char* argv[]);
  173. ///
  174. /// Function outputs information about arguments into a file
  175. ///
  176. /// @param file file to output the arguments data
  177. /// @param context object with information about options
  178. ///
  179. void fprintHelp(FILE* file, ParseContext* context);
  180. ///
  181. /// Function outputs information about arguments into stdout
  182. ///
  183. /// @param context object with information about options
  184. ///
  185. void printHelp(ParseContext* context);
  186. #endif // _CARGS_H