optparse.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * @File: optparse.h
  3. * @Author: liu2guang
  4. * @Date: 2018-06-20 14:52:10
  5. *
  6. * @LICENSE: MIT
  7. * https://github.com/liu2guang/optparse/blob/master/LICENSE
  8. *
  9. * Change Logs:
  10. * Date Author Notes
  11. * 2018-06-20 liu2guang Adapter RT-Thread.
  12. */
  13. #ifndef __OPTPARSE_H_
  14. #define __OPTPARSE_H_
  15. struct optparse
  16. {
  17. int argc;
  18. char **argv;
  19. int permute;
  20. int optind;
  21. int optopt;
  22. char *optarg;
  23. char errmsg[64];
  24. int subopt;
  25. };
  26. enum optparse_argtype
  27. {
  28. OPTPARSE_NONE,
  29. OPTPARSE_REQUIRED,
  30. OPTPARSE_OPTIONAL
  31. };
  32. struct optparse_long
  33. {
  34. const char *longname;
  35. int shortname;
  36. enum optparse_argtype argtype;
  37. };
  38. /**
  39. * Initializes the parser state.
  40. */
  41. void optparse_init(struct optparse *options, int argc, char **argv);
  42. /**
  43. * Read the next option in the argv array.
  44. * @param optstring a getopt()-formatted option string.
  45. * @return the next option character, -1 for done, or '?' for error
  46. *
  47. * Just like getopt(), a character followed by no colons means no
  48. * argument. One colon means the option has a required argument. Two
  49. * colons means the option takes an optional argument.
  50. */
  51. int optparse(struct optparse *options, const char *optstring);
  52. /**
  53. * Handles GNU-style long options in addition to getopt() options.
  54. * This works a lot like GNU's getopt_long(). The last option in
  55. * longopts must be all zeros, marking the end of the array. The
  56. * longindex argument may be NULL.
  57. */
  58. int optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex);
  59. /**
  60. * Used for stepping over non-option arguments.
  61. * @return the next non-option argument, or NULL for no more arguments
  62. *
  63. * Argument parsing can continue with optparse() after using this
  64. * function. That would be used to parse the options for the
  65. * subcommand returned by optparse_arg(). This function allows you to
  66. * ignore the value of optind.
  67. */
  68. char *optparse_arg(struct optparse *options);
  69. #endif /* __OPTPARSE_H_ */