You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
1.2 KiB

  1. #pragma once
  2. #include <cppcore/conversion/convert_cast.h>
  3. #include "context.h"
  4. namespace cppargs
  5. {
  6. next_result context::next(std::string* key)
  7. {
  8. if (key)
  9. key->clear();
  10. if (index + 1 >= argc)
  11. return next_result::finished;
  12. ++index;
  13. arg = argv[index];
  14. value = nullptr;
  15. /* check argument */
  16. int i = 0;
  17. value = nullptr;
  18. while (arg[i] == '-')
  19. ++i;
  20. /* parse value */
  21. if (i == 1)
  22. {
  23. if (key)
  24. *key = std::string("-") + arg[1];
  25. if (arg[2] != '\0')
  26. value = &arg[2];
  27. return next_result::option_short;
  28. }
  29. else if (i == 2)
  30. {
  31. auto tmp = strchr(arg, '=');
  32. if (tmp)
  33. {
  34. value = tmp + 1;
  35. if (key)
  36. *key = std::string(arg, cppcore::convert_cast<size_t>(tmp - arg));
  37. }
  38. else if (key)
  39. {
  40. *key = arg;
  41. }
  42. return next_result::option_long;
  43. }
  44. else
  45. {
  46. return next_result::argument;
  47. }
  48. }
  49. }