optparse_demo.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "optparse.h"
  2. #include "finsh.h"
  3. int optparse_short_test(int argc, char **argv)
  4. {
  5. int ch;
  6. struct optparse options;
  7. optparse_init(&options, argv);
  8. while((ch = optparse(&options, "ab:c::")) != -1)
  9. {
  10. ch = ch;
  11. rt_kprintf("\n");
  12. rt_kprintf("optopt = %c\n", options.optopt);
  13. rt_kprintf("optarg = %s\n", options.optarg);
  14. rt_kprintf("optind = %d\n", options.optind);
  15. }
  16. rt_kprintf("\n");
  17. return RT_EOK;
  18. }
  19. MSH_CMD_EXPORT_ALIAS(optparse_short_test, ost, test optparse_short cmd.);
  20. static struct optparse_long long_opts[] =
  21. {
  22. {"aaa", 'a', OPTPARSE_NONE },
  23. {"bbb", 'b', OPTPARSE_REQUIRED},
  24. {"ccc", 'c', OPTPARSE_OPTIONAL},
  25. { NULL, 0, OPTPARSE_NONE }
  26. };
  27. int optparse_long_test(int argc, char **argv)
  28. {
  29. int ch;
  30. int option_index;
  31. struct optparse options;
  32. optparse_init(&options, argv);
  33. while((ch = optparse_long(&options, long_opts, &option_index)) != -1)
  34. {
  35. ch = ch;
  36. rt_kprintf("\n");
  37. rt_kprintf("optopt = %c\n", options.optopt);
  38. rt_kprintf("optarg = %s\n", options.optarg);
  39. rt_kprintf("optind = %d\n", options.optind);
  40. rt_kprintf("option_index = %d\n", option_index);
  41. }
  42. rt_kprintf("\n");
  43. return RT_EOK;
  44. }
  45. MSH_CMD_EXPORT_ALIAS(optparse_long_test, olt, test optparse_long cmd.);