args.cc 3.1 KB

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