Skip to content

Commit

Permalink
argparse: use replace raw char* strings with string views
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrelliCopter committed Apr 7, 2024
1 parent d69eec8 commit f9928df
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/argparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ArgParse::Options::ShowShortUsage(const std::string_view name, std::ostream
out << "Usage: " << name;
for (const auto& it : options)
{
if (it.argumentName)
if (!it.argumentName.empty())
{
// Option with argument
it.required
Expand Down Expand Up @@ -55,8 +55,8 @@ void ArgParse::Options::ShowHelpUsage(const std::string_view name, std::ostream&
out << ">" << std::endl;

// Determine the alignment width from the longest argument
auto paramLength = [](const Option& p) -> int { return p.argumentName
? static_cast<int>(std::strlen(p.argumentName) + 3)
auto paramLength = [](const Option& p) -> int { return !p.argumentName.empty()
? static_cast<int>(p.argumentName.length() + 3)
: 1; };
auto longestParam = std::max_element(options.begin(), options.end(),
[=](auto a, auto b) -> bool { return paramLength(a) < paramLength(b); });
Expand All @@ -67,7 +67,8 @@ void ArgParse::Options::ShowHelpUsage(const std::string_view name, std::ostream&
{
auto decorateArgument = [=] { return " <" + std::string(it.argumentName) + "> "; };
out << " -" << it.flag
<< std::left << std::setw(alignWidth) << std::setfill('-') << (it.argumentName ? decorateArgument() : " ")
<< std::left << std::setw(alignWidth) << std::setfill('-')
<< (!it.argumentName.empty() ? decorateArgument() : " ")
<< " " << it.helpString << std::endl;
}
out << std::flush;
Expand Down Expand Up @@ -99,7 +100,7 @@ ArgParse::ParseCtrl ArgParse::ParserState::Next(const std::string_view token)
const auto opt = getOption(flagChar);
if (opt.has_value())
{
bool expect = opt.value().get().argumentName != nullptr;
bool expect = !opt.value().get().argumentName.empty();
if (token.length() <= 2)
{
expectArg = expect;
Expand Down
8 changes: 4 additions & 4 deletions src/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ namespace ArgParse
{
struct Option
{
const char* argumentName;
const char* helpString;
std::string_view argumentName;
std::string_view helpString;
char flag;
bool required;

static constexpr Option Optional(char flag, const char* name, const char* help)
static constexpr Option Optional(char flag, const std::string_view name, const std::string_view help)
{
return { name, help, flag, false };
}
static constexpr Option Required(char flag, const char* name, const char* help)
static constexpr Option Required(char flag, const std::string_view name, const std::string_view help)
{
return { name, help, flag, true };
}
Expand Down
4 changes: 2 additions & 2 deletions src/tmx2gba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ using ArgParse::Option;

static const ArgParse::Options options =
{
Option::Optional('h', nullptr, "Display this help & command info"),
Option::Optional('v', nullptr, "Display version & quit"),
Option::Optional('h', {}, "Display this help & command info"),
Option::Optional('v', {}, "Display version & quit"),
Option::Optional('l', "name", "Name of layer to use (default first layer in TMX)"),
Option::Optional('y', "name", "Layer for palette mappings"),
Option::Optional('c', "name", "Output a separate 8bit collision map of the specified layer"),
Expand Down

0 comments on commit f9928df

Please sign in to comment.