args.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <limits.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "internal.h"
  21. bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args,
  22. const std::vector<std::string> &args,
  23. const struct argument *templates) {
  24. out_args->clear();
  25. for (size_t i = 0; i < args.size(); i++) {
  26. const std::string &arg = args[i];
  27. const struct argument *templ = nullptr;
  28. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  29. if (strcmp(arg.c_str(), templates[j].name) == 0) {
  30. templ = &templates[j];
  31. break;
  32. }
  33. }
  34. if (templ == nullptr) {
  35. fprintf(stderr, "Unknown argument: %s\n", arg.c_str());
  36. return false;
  37. }
  38. if (out_args->find(arg) != out_args->end()) {
  39. fprintf(stderr, "Duplicate argument: %s\n", arg.c_str());
  40. return false;
  41. }
  42. if (templ->type == kBooleanArgument) {
  43. (*out_args)[arg] = "";
  44. } else {
  45. if (i + 1 >= args.size()) {
  46. fprintf(stderr, "Missing argument for option: %s\n", arg.c_str());
  47. return false;
  48. }
  49. (*out_args)[arg] = args[++i];
  50. }
  51. }
  52. for (size_t j = 0; templates[j].name[0] != 0; j++) {
  53. const struct argument *templ = &templates[j];
  54. if (templ->type == kRequiredArgument &&
  55. out_args->find(templ->name) == out_args->end()) {
  56. fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. void PrintUsage(const struct argument *templates) {
  63. for (size_t i = 0; templates[i].name[0] != 0; i++) {
  64. const struct argument *templ = &templates[i];
  65. fprintf(stderr, "%s\t%s\n", templ->name, templ->description);
  66. }
  67. }
  68. bool GetUnsigned(unsigned *out, const std::string &arg_name,
  69. unsigned default_value,
  70. const std::map<std::string, std::string> &args) {
  71. const auto &it = args.find(arg_name);
  72. if (it == args.end()) {
  73. *out = default_value;
  74. return true;
  75. }
  76. const std::string &value = it->second;
  77. if (value.empty()) {
  78. return false;
  79. }
  80. char *endptr;
  81. unsigned long int num = strtoul(value.c_str(), &endptr, 10);
  82. if (*endptr ||
  83. num > UINT_MAX) {
  84. return false;
  85. }
  86. *out = num;
  87. return true;
  88. }