2015-08-14 14:26:04 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Flags.h"
|
|
|
|
#include "util/StringPiece.h"
|
2015-11-02 16:10:55 -08:00
|
|
|
#include "util/Util.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace aapt {
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::requiredFlag(const StringPiece& name,
|
|
|
|
const StringPiece& description, std::string* value) {
|
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
*value = arg.toString();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, true, 1, false});
|
|
|
|
return *this;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::requiredFlagList(const StringPiece& name,
|
|
|
|
const StringPiece& description,
|
2015-08-14 14:26:04 -07:00
|
|
|
std::vector<std::string>* value) {
|
2016-10-19 12:18:14 -07:00
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
value->push_back(arg.toString());
|
|
|
|
return true;
|
|
|
|
};
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, true, 1, false});
|
|
|
|
return *this;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::optionalFlag(const StringPiece& name,
|
|
|
|
const StringPiece& description,
|
2015-08-14 14:26:04 -07:00
|
|
|
Maybe<std::string>* value) {
|
2016-10-19 12:18:14 -07:00
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
*value = arg.toString();
|
|
|
|
return true;
|
|
|
|
};
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, false, 1, false});
|
|
|
|
return *this;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::optionalFlagList(const StringPiece& name,
|
|
|
|
const StringPiece& description,
|
2015-08-14 14:26:04 -07:00
|
|
|
std::vector<std::string>* value) {
|
2016-10-19 12:18:14 -07:00
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
value->push_back(arg.toString());
|
|
|
|
return true;
|
|
|
|
};
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, false, 1, false});
|
|
|
|
return *this;
|
2016-08-08 12:35:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::optionalFlagList(const StringPiece& name,
|
|
|
|
const StringPiece& description,
|
2016-08-08 12:35:04 -07:00
|
|
|
std::unordered_set<std::string>* value) {
|
2016-10-19 12:18:14 -07:00
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
value->insert(arg.toString());
|
|
|
|
return true;
|
|
|
|
};
|
2016-08-08 12:35:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, false, 1, false});
|
|
|
|
return *this;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Flags& Flags::optionalSwitch(const StringPiece& name,
|
|
|
|
const StringPiece& description, bool* value) {
|
|
|
|
auto func = [value](const StringPiece& arg) -> bool {
|
|
|
|
*value = true;
|
|
|
|
return true;
|
|
|
|
};
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
mFlags.push_back(
|
|
|
|
Flag{name.toString(), description.toString(), func, false, 0, false});
|
|
|
|
return *this;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Flags::usage(const StringPiece& command, std::ostream* out) {
|
2016-10-19 12:18:14 -07:00
|
|
|
constexpr size_t kWidth = 50;
|
2016-01-11 13:10:24 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << command << " [options]";
|
|
|
|
for (const Flag& flag : mFlags) {
|
|
|
|
if (flag.required) {
|
|
|
|
*out << " " << flag.name << " arg";
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << " files...\n\nOptions:\n";
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
for (const Flag& flag : mFlags) {
|
|
|
|
std::string argLine = flag.name;
|
|
|
|
if (flag.numArgs > 0) {
|
|
|
|
argLine += " arg";
|
|
|
|
}
|
2015-11-02 16:10:55 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// Split the description by newlines and write out the argument (which is
|
|
|
|
// empty after
|
|
|
|
// the first line) followed by the description line. This will make sure
|
|
|
|
// that multiline
|
|
|
|
// descriptions are still right justified and aligned.
|
|
|
|
for (StringPiece line : util::tokenize(flag.description, '\n')) {
|
|
|
|
*out << " " << std::setw(kWidth) << std::left << argLine << line << "\n";
|
|
|
|
argLine = " ";
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
*out << " " << std::setw(kWidth) << std::left << "-h"
|
|
|
|
<< "Displays this help menu\n";
|
|
|
|
out->flush();
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
bool Flags::parse(const StringPiece& command,
|
|
|
|
const std::vector<StringPiece>& args,
|
2015-08-14 14:26:04 -07:00
|
|
|
std::ostream* outError) {
|
2016-10-19 12:18:14 -07:00
|
|
|
for (size_t i = 0; i < args.size(); i++) {
|
|
|
|
StringPiece arg = args[i];
|
|
|
|
if (*(arg.data()) != '-') {
|
|
|
|
mArgs.push_back(arg.toString());
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (arg == "-h" || arg == "--help") {
|
|
|
|
usage(command, outError);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
bool match = false;
|
|
|
|
for (Flag& flag : mFlags) {
|
|
|
|
if (arg == flag.name) {
|
|
|
|
if (flag.numArgs > 0) {
|
|
|
|
i++;
|
|
|
|
if (i >= args.size()) {
|
|
|
|
*outError << flag.name << " missing argument.\n\n";
|
2015-08-14 14:26:04 -07:00
|
|
|
usage(command, outError);
|
|
|
|
return false;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
flag.action(args[i]);
|
|
|
|
} else {
|
|
|
|
flag.action({});
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
flag.parsed = true;
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!match) {
|
|
|
|
*outError << "unknown option '" << arg << "'.\n\n";
|
|
|
|
usage(command, outError);
|
|
|
|
return false;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
for (const Flag& flag : mFlags) {
|
|
|
|
if (flag.required && !flag.parsed) {
|
|
|
|
*outError << "missing required flag " << flag.name << "\n\n";
|
|
|
|
usage(command, outError);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
const std::vector<std::string>& Flags::getArgs() { return mArgs; }
|
|
|
|
|
|
|
|
} // namespace aapt
|