utils_getopt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __GETOPT_H__
  31. #define __GETOPT_H__
  32. /**
  33. * @brief Parameters needed to parse the command line
  34. *
  35. */
  36. typedef struct getopt_env {
  37. char *optarg; /*!< if the option accepts parameters, then optarg point to the option parameter*/
  38. int optind; /*!< current index of argv*/
  39. int opterr; /*!< non-zero enable error message output, while 0,no error message output*/
  40. int optopt; /*!< contain unrecognized option character*/
  41. int __optpos;
  42. } getopt_env_t;
  43. /**
  44. * @brief Initialize struct getopt_env
  45. *
  46. * @param env pointer to struct getopt_env
  47. * @param opterr set error message output method
  48. *
  49. * @return
  50. * - 0: success
  51. * - -1: fail
  52. */
  53. int utils_getopt_init(getopt_env_t *env, int opterr);
  54. /**
  55. * @brief Parses the command-line arguments
  56. *
  57. * @param env pointer to struct getopt_env
  58. * @param argc the argument count
  59. * @param argv the argument array
  60. *
  61. * @return
  62. * - option character : an option was successfully found
  63. * - -1 : all command-line options have been parsed
  64. * - '?' : option character was not in optstring
  65. * - ':' or '?' : If utils_getopt() encounters an option with a missing argument, then the return value depends on the first character in optstring: if it is ':', then ':' is returned; otherwise '?' is returned
  66. *
  67. * @note Example
  68. * @code
  69. *
  70. * #include <utils_getopt.h>
  71. * #include <stdio.h>
  72. *
  73. * void cmd(char *buf, int len, int argc, char **argv)
  74. * {
  75. * int opt;
  76. getopt_env_t getopt_env;
  77. utils_getopt_init(&getopt_env, 0);
  78. * //put ':' in the starting of the string so that program can distinguish between '?' and ':'
  79. * while ((opt = utils_getopt(&getopt_env, argc, argv, ":if:lr")) != -1) {
  80. * switch(opt)
  81. * {
  82. * case 'i':
  83. * case 'l':
  84. * case 'r':
  85. * printf("option: %c\r\n", opt);
  86. * break;
  87. * case 'f':
  88. * printf("filename: %s\r\n", getopt_env.optarg);
  89. * break;
  90. * case ':':
  91. printf("%s: %c requires an argument\r\n", *argv, getopt_env.optopt);
  92. * break;
  93. * case '?':
  94. * printf("unknow option: %c\r\n", getopt_env.optopt);
  95. * break;
  96. * }
  97. * }
  98. * //optind is for the extra arguments which are not parsed
  99. * for(; getopt_env.optind < argc; getopt_env.optind++){
  100. * printf("extra arguments: %s\r\n", argv[getopt_env.optind]);
  101. * }
  102. *
  103. * }
  104. * @endcode
  105. */
  106. int utils_getopt(getopt_env_t *env, int argc, char * const argv[], const char *optstring);
  107. #endif /* __GETOPT_H__ */