argp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <argp.h>
  3. enum boolean { NO, YES };
  4. enum boolean bool = NO;
  5. const char * argp_program_version = "0.1";
  6. /* mail bug reports to */
  7. const char * argp_program_bug_address = "jbranso@fastmail.com";
  8. //define an argp option called joke aka "--joke" or "-j"
  9. /* struct argp_option argp_options_joke; */
  10. /* argp_options_joke.name = "joke"; */
  11. /* argp_options_joke.key = 'j'; */
  12. /* /\* This is the value that you see when you print: */
  13. /* ** ./argp --usage. */
  14. /* *\/ */
  15. /* argp_options_joke.arg = "ARG"; //must be provided. */
  16. /* argp_options_joke.doc = "Print a funny joke."; */
  17. /* argp_options_joke.flags = OPTION_ARG_OPTIONAL; // this option does not need an argument. */
  18. static const struct argp_option options [] =
  19. {
  20. {"joke", 'j', "ARG", OPTION_ARG_OPTIONAL, "Print a funny joke" },
  21. { 0 }
  22. };
  23. // the above can be specified much simplier. I could actually get rid of the argp_options_joke variable.
  24. //struct argp_option options [2] = { "joke", "j" "STRING", 0, "Print a funny joke", { 0 } };
  25. //define an argp parse function
  26. error_t argp_parser (int opt, char *arg, struct argp_state *state) {
  27. extern enum boolean bool;
  28. switch (opt)
  29. {
  30. // if this parser function is called on an option that it doesn't recognize, then don't do anything.
  31. default:
  32. return ARGP_ERR_UNKNOWN;
  33. case 'j':
  34. {
  35. bool = YES;
  36. break;
  37. }
  38. }
  39. return 0;
  40. }
  41. /* struct argp argp; */
  42. struct argp argp =
  43. { options, argp_parser, 0, "Just a simple test argp program" };
  44. /* a string containing the basic usage of this program. */
  45. /* If I try to set the argp values manually, I get a segmentation fault. */
  46. /* argp.args_doc = "argp [options]"; */
  47. /* argp.doc = "Just a simple test argp program!"; */
  48. /* argp.options = options; */
  49. /* argp.parser = argp_parser; */
  50. int main (int argc, char **argv) {
  51. // I still have a bit more to learn with argp, but that's ok!
  52. argp_parse (&argp, argc, argv, 0, 0, 0);
  53. if (bool == YES) {
  54. printf ("What do you call a box full of ducks?\n");
  55. printf ("A bunch of quackers.\n");
  56. }
  57. return 0;
  58. }