blob: cabc8f398c469ae3faf5b2b50d1301ed26e67dd7 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <iomanip>
19#include <iostream>
20#include <memory>
21#include <set>
22#include <string>
23#include <vector>
24
25#include "android-base/macros.h"
26
27#include "idmap2/CommandLineOptions.h"
28
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010029namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020030
31std::unique_ptr<std::vector<std::string>> CommandLineOptions::ConvertArgvToVector(
32 int argc, const char** argv) {
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010033 return std::make_unique<std::vector<std::string>>(argv + 1, argv + argc);
Mårten Kongstad02751232018-04-27 13:16:32 +020034}
35
36CommandLineOptions& CommandLineOptions::OptionalFlag(const std::string& name,
37 const std::string& description, bool* value) {
38 assert(value != nullptr);
39 auto func = [value](const std::string& arg ATTRIBUTE_UNUSED) -> void { *value = true; };
40 options_.push_back(Option{name, description, func, Option::COUNT_OPTIONAL, false});
41 return *this;
42}
43
44CommandLineOptions& CommandLineOptions::MandatoryOption(const std::string& name,
45 const std::string& description,
46 std::string* value) {
47 assert(value != nullptr);
48 auto func = [value](const std::string& arg) -> void { *value = arg; };
49 options_.push_back(Option{name, description, func, Option::COUNT_EXACTLY_ONCE, true});
50 return *this;
51}
52
53CommandLineOptions& CommandLineOptions::MandatoryOption(const std::string& name,
54 const std::string& description,
55 std::vector<std::string>* value) {
56 assert(value != nullptr);
57 auto func = [value](const std::string& arg) -> void { value->push_back(arg); };
58 options_.push_back(Option{name, description, func, Option::COUNT_ONCE_OR_MORE, true});
59 return *this;
60}
61
62CommandLineOptions& CommandLineOptions::OptionalOption(const std::string& name,
63 const std::string& description,
64 std::string* value) {
65 assert(value != nullptr);
66 auto func = [value](const std::string& arg) -> void { *value = arg; };
67 options_.push_back(Option{name, description, func, Option::COUNT_OPTIONAL, true});
68 return *this;
69}
70
71bool CommandLineOptions::Parse(const std::vector<std::string>& argv, std::ostream& outError) const {
72 const auto pivot = std::partition(options_.begin(), options_.end(), [](const Option& opt) {
73 return opt.count != Option::COUNT_OPTIONAL;
74 });
75 std::set<std::string> mandatory_opts;
76 std::transform(options_.begin(), pivot, std::inserter(mandatory_opts, mandatory_opts.end()),
77 [](const Option& opt) -> std::string { return opt.name; });
78
79 const size_t argv_size = argv.size();
80 for (size_t i = 0; i < argv_size; i++) {
81 const std::string arg = argv[i];
82 if ("--help" == arg || "-h" == arg) {
83 Usage(outError);
84 return false;
85 }
86 bool match = false;
87 for (const Option& opt : options_) {
88 if (opt.name == arg) {
89 match = true;
90
91 if (opt.argument) {
92 i++;
93 if (i >= argv_size) {
94 outError << "error: " << opt.name << ": missing argument" << std::endl;
95 Usage(outError);
96 return false;
97 }
98 }
99 opt.action(argv[i]);
100 mandatory_opts.erase(opt.name);
101 break;
102 }
103 }
104 if (!match) {
105 outError << "error: " << arg << ": unknown option" << std::endl;
106 Usage(outError);
107 return false;
108 }
109 }
110
111 if (!mandatory_opts.empty()) {
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100112 for (const auto& opt : mandatory_opts) {
113 outError << "error: " << opt << ": missing mandatory option" << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200114 }
115 Usage(outError);
116 return false;
117 }
118 return true;
119}
120
121void CommandLineOptions::Usage(std::ostream& out) const {
122 size_t maxLength = 0;
123 out << "usage: " << name_;
124 for (const Option& opt : options_) {
125 const bool mandatory = opt.count != Option::COUNT_OPTIONAL;
126 out << " ";
127 if (!mandatory) {
128 out << "[";
129 }
130 if (opt.argument) {
131 out << opt.name << " arg";
132 maxLength = std::max(maxLength, opt.name.size() + 4);
133 } else {
134 out << opt.name;
135 maxLength = std::max(maxLength, opt.name.size());
136 }
137 if (!mandatory) {
138 out << "]";
139 }
140 if (opt.count == Option::COUNT_ONCE_OR_MORE) {
141 out << " [" << opt.name << " arg [..]]";
142 }
143 }
144 out << std::endl << std::endl;
145 for (const Option& opt : options_) {
146 out << std::left << std::setw(maxLength);
147 if (opt.argument) {
148 out << (opt.name + " arg");
149 } else {
150 out << opt.name;
151 }
152 out << " " << opt.description;
153 if (opt.count == Option::COUNT_ONCE_OR_MORE) {
154 out << " (can be provided multiple times)";
155 }
156 out << std::endl;
157 }
158}
159
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100160} // namespace android::idmap2