scsl/Flag.cc

445 lines
8.4 KiB
C++
Raw Normal View History

///
/// \file Flag.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-12
/// \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.
///
#include <iostream>
#include <regex>
#include <vector>
#include "Flag.h"
2023-10-16 00:09:31 +00:00
#include "StringUtil.h"
2023-10-16 00:09:31 +00:00
namespace scsl {
static const std::regex isFlag("^--?[a-zA-Z0-9][a-zA-Z0-9-_]*$",
std::regex_constants::nosubs);
2023-10-16 00:09:31 +00:00
std::string
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-16 00:09:31 +00:00
Flag *
NewFlag(std::string fName, FlagType fType, std::string fDescription)
{
2023-10-16 00:09:31 +00:00
auto flag = new Flag;
flag->Type = fType;
flag->WasSet = false;
flag->Name = fName;
2023-10-16 00:09:31 +00:00
flag->Description = fDescription;
flag->Value = FlagValue{};
2023-10-16 00:09:31 +00:00
return flag;
}
Flags::Flags(std::string fName)
: name(fName), description("")
{
}
Flags::Flags(std::string fName, std::string fDescription)
: name(fName), description(fDescription)
{
}
Flags::~Flags()
{
for (auto flag : this->flags) {
if (flag.second->Type == FlagType::String) {
delete flag.second->Value.s;
}
delete flag.second;
}
}
bool
Flags::Register(std::string fName, FlagType fType, std::string fDescription)
{
if (!std::regex_search(fName, isFlag) || fName == "-h") {
return false;
}
if (this->flags.count(fName) != 0) {
return false;
}
auto flag = NewFlag(fName, fType, fDescription);
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();
}
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-16 00:09:31 +00:00
bool
Flags::ValueOf(std::string fName, FlagValue &value)
{
if (this->flags.count(fName)) {
2023-10-16 00:09:31 +00:00
return false;
}
2023-10-16 00:09:31 +00:00
value = this->flags[fName]->Value;
return true;
}
Flags::ParseStatus
Flags::parseArg(int argc, char **argv, int &index)
{
std::string arg(argv[index]);
2023-10-16 00:09:31 +00:00
U::S::TrimWhitespace(arg);
index++;
if (!std::regex_search(arg, isFlag)) {
return ParseStatus::EndOfFlags;
}
if (this->flags.count(arg) == 0) {
if (arg == "-h" || arg == "--help") {
Usage(std::cout, 0);
}
return ParseStatus::NotRegistered;
}
auto flag = flags[arg];
if ((flag->Type != FlagType::Boolean) && index == argc) {
return ParseStatus::NotEnoughArgs;
}
2023-10-16 00:09:31 +00:00
switch (flag->Type) {
case FlagType::Boolean:
flag->WasSet = true;
flag->Value.b = true;
return ParseStatus::OK;
2023-10-16 00:09:31 +00:00
case FlagType::Integer:
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:
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:
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:
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;
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
}
return ParseStatus::OK;
}
Flags::ParseStatus
Flags::Parse(int argc, char **argv, bool skipFirst)
{
int index = 0;
if (skipFirst) {
index = 1;
}
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:
// 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
}
}
return ParseStatus::OK;
}
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);
}
size_t
Flags::NumArgs()
{
return this->args.size();
}
std::vector<std::string>
Flags::Args()
{
return this->args;
}
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)
{
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)
{
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)
{
auto flag = this->checkGetArg(fName, FlagType::UnsignedInteger);
2023-10-16 00:09:31 +00:00
flagValue = flag->Value.u;
return flag->WasSet;
}
bool
2023-10-16 00:09:31 +00:00
Flags::GetSizeT(std::string fName, std::size_t &flagValue)
{
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)
{
auto flag = this->checkGetArg(fName, FlagType::String);
2023-10-16 00:09:31 +00:00
if (flag->Value.s == nullptr) {
return false;
}
2023-10-16 00:09:31 +00:00
flagValue = *(flag->Value.s);
return flag->WasSet;
}
2023-10-16 00:09:31 +00:00
} // namespace scsl