2023-10-15 08:11:06 +00:00
|
|
|
///
|
2023-10-16 09:18:09 +00:00
|
|
|
/// \file Flag.cc
|
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2023-10-12
|
2023-10-15 08:11:06 +00:00
|
|
|
/// \brief Flag defines a command-line flag parser.
|
|
|
|
///
|
|
|
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
///
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
#include <iostream>
|
2023-10-15 08:11:06 +00:00
|
|
|
#include <regex>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Flag.h"
|
2023-10-16 00:09:31 +00:00
|
|
|
#include "StringUtil.h"
|
2023-10-15 08:11:06 +00:00
|
|
|
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
namespace scsl {
|
2023-10-15 08:11:06 +00:00
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
static const std::regex isFlag("^--?[a-zA-Z0-9][a-zA-Z0-9-_]*$",
|
2023-10-15 08:11:06 +00:00
|
|
|
std::regex_constants::nosubs);
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
std::string
|
2023-10-16 09:18:09 +00:00
|
|
|
Flags::ParseStatusToString(ParseStatus status)
|
2023-10-16 00:09:31 +00:00
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case ParseStatus::OK:
|
|
|
|
return "OK";
|
|
|
|
case ParseStatus::EndOfFlags:
|
|
|
|
return "end of flags";
|
|
|
|
case ParseStatus::NotRegistered:
|
|
|
|
return "flag not registered";
|
|
|
|
case ParseStatus::NotEnoughArgs:
|
|
|
|
return "not enough args passed to flags";
|
|
|
|
default:
|
|
|
|
return "unknown/unspecified parse error";
|
|
|
|
}
|
|
|
|
}
|
2023-10-15 08:11:06 +00:00
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
Flag *
|
2023-10-16 09:18:09 +00:00
|
|
|
NewFlag(std::string fName, FlagType fType, std::string fDescription)
|
2023-10-15 08:11:06 +00:00
|
|
|
{
|
2023-10-16 00:09:31 +00:00
|
|
|
auto flag = new Flag;
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->Type = fType;
|
|
|
|
flag->WasSet = false;
|
|
|
|
flag->Name = fName;
|
2023-10-16 00:09:31 +00:00
|
|
|
flag->Description = fDescription;
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->Value = FlagValue{};
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
return flag;
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Flags::Flags(std::string fName)
|
2023-10-16 09:18:09 +00:00
|
|
|
: name(fName), description("")
|
2023-10-15 08:11:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Flags::Flags(std::string fName, std::string fDescription)
|
|
|
|
: name(fName), description(fDescription)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
Flags::~Flags()
|
|
|
|
{
|
|
|
|
for (auto flag : this->flags) {
|
|
|
|
if (flag.second->Type == FlagType::String) {
|
|
|
|
delete flag.second->Value.s;
|
|
|
|
}
|
|
|
|
delete flag.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-15 08:11:06 +00:00
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, FlagType fType, std::string fDescription)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
if (!std::regex_search(fName, isFlag) || fName == "-h") {
|
2023-10-15 08:11:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->flags.count(fName) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = NewFlag(fName, fType, fDescription);
|
2023-10-15 08:11:06 +00:00
|
|
|
this->flags[fName] = flag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, bool defaultValue, std::string fDescription)
|
|
|
|
{
|
|
|
|
if (!this->Register(fName, FlagType::Boolean, fDescription)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->flags[fName]->Value.b = defaultValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, int defaultValue, std::string fDescription)
|
|
|
|
{
|
|
|
|
if (!this->Register(fName, FlagType::Integer, fDescription)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->flags[fName]->Value.i = defaultValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, unsigned int defaultValue, std::string fDescription)
|
|
|
|
{
|
|
|
|
if (!this->Register(fName, FlagType::UnsignedInteger, fDescription)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->flags[fName]->Value.u = defaultValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, size_t defaultValue, std::string fDescription)
|
|
|
|
{
|
|
|
|
if (!this->Register(fName, FlagType::SizeT, fDescription)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->flags[fName]->Value.size = defaultValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::Register(std::string fName, std::string defaultValue, std::string fDescription)
|
|
|
|
{
|
|
|
|
if (!this->Register(fName, FlagType::String, fDescription)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->flags[fName]->Value.s = new std::string(defaultValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
Flags::Size()
|
|
|
|
{
|
|
|
|
return this->flags.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-15 08:11:06 +00:00
|
|
|
Flag *
|
|
|
|
Flags::Lookup(std::string fName)
|
|
|
|
{
|
|
|
|
if (this->flags.count(fName) == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
return this->flags[fName];
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
bool
|
|
|
|
Flags::ValueOf(std::string fName, FlagValue &value)
|
2023-10-15 08:11:06 +00:00
|
|
|
{
|
|
|
|
if (this->flags.count(fName)) {
|
2023-10-16 00:09:31 +00:00
|
|
|
return false;
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
value = this->flags[fName]->Value;
|
|
|
|
return true;
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
Flags::ParseStatus
|
2023-10-15 08:11:06 +00:00
|
|
|
Flags::parseArg(int argc, char **argv, int &index)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
std::string arg(argv[index]);
|
2023-10-16 00:09:31 +00:00
|
|
|
U::S::TrimWhitespace(arg);
|
|
|
|
|
2023-10-15 08:11:06 +00:00
|
|
|
index++;
|
|
|
|
if (!std::regex_search(arg, isFlag)) {
|
|
|
|
return ParseStatus::EndOfFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->flags.count(arg) == 0) {
|
2023-10-16 09:18:09 +00:00
|
|
|
if (arg == "-h" || arg == "--help") {
|
|
|
|
Usage(std::cout, 0);
|
|
|
|
}
|
2023-10-15 08:11:06 +00:00
|
|
|
return ParseStatus::NotRegistered;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flag = flags[arg];
|
2023-10-16 09:18:09 +00:00
|
|
|
if ((flag->Type != FlagType::Boolean) && index == argc) {
|
|
|
|
return ParseStatus::NotEnoughArgs;
|
|
|
|
}
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
switch (flag->Type) {
|
|
|
|
case FlagType::Boolean:
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->WasSet = true;
|
2023-10-15 08:11:06 +00:00
|
|
|
flag->Value.b = true;
|
|
|
|
return ParseStatus::OK;
|
2023-10-16 00:09:31 +00:00
|
|
|
case FlagType::Integer:
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->WasSet = true;
|
2023-10-16 00:09:31 +00:00
|
|
|
flag->Value.i = std::stoi(argv[++index], 0, 0);
|
|
|
|
return ParseStatus::OK;
|
|
|
|
case FlagType::UnsignedInteger:
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->WasSet = true;
|
2023-10-16 00:09:31 +00:00
|
|
|
flag->Value.u = static_cast<unsigned int>(std::stoi(argv[index++], 0, 0));
|
|
|
|
return ParseStatus::OK;
|
|
|
|
case FlagType::String:
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->WasSet = true;
|
2023-10-16 00:09:31 +00:00
|
|
|
flag->Value.s = new std::string(argv[index++]);
|
|
|
|
return ParseStatus::OK;
|
|
|
|
case FlagType::SizeT:
|
2023-10-16 09:18:09 +00:00
|
|
|
flag->WasSet = true;
|
2023-10-16 00:09:31 +00:00
|
|
|
flag->Value.size = static_cast<size_t>(std::stoull(argv[index++]));
|
|
|
|
return ParseStatus::OK;
|
2023-10-15 08:11:06 +00:00
|
|
|
default:
|
2023-10-16 00:09:31 +00:00
|
|
|
#if defined(NDEBUG) or defined(SCSL_NOEXCEPT)
|
|
|
|
return ParseStatus::Unknown;
|
|
|
|
#else
|
|
|
|
throw std::runtime_error("unhandled type");
|
|
|
|
#endif
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParseStatus::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
Flags::ParseStatus
|
|
|
|
Flags::Parse(int argc, char **argv, bool skipFirst)
|
2023-10-15 08:11:06 +00:00
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
int index = 0;
|
|
|
|
if (skipFirst) {
|
|
|
|
index = 1;
|
|
|
|
}
|
2023-10-15 08:11:06 +00:00
|
|
|
|
|
|
|
while (index != argc) {
|
|
|
|
auto result = this->parseArg(argc, argv, index);
|
|
|
|
switch (result) {
|
|
|
|
case ParseStatus::OK:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case ParseStatus::EndOfFlags:
|
|
|
|
while (index < argc) {
|
|
|
|
this->args.push_back(std::string(argv[index]));
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case ParseStatus::NotEnoughArgs:
|
2023-10-16 00:09:31 +00:00
|
|
|
case ParseStatus::NotRegistered:
|
2023-10-15 08:11:06 +00:00
|
|
|
// fall through //
|
|
|
|
return result;
|
|
|
|
default:
|
2023-10-16 00:09:31 +00:00
|
|
|
#if defined(NDEBUG) or defined(SCSL_NOEXCEPT)
|
|
|
|
return ParseStatus::Unknown;
|
|
|
|
#else
|
|
|
|
throw std::runtime_error("unhandled parse state");
|
|
|
|
#endif
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseStatus::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
void
|
|
|
|
Flags::Usage(std::ostream &os, int exitCode)
|
|
|
|
{
|
|
|
|
os << this->name << ":\t";
|
|
|
|
auto indent = this->name.size() + 7;
|
|
|
|
|
|
|
|
U::S::WriteTabIndented(os, description, 72 - indent, indent / 8, false);
|
|
|
|
os << "\n\n";
|
|
|
|
|
|
|
|
for (const auto &pair : this->flags) {
|
|
|
|
auto argLine = "\t" + pair.first;
|
|
|
|
switch (pair.second->Type) {
|
|
|
|
case FlagType::Boolean:
|
|
|
|
argLine += "\t\t";
|
|
|
|
break;
|
|
|
|
case FlagType::Integer:
|
|
|
|
argLine += "int\t\t";
|
|
|
|
break;
|
|
|
|
case FlagType::UnsignedInteger:
|
|
|
|
argLine += "uint\t\t";
|
|
|
|
break;
|
|
|
|
case FlagType::SizeT:
|
|
|
|
argLine += "size_t\t";
|
|
|
|
break;
|
|
|
|
case FlagType::String:
|
|
|
|
argLine += "string\t";
|
|
|
|
break;
|
|
|
|
case FlagType::Unknown:
|
|
|
|
// fallthrough
|
|
|
|
default:
|
|
|
|
#ifdef SCSL_NOEXCEPT
|
|
|
|
abort();
|
|
|
|
#else
|
|
|
|
throw std::runtime_error("unhandled parsing error - this is a bug");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << argLine;
|
|
|
|
indent = argLine.size();
|
|
|
|
U::S::WriteTabIndented(os, pair.second->Description,
|
|
|
|
72-indent, (indent/8)+2, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "\n";
|
|
|
|
exit(exitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-15 08:11:06 +00:00
|
|
|
size_t
|
|
|
|
Flags::NumArgs()
|
|
|
|
{
|
|
|
|
return this->args.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
Flags::Args()
|
|
|
|
{
|
|
|
|
return this->args;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 09:18:09 +00:00
|
|
|
std::string
|
|
|
|
Flags::Arg(size_t i)
|
|
|
|
{
|
|
|
|
if (i >= this->args.size()) {
|
|
|
|
throw std::out_of_range("index is out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->args[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
Flag *
|
|
|
|
Flags::checkGetArg(std::string fName, FlagType eType)
|
|
|
|
{
|
|
|
|
if (this->flags[fName] == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flag = this->flags[fName];
|
|
|
|
if (flag == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag->Type != eType) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::GetBool(std::string fName, bool &flagValue)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = this->checkGetArg(fName, FlagType::Boolean);
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
flagValue = flag->Value.b;
|
|
|
|
return flag->WasSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::GetInteger(std::string fName, int &flagValue)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = this->checkGetArg(fName, FlagType::Integer);
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
flagValue = flag->Value.i;
|
|
|
|
return flag->WasSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::GetUnsignedInteger(std::string fName, unsigned int &flagValue)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = this->checkGetArg(fName, FlagType::UnsignedInteger);
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
flagValue = flag->Value.u;
|
|
|
|
return flag->WasSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-15 08:11:06 +00:00
|
|
|
bool
|
2023-10-16 00:09:31 +00:00
|
|
|
Flags::GetSizeT(std::string fName, std::size_t &flagValue)
|
2023-10-15 08:11:06 +00:00
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = this->checkGetArg(fName, FlagType::SizeT);
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
flagValue = flag->Value.size;
|
|
|
|
return flag->WasSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Flags::GetString(std::string fName, std::string &flagValue)
|
|
|
|
{
|
2023-10-16 09:18:09 +00:00
|
|
|
auto flag = this->checkGetArg(fName, FlagType::String);
|
2023-10-16 00:09:31 +00:00
|
|
|
|
|
|
|
if (flag->Value.s == nullptr) {
|
2023-10-15 08:11:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
flagValue = *(flag->Value.s);
|
|
|
|
return flag->WasSet;
|
2023-10-15 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-16 00:09:31 +00:00
|
|
|
} // namespace scsl
|
2023-10-15 08:11:06 +00:00
|
|
|
|