getopt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Getopt for Microsoft C
  2. This code is a modification of the Free Software Foundation, Inc.
  3. Getopt library for parsing command line argument the purpose was
  4. to provide a Microsoft Visual C friendly derivative. This code
  5. provides functionality for both Unicode and Multibyte builds.
  6. Date: 02/03/2011 - Ludvik Jerabek - Initial Release
  7. Version: 1.0
  8. Comment: Supports getopt, getopt_long, and getopt_long_only
  9. and POSIXLY_CORRECT environment flag
  10. License: LGPL
  11. Revisions:
  12. 02/03/2011 - Ludvik Jerabek - Initial Release
  13. 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
  14. 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
  15. 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
  16. 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
  17. 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
  18. 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi
  19. 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features
  20. 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable
  21. 24/10/2020 - Paul-Louis Ageneau - Removed Unicode version
  22. **DISCLAIMER**
  23. THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  24. EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
  25. IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  26. PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
  27. EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
  28. APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
  29. DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
  30. USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
  31. PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
  32. YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
  33. EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34. */
  35. #ifndef __GETOPT_H_
  36. #define __GETOPT_H_
  37. #ifdef _GETOPT_API
  38. #undef _GETOPT_API
  39. #endif
  40. #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT)
  41. #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually"
  42. #elif defined(STATIC_GETOPT)
  43. #pragma message("Warning static builds of getopt violate the Lesser GNU Public License")
  44. #define _GETOPT_API
  45. #elif defined(EXPORTS_GETOPT)
  46. #pragma message("Exporting getopt library")
  47. #define _GETOPT_API __declspec(dllexport)
  48. #else
  49. #pragma message("Importing getopt library")
  50. #define _GETOPT_API __declspec(dllimport)
  51. #endif
  52. // Change behavior for C\C++
  53. #ifdef __cplusplus
  54. #define _BEGIN_EXTERN_C extern "C" {
  55. #define _END_EXTERN_C }
  56. #define _GETOPT_THROW throw()
  57. #else
  58. #define _BEGIN_EXTERN_C
  59. #define _END_EXTERN_C
  60. #define _GETOPT_THROW
  61. #endif
  62. // Standard GNU options
  63. #define null_argument 0 /*Argument Null*/
  64. #define no_argument 0 /*Argument Switch Only*/
  65. #define required_argument 1 /*Argument Required*/
  66. #define optional_argument 2 /*Argument Optional*/
  67. // Shorter Options
  68. #define ARG_NULL 0 /*Argument Null*/
  69. #define ARG_NONE 0 /*Argument Switch Only*/
  70. #define ARG_REQ 1 /*Argument Required*/
  71. #define ARG_OPT 2 /*Argument Optional*/
  72. #include <string.h>
  73. _BEGIN_EXTERN_C
  74. extern _GETOPT_API int optind;
  75. extern _GETOPT_API int opterr;
  76. extern _GETOPT_API int optopt;
  77. struct option_a
  78. {
  79. const char* name;
  80. int has_arg;
  81. int *flag;
  82. int val;
  83. };
  84. extern _GETOPT_API char *optarg_a;
  85. extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW;
  86. extern _GETOPT_API int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW;
  87. extern _GETOPT_API int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW;
  88. _END_EXTERN_C
  89. #undef _BEGIN_EXTERN_C
  90. #undef _END_EXTERN_C
  91. #undef _GETOPT_THROW
  92. #undef _GETOPT_API
  93. #define getopt getopt_a
  94. #define getopt_long getopt_long_a
  95. #define getopt_long_only getopt_long_only_a
  96. #define option option_a
  97. #define optarg optarg_a
  98. #endif // __GETOPT_H_