Flags: show default value.

This commit is contained in:
Kyle Isom 2023-10-24 12:12:17 -07:00
parent f76d524999
commit 33675c18ec
3 changed files with 29 additions and 13 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.22)
project(scsl LANGUAGES CXX
VERSION 1.1.1
VERSION 1.1.2
DESCRIPTION "Shimmering Clarity Standard Library")
set(CMAKE_CXX_STANDARD 14)

View File

@ -42,7 +42,7 @@ namespace geom {
/// and rotations in three dimensions.
///
/// Quaternions encode rotations in three-dimensional space. While
/// technically a MakeQuaternion is comprised of a real element and a
/// technically a Quaternion is comprised of a real element and a
/// complex vector<3>, for the purposes of this library, it is modeled
/// as a floating point 4D vector of the form <w, x, y, z>, where x, y,
/// and z represent an Axis of rotation in R3 and w the Angle, in

View File

@ -25,16 +25,16 @@
#include <regex>
#include <utility>
#include <vector>
#include <scsl/Flags.h>
#include <scsl/StringUtil.h>
#include <vector>
namespace scsl {
static const std::regex isFlag("^--?[a-zA-Z0-9][a-zA-Z0-9-_]*$",
std::regex_constants::nosubs|std::regex_constants::optimize);
std::regex_constants::nosubs | std::regex_constants::optimize);
std::string
Flags::ParseStatusToString(ParseStatus status)
@ -82,7 +82,7 @@ Flags::Flags(std::string fName, std::string fDescription)
Flags::~Flags()
{
for (auto &flag : this->flags) {
for (auto &flag: this->flags) {
if (flag.second->Type == FlagType::String) {
delete flag.second->Value.s;
}
@ -300,7 +300,6 @@ Flags::Parse(int argc, char **argv, bool skipFirst)
throw std::runtime_error("unhandled parse state");
#endif
}
}
return ParseStatus::OK;
@ -313,26 +312,44 @@ Flags::Usage(std::ostream &os, int exitCode)
os << this->name << ":\t";
auto indent = this->name.size() + 7;
scstring::WriteTabIndented(os, description, 72 - indent, indent / 8, false);
scstring::WriteTabIndented(os, this->description, 72 - indent,
indent / 8, false);
os << "\n\n";
for (const auto &pair : this->flags) {
for (const auto &pair: this->flags) {
auto argDesc = pair.second->Description;
if (!argDesc.empty()) {
argDesc += ' ';
}
auto argLine = "\t" + pair.first;
switch (pair.second->Type) {
case FlagType::Boolean:
argLine += "\t\t";
argDesc += "(default=false)";
break;
case FlagType::Integer:
argLine += " int\t\t";
if (pair.second->Value.i != 0) {
argDesc += "(default=" + std::to_string(pair.second->Value.i) + ")";
}
break;
case FlagType::UnsignedInteger:
argLine += " uint\t\t";
if (pair.second->Value.u != 0) {
argDesc += "(default=" + std::to_string(pair.second->Value.u) + ")";
}
break;
case FlagType::SizeT:
argLine += " size_t\t";
if (pair.second->Value.size != 0) {
argDesc += "(default=" + std::to_string(pair.second->Value.size) + ")";
}
break;
case FlagType::String:
argLine += " string\t";
if (pair.second->Value.s != nullptr && !(pair.second->Value.s->empty())) {
argDesc += "(default=" + *(pair.second->Value.s) + ")";
}
break;
case FlagType::Unknown:
// fallthrough
@ -347,8 +364,8 @@ Flags::Usage(std::ostream &os, int exitCode)
os << argLine;
indent = argLine.size();
scstring::WriteTabIndented(os, pair.second->Description,
72-indent, (indent/8)+2, false);
scstring::WriteTabIndented(os, argDesc, 72 - indent,
(indent / 8) + 2, false);
}
os << "\n";
@ -382,7 +399,7 @@ Flags::Arg(size_t i)
Flag *
Flags::checkGetArg(std::string& fName, FlagType eType)
Flags::checkGetArg(std::string &fName, FlagType eType)
{
if (this->flags[fName] == nullptr) {
return nullptr;
@ -455,5 +472,4 @@ Flags::GetString(std::string fName, std::string &flagValue)
}
} // namespace scsl
}// namespace scsl