optparse.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. char **argv;
  18. int permute;
  19. int optind;
  20. int optopt;
  21. char *optarg;
  22. char errmsg[64];
  23. int subopt;
  24. };
  25. enum optparse_argtype
  26. {
  27. OPTPARSE_NONE,
  28. OPTPARSE_REQUIRED,
  29. OPTPARSE_OPTIONAL
  30. };
  31. struct optparse_long
  32. {
  33. const char *longname;
  34. int shortname;
  35. enum optparse_argtype argtype;
  36. };
  37. /**
  38. * Initializes the parser state.
  39. */
  40. void optparse_init(struct optparse *options, char **argv);
  41. /**
  42. * Read the next option in the argv array.
  43. * @param optstring a getopt()-formatted option string.
  44. * @return the next option character, -1 for done, or '?' for error
  45. *
  46. * Just like getopt(), a character followed by no colons means no
  47. * argument. One colon means the option has a required argument. Two
  48. * colons means the option takes an optional argument.
  49. */
  50. int optparse(struct optparse *options, const char *optstring);
  51. /**
  52. * Handles GNU-style long options in addition to getopt() options.
  53. * This works a lot like GNU's getopt_long(). The last option in
  54. * longopts must be all zeros, marking the end of the array. The
  55. * longindex argument may be NULL.
  56. */
  57. int optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex);
  58. /**
  59. * Used for stepping over non-option arguments.
  60. * @return the next non-option argument, or NULL for no more arguments
  61. *
  62. * Argument parsing can continue with optparse() after using this
  63. * function. That would be used to parse the options for the
  64. * subcommand returned by optparse_arg(). This function allows you to
  65. * ignore the value of optind.
  66. */
  67. char *optparse_arg(struct optparse *options);
  68. #endif /* __OPTPARSE_H_ */