cmdline.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * C Reusables
  3. * <http://github.com/mity/c-reusables>
  4. *
  5. * Copyright (c) 2017 Martin Mitas
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. * IN THE SOFTWARE.
  24. */
  25. #ifndef CRE_CMDLINE_H
  26. #define CRE_CMDLINE_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* The option may have an argument. (Affects only long option.) */
  31. #define CMDLINE_OPTFLAG_OPTIONALARG 0x0001
  32. /* The option must have an argument.
  33. * Such short option cannot be grouped within single '-abc'. */
  34. #define CMDLINE_OPTFLAG_REQUIREDARG 0x0002
  35. /* Enable special compiler-like mode for the long option.
  36. *
  37. * Note ::shortname is not supported with this flag. CMDLINE_OPTION::shortname
  38. * is silently ignored if the flag is used.
  39. *
  40. * With this flag, CMDLINE_OPTION::longname is treated differently as follows:
  41. *
  42. * 1. The option matches if the CMDLINE_OPTION::longname is the exact prefix
  43. * of the argv[i] from commandline.
  44. *
  45. * 2. Double dash ("--") is not automatically prepended to
  46. * CMDLINE_OPTION::longname. (If you desire any leading dash, include it
  47. * explicitly in CMDLINE_OPTION initialization.)
  48. *
  49. * 3. An argument (optionally after a whitespace) is required (the flag
  50. * CMDLINE_OPTFLAG_COMPILERLIKE implicitly implies also the flag
  51. * CMDLINE_OPTFLAG_REQUIREDARG).
  52. *
  53. * But there is no delimiter expected (no "=" between the option and its
  54. * argument). Whitespace is optional between the option and its argument.
  55. *
  56. * Intended use is for options similar to what many compilers accept.
  57. * For example:
  58. * -DDEBUG=0 (-D is the option, DEBUG=0 is the argument).
  59. * -Isrc/include (-I is the option, src/include is the argument).
  60. * -isystem /usr/include (-isystem is the option, /usr/include is the argument).
  61. * -lmath (-l is the option, math is the argument).
  62. */
  63. #define CMDLINE_OPTFLAG_COMPILERLIKE 0x0004
  64. /* Special (reserved) option IDs. Do not use these for any CMDLINE_OPTION::id.
  65. * See documentation of cmdline_read() to get info about their meaning.
  66. */
  67. #define CMDLINE_OPTID_NONE 0
  68. #define CMDLINE_OPTID_UNKNOWN (-0x7fffffff + 0)
  69. #define CMDLINE_OPTID_MISSINGARG (-0x7fffffff + 1)
  70. #define CMDLINE_OPTID_BOGUSARG (-0x7fffffff + 2)
  71. typedef struct CMDLINE_OPTION {
  72. char shortname; /* Short (single char) option or 0. */
  73. const char* longname; /* Long name (after "--") or NULL. */
  74. int id; /* Non-zero ID to identify the option in the callback; or zero to denote end of options list. */
  75. unsigned flags; /* Bitmask of CMDLINE_OPTFLAG_xxxx flags. */
  76. } CMDLINE_OPTION;
  77. /* Parses all options and their arguments as specified by argc, argv accordingly
  78. * with the given options (except argv[0] which is ignored).
  79. *
  80. * The caller must specify the list of supported options in the 1st parameter
  81. * of the function. The array must end with a record whose CMDLINE_OPTION::id
  82. * is zero to zero.
  83. *
  84. * The provided callback function is called for each option on the command
  85. * line so that:
  86. *
  87. * -- the "id" refers to the id of the option as specified in options[].
  88. *
  89. * -- the "arg" specifies an argument of the option or NULL if none is
  90. * provided.
  91. *
  92. * -- the "userdata" just allows to pass in some caller's context into
  93. * the callback.
  94. *
  95. * Special cases (recognized via special "id" value) are reported to the
  96. * callback as follows:
  97. *
  98. * -- If id is CMDLINE_OPTID_NONE, the callback informs about a non-option
  99. * also known as a positional argument.
  100. *
  101. * All argv[] tokens which are not interpreted as an options or an argument
  102. * of any option fall into this category.
  103. *
  104. * Usually, programs interpret these as paths to file to process.
  105. *
  106. * -- If id is CMDLINE_OPTID_UNKNOWN, the corresponding argv[] looks like an
  107. * option but it is not found in the options[] passed to cmdline_read().
  108. *
  109. * The callback's parameter arg specifies the guilty command line token.
  110. * Usually, program writes down an error message and exits.
  111. *
  112. * -- If id is CMDLINE_OPTID_MISSINGARG, the given option is valid but its
  113. * flag in options[] requires an argument; yet there is none on the
  114. * command line.
  115. *
  116. * The callback's parameter arg specifies the guilty option name.
  117. * Usually, program writes down an error message and exits.
  118. *
  119. * -- If id is CMDLINE_OPTID_BOGUSARG, the given option is valid but its
  120. * flag in options[] does not expect an argument; yet the command line
  121. * does provide one.
  122. *
  123. * The callback's parameter arg specifies the guilty option name.
  124. * Usually, program writes down an error message and exits.
  125. *
  126. * On success, zero is returned.
  127. *
  128. * If the callback returns a non-zero, cmdline_read() aborts immediately and
  129. * cmdline_read() propagates the same return value to the caller.
  130. */
  131. int cmdline_read(const CMDLINE_OPTION* options, int argc, char** argv,
  132. int (*callback)(int /*id*/, const char* /*arg*/, void* /*userdata*/),
  133. void* userdata);
  134. #ifdef __cplusplus
  135. } /* extern "C" { */
  136. #endif
  137. #endif /* CRE_CMDLINE_H */