選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

75 行
2.3 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <string>
  15. #include <vector>
  16. #include <map>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "internal.h"
  20. bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args,
  21. const std::vector<std::string> &args,
  22. const struct argument *templates) {
  23. out_args->clear();
  24. for (size_t i = 0; i < args.size(); i++) {
  25. const std::string &arg = args[i];
  26. const struct argument *templ = nullptr;
  27. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  28. if (strcmp(arg.c_str(), templates[j].name) == 0) {
  29. templ = &templates[j];
  30. break;
  31. }
  32. }
  33. if (templ == nullptr) {
  34. fprintf(stderr, "Unknown argument: %s\n", arg.c_str());
  35. return false;
  36. }
  37. if (i + 1 >= args.size()) {
  38. fprintf(stderr, "Missing argument for option: %s\n", arg.c_str());
  39. return false;
  40. }
  41. if (out_args->find(arg) != out_args->end()) {
  42. fprintf(stderr, "Duplicate value given for: %s\n", arg.c_str());
  43. return false;
  44. }
  45. (*out_args)[arg] = args[++i];
  46. }
  47. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  48. const struct argument *templ = &templates[j];
  49. if (templ->required && out_args->find(templ->name) == out_args->end()) {
  50. fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. void PrintUsage(const struct argument *templates) {
  57. for (size_t i = 0; templates[i].name[0] != 0; i++) {
  58. const struct argument *templ = &templates[i];
  59. fprintf(stderr, "%s\t%s\n", templ->name, templ->description);
  60. }
  61. }