argparse.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2008-2024 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __PJ_ARGPARSE_H__
  19. #define __PJ_ARGPARSE_H__
  20. /**
  21. * @file argparse.h
  22. * @brief Command line argument parser
  23. */
  24. #include <pj/ctype.h>
  25. #include <pj/errno.h>
  26. #include <pj/string.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * Define function to display parsing error.
  30. */
  31. #ifndef PJ_ARGPARSE_ERROR
  32. # include <stdio.h>
  33. # define PJ_ARGPARSE_ERROR(fmt, arg) printf(fmt "\n", arg)
  34. #endif
  35. /**
  36. * @defgroup PJ_ARGPARSE Command line argument parser
  37. * @ingroup PJ_MISC
  38. * @{
  39. *
  40. * This module provides header only utilities to parse command line arguments.
  41. * This is mostly used by PJSIP test and sample apps. Note that there is
  42. * getopt() implementation in PJLIB-UTIL (but it's in PJLIB-UTIL, so it can't
  43. * be used by PJLIB).
  44. *
  45. * Limitations:
  46. * - the utility only supports white space(s) as separator between an option
  47. * and its value. Equal sign is not supported.
  48. * - the utility does not support double dash (--) as separator between options
  49. * and operands. It will keep treating arguments after -- as possible
  50. * options.
  51. */
  52. /**
  53. * Peek the next possible option from argv. An argument is considered an
  54. * option if it starts with "-".
  55. *
  56. * @param argv The argv, which must be null terminated.
  57. *
  58. * @return next option or NULL.
  59. */
  60. PJ_INLINE(char*) pj_argparse_peek_next_option(char *const argv[])
  61. {
  62. while (*argv) {
  63. const char *arg = *argv;
  64. if (*arg=='-') {
  65. return *argv;
  66. }
  67. ++argv;
  68. }
  69. return NULL;
  70. }
  71. /**
  72. * Check that an option exists, without modifying argv.
  73. *
  74. * @param argv The argv, which must be null terminated.
  75. * @param opt The option to find, e.g. "-h", "--help"
  76. *
  77. * @return PJ_TRUE if the option exists, else PJ_FALSE.
  78. */
  79. PJ_INLINE(pj_bool_t) pj_argparse_exists(char *const argv[], const char *opt)
  80. {
  81. int i;
  82. for (i=1; argv[i]; ++i) {
  83. if (pj_ansi_strcmp(argv[i], opt)==0)
  84. return PJ_TRUE;
  85. }
  86. return PJ_FALSE;
  87. }
  88. /**
  89. * Check for an option and if it exists remove that option from argc/argv and
  90. * returns PJ_TRUE.
  91. *
  92. * @param argc Pointer to argc.
  93. * @param argv Null terminated argv.
  94. * @param opt The option to find, e.g. "-h", "--help"
  95. *
  96. * @return PJ_TRUE if the option exists, else PJ_FALSE.
  97. */
  98. PJ_INLINE(pj_bool_t) pj_argparse_get_bool(int *argc, char *argv[],
  99. const char *opt)
  100. {
  101. int i;
  102. for (i=1; argv[i]; ++i) {
  103. if (pj_ansi_strcmp(argv[i], opt)==0) {
  104. pj_memmove(&argv[i], &argv[i+1], ((*argc)-i)*sizeof(char*));
  105. (*argc)--;
  106. return PJ_TRUE;
  107. }
  108. }
  109. return PJ_FALSE;
  110. }
  111. /**
  112. * Check for an option and if it exists get the value and remove both
  113. * the option and the value from argc/argv. Note that the function only
  114. * supports whitespace(s) as separator between option and value (i.e. equal
  115. * sign is not supported, e.g. "--server=127.0.0.1" will not be parsed
  116. * correctly).
  117. *
  118. * @param argc Pointer to argc.
  119. * @param argv Null terminated argv.
  120. * @param opt The option to find, e.g. "-t", "--type"
  121. * @param ptr_value Pointer to receive the value.
  122. *
  123. * @return - PJ_SUCCESS if the option exists and value is found
  124. * or if the option does not exist
  125. * - PJ_EINVAL if the option exits but value is not found
  126. */
  127. PJ_INLINE(pj_status_t) pj_argparse_get_str(int *argc, char *argv[],
  128. const char *opt, char **ptr_value)
  129. {
  130. int i;
  131. for (i=1; argv[i]; ++i) {
  132. if (pj_ansi_strcmp(argv[i], opt)==0) {
  133. pj_memmove(&argv[i], &argv[i+1], ((*argc)-i)*sizeof(char*));
  134. (*argc)--;
  135. if (argv[i]) {
  136. char *val = argv[i];
  137. pj_memmove(&argv[i], &argv[i+1], ((*argc)-i)*sizeof(char*));
  138. (*argc)--;
  139. *ptr_value = val;
  140. return PJ_SUCCESS;
  141. } else {
  142. PJ_ARGPARSE_ERROR("Error: missing value for %s argument",
  143. opt);
  144. return PJ_EINVAL;
  145. }
  146. }
  147. }
  148. return PJ_SUCCESS;
  149. }
  150. /**
  151. * Check for an option and if it exists, get the integer value and remove both
  152. * the option and the value from argc/argv. Note that the function only
  153. * supports whitespace(s) as separator between option and value (i.e. equal
  154. * sign is not supported, e.g. "--port=80" will not be parsed correctly).
  155. *
  156. * @param opt The option to find, e.g. "-h", "--help"
  157. * @param argc Pointer to argc.
  158. * @param argv Null terminated argv.
  159. * @param ptr_value Pointer to receive the value.
  160. *
  161. * @return - PJ_SUCCESS if the option exists and value is found
  162. * or if the option does not exist
  163. * - PJ_EINVAL if the option exits but value is not found,
  164. * or if the value is not an integer.
  165. */
  166. PJ_INLINE(pj_status_t) pj_argparse_get_int(int *argc, char *argv[],
  167. const char *opt, int *ptr_value)
  168. {
  169. char *endptr, *sval=NULL;
  170. long val;
  171. pj_status_t status = pj_argparse_get_str(argc, argv, opt, &sval);
  172. if (status!=PJ_SUCCESS || !sval)
  173. return status;
  174. val = strtol(sval, &endptr, 10);
  175. if (*endptr) {
  176. PJ_ARGPARSE_ERROR("Error: invalid value for %s argument",
  177. opt);
  178. return PJ_EINVAL;
  179. }
  180. *ptr_value = (int)val;
  181. return PJ_SUCCESS;
  182. }
  183. /**
  184. * @}
  185. */
  186. PJ_END_DECL
  187. #endif /* __PJ_ARGPARSE_H__ */