blob: 9ec18a3f5bb9dd6456f5674cc5ca05f37bd45f46 [file] [log] [blame]
Adam Langleyaacec172014-06-20 12:00:00 -07001/* 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
15#include <string>
16#include <vector>
Adam Langleyaacec172014-06-20 12:00:00 -070017
Adam Langley839b8812015-05-26 11:36:46 -070018#include <limits.h>
Ben Laurieeba23842014-09-30 12:44:15 +010019#include <stdio.h>
David Benjamin1a3c2322015-06-05 15:11:42 -040020#include <stdlib.h>
Adam Langley1f26ed72015-04-13 15:59:36 -070021#include <string.h>
Adam Langleyaacec172014-06-20 12:00:00 -070022
23#include "internal.h"
24
25
26bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args,
27 const std::vector<std::string> &args,
28 const struct argument *templates) {
29 out_args->clear();
30
31 for (size_t i = 0; i < args.size(); i++) {
32 const std::string &arg = args[i];
33 const struct argument *templ = nullptr;
34 for (size_t j = 0; templates[j].name[0] != 0; j++) {
Adam Langley1f26ed72015-04-13 15:59:36 -070035 if (strcmp(arg.c_str(), templates[j].name) == 0) {
Adam Langleyaacec172014-06-20 12:00:00 -070036 templ = &templates[j];
37 break;
38 }
39 }
40
41 if (templ == nullptr) {
42 fprintf(stderr, "Unknown argument: %s\n", arg.c_str());
43 return false;
44 }
45
Adam Langleyaacec172014-06-20 12:00:00 -070046 if (out_args->find(arg) != out_args->end()) {
David Benjamin05709232015-03-23 19:01:33 -040047 fprintf(stderr, "Duplicate argument: %s\n", arg.c_str());
Adam Langleyaacec172014-06-20 12:00:00 -070048 return false;
49 }
50
David Benjamin05709232015-03-23 19:01:33 -040051 if (templ->type == kBooleanArgument) {
52 (*out_args)[arg] = "";
53 } else {
54 if (i + 1 >= args.size()) {
55 fprintf(stderr, "Missing argument for option: %s\n", arg.c_str());
56 return false;
57 }
58 (*out_args)[arg] = args[++i];
59 }
Adam Langleyaacec172014-06-20 12:00:00 -070060 }
61
62 for (size_t j = 0; templates[j].name[0] != 0; j++) {
63 const struct argument *templ = &templates[j];
David Benjamin05709232015-03-23 19:01:33 -040064 if (templ->type == kRequiredArgument &&
65 out_args->find(templ->name) == out_args->end()) {
Adam Langleyaacec172014-06-20 12:00:00 -070066 fprintf(stderr, "Missing value for required argument: %s\n", templ->name);
67 return false;
68 }
69 }
70
71 return true;
72}
73
74void PrintUsage(const struct argument *templates) {
75 for (size_t i = 0; templates[i].name[0] != 0; i++) {
76 const struct argument *templ = &templates[i];
77 fprintf(stderr, "%s\t%s\n", templ->name, templ->description);
78 }
79}
Adam Langley839b8812015-05-26 11:36:46 -070080
81bool GetUnsigned(unsigned *out, const std::string &arg_name,
82 unsigned default_value,
83 const std::map<std::string, std::string> &args) {
84 const auto &it = args.find(arg_name);
85 if (it == args.end()) {
86 *out = default_value;
87 return true;
88 }
89
90 const std::string &value = it->second;
91 if (value.empty()) {
92 return false;
93 }
94
95 char *endptr;
96 unsigned long int num = strtoul(value.c_str(), &endptr, 10);
97 if (*endptr ||
98 num > UINT_MAX) {
99 return false;
100 }
101
102 *out = num;
103 return true;
104}