25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

78 satır
2.4 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 <stdio.h>
  17. #include <string.h>
  18. #include "internal.h"
  19. bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args,
  20. const std::vector<std::string> &args,
  21. const struct argument *templates) {
  22. out_args->clear();
  23. for (size_t i = 0; i < args.size(); i++) {
  24. const std::string &arg = args[i];
  25. const struct argument *templ = nullptr;
  26. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  27. if (strcmp(arg.c_str(), templates[j].name) == 0) {
  28. templ = &templates[j];
  29. break;
  30. }
  31. }
  32. if (templ == nullptr) {
  33. fprintf(stderr, "Unknown argument: %s\n", arg.c_str());
  34. return false;
  35. }
  36. if (out_args->find(arg) != out_args->end()) {
  37. fprintf(stderr, "Duplicate argument: %s\n", arg.c_str());
  38. return false;
  39. }
  40. if (templ->type == kBooleanArgument) {
  41. (*out_args)[arg] = "";
  42. } else {
  43. if (i + 1 >= args.size()) {
  44. fprintf(stderr, "Missing argument for option: %s\n", arg.c_str());
  45. return false;
  46. }
  47. (*out_args)[arg] = args[++i];
  48. }
  49. }
  50. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  51. const struct argument *templ = &templates[j];
  52. if (templ->type == kRequiredArgument &&
  53. out_args->find(templ->name) == out_args->end()) {
  54. fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. void PrintUsage(const struct argument *templates) {
  61. for (size_t i = 0; templates[i].name[0] != 0; i++) {
  62. const struct argument *templ = &templates[i];
  63. fprintf(stderr, "%s\t%s\n", templ->name, templ->description);
  64. }
  65. }