1
0

getopt.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. getopt.h
  8. Abstract:
  9. This header contains definitions for the non-standard getopt functions that
  10. parse command line options and support long arguments.
  11. Author:
  12. Evan Green 21-Aug-2013
  13. --*/
  14. #ifndef _GETOPT_H
  15. #define _GETOPT_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <libcbase.h>
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. //
  27. // Definitions for the has_arg field of the option structure.
  28. //
  29. #define no_argument 0
  30. #define required_argument 1
  31. #define optional_argument 2
  32. //
  33. // ------------------------------------------------------ Data Type Definitions
  34. //
  35. /*++
  36. Structure Description:
  37. This structure defines the option structure used to define a single long
  38. command line option.
  39. Members:
  40. name - Stores a pointer to the null terminated string containing the name
  41. of the long option.
  42. has_arg - Stores a flag. Valid values are no_argument if the long option
  43. does not take an argument, required_argument if the long option must
  44. take an argument, or optional_argument if the long option can either
  45. take or not take an argument.
  46. flag - Stores a pointer where a value should be set. If this is NULL,
  47. then getopt_long returns the value member. Otherwise, getopt_long
  48. returns 0, and this member points to a variable of type int which is
  49. set to the value member if the option is found (and left unchanged if
  50. the option is not encountered).
  51. val - Stores the value to either return or set in the flag pointer.
  52. --*/
  53. struct option {
  54. const char *name;
  55. int has_arg;
  56. int *flag;
  57. int val;
  58. };
  59. //
  60. // -------------------------------------------------------------------- Globals
  61. //
  62. //
  63. // Define the global that points to the argument if the getopt function finds
  64. // an option that takes an argument.
  65. //
  66. LIBC_API extern char *optarg;
  67. //
  68. // Define the global that contains the index of the next argument to be
  69. // processed by the getopt function.
  70. //
  71. LIBC_API extern int optind;
  72. //
  73. // Define the global that controls whether or not an error message is printed
  74. // to standrad error when the getopt function detects an error. The user can
  75. // set this to 0 to disable such messages.
  76. //
  77. LIBC_API extern int opterr;
  78. //
  79. // Define the global that is set to the unknown option if an option is passed
  80. // in the arguments that is not in the options string during a call to getopt.
  81. //
  82. LIBC_API extern int optopt;
  83. //
  84. // Define the global that can be used to reset the option system so that it
  85. // can be called with a different array or called repeatedly on the same array.
  86. // Setting optind to zero has the same effect as setting optreset to non-zero.
  87. //
  88. LIBC_API extern int optreset;
  89. //
  90. // -------------------------------------------------------- Function Prototypes
  91. //
  92. LIBC_API
  93. int
  94. getopt_long (
  95. int ArgumentCount,
  96. char *const Arguments[],
  97. const char *ShortOptions,
  98. const struct option *LongOptions,
  99. int *LongIndex
  100. );
  101. /*++
  102. Routine Description:
  103. This routine works just like the getopt function (see that for details),
  104. except it allow allows long options of the form --option=argument or
  105. --option argument.
  106. Arguments:
  107. ArgumentCount - Supplies the argument count from main.
  108. Arguments - Supplies the argument array from main.
  109. ShortOptions - Supplies the short option string. This parameter works the
  110. same way as the Options string of getopt.
  111. LongOptions - Supplies a pointer to an array of long options. The array
  112. must be terminated with a NULLed out option structure. Long option
  113. names can be abbreviated in the argument list provided that the
  114. abbreviation is unique.
  115. LongIndex - Supplies an optional pointer that returns the index into the
  116. long options array of the long option that matched.
  117. Return Value:
  118. Returns the same set of values as the getopt function. If a long option
  119. masked, then either 0 or the value set inside the long option is returned
  120. depending on the flag member of the long option.
  121. --*/
  122. LIBC_API
  123. int
  124. getopt_long_only (
  125. int ArgumentCount,
  126. char *const Arguments[],
  127. const char *ShortOptions,
  128. const struct option *LongOptions,
  129. int *LongIndex
  130. );
  131. /*++
  132. Routine Description:
  133. This routine works just like the getopt_long function except it allows
  134. long arguments to have only one dash at the beginning instead of two
  135. (ie -option instead of --option). If an argument does not match for long
  136. options of either --option or -option, the short options will be tried.
  137. Arguments:
  138. ArgumentCount - Supplies the argument count from main.
  139. Arguments - Supplies the argument array from main.
  140. ShortOptions - Supplies the short option string. This parameter works the
  141. same way as the Options string of getopt.
  142. LongOptions - Supplies a pointer to an array of long options. The array
  143. must be terminated with a NULLed out option structure. Long option
  144. names can be abbreviated in the argument list provided that the
  145. abbreviation is unique.
  146. LongIndex - Supplies an optional pointer that returns the index into the
  147. long options array of the long option that matched.
  148. Return Value:
  149. Returns the same set of values as the getopt function. If a long option
  150. masked, then either 0 or the value set inside the long option is returned
  151. depending on the flag member of the long option.
  152. --*/
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif