Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
- I think this still needs rewriting internally, i.e. the header is fine
   - All parsing could/should be done in the CmdArgs ctor as the data is never changed afterwards
  • Loading branch information
visuve committed Jan 1, 2024
1 parent 2948e1c commit bcef98e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
66 changes: 35 additions & 31 deletions HackLib/CmdArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ std::type_index CmdArgs::TypeByKey(std::string_view key) const
return expectedArgument->Type;
}

std::any CmdArgs::Parse(std::type_index type, const std::string& value) const
{
if (type == typeid(std::filesystem::path))
{
return std::filesystem::path(value);
}
else if (type == typeid(double))
{
return std::stod(value);
}
else if (type == typeid(float))
{
return std::stof(value);
}
else if (type == typeid(int))
{
return std::stoi(value);
}
else if (type == typeid(std::string))
{
return value;
}

throw CmdArgs::Exception("Unsupported argument type requested", _usage);
}

std::any CmdArgs::ValueByKey(std::string_view key) const
{
std::type_index type = TypeByKey(key);
Expand All @@ -167,48 +193,26 @@ std::any CmdArgs::ValueByKey(std::string_view key) const
return true;
}

std::string value;

for (std::string_view providedArgument : _givenArguments)
for (std::string_view givenArgument : _givenArguments)
{
if (!providedArgument.starts_with(key))
if (!givenArgument.starts_with(key))
{
continue;
}

providedArgument.remove_prefix(key.size());
givenArgument.remove_prefix(key.size());

if (providedArgument.empty() || providedArgument.front() != L'=')
if (givenArgument.empty() || givenArgument.front() != L'=')
{
throw CmdArgs::Exception(
"Arguments with values should be passed with \'=\' sign", _usage);
}

providedArgument.remove_prefix(1);
value = providedArgument;
break;
givenArgument.remove_prefix(1);

return Parse(type, std::string(givenArgument));
}

if (type == typeid(std::filesystem::path))
{
return std::filesystem::path(value);
}
else if (type == typeid(double))
{
return std::stod(value);
}
else if (type == typeid(float))
{
return std::stof(value);
}
else if (type == typeid(int))
{
return std::stoi(value);
}
else if (type == typeid(std::string))
{
return value;
}

throw CmdArgs::Exception("Unsupported argument type requested", _usage);
// Should never reach here
throw CmdArgs::Exception("Missing value requested", _usage);
}
3 changes: 2 additions & 1 deletion HackLib/CmdArgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CmdArgs
private:
bool AppearsValid(std::string_view raw) const;
std::type_index TypeByKey(std::string_view key) const;
std::any ValueByKey(std::string_view key) const;
std::any Parse(std::type_index type, const std::string& value) const;
std::any ValueByKey(std::string_view key) const;

std::string _usage;
const std::vector<Argument> _expected;
Expand Down
11 changes: 11 additions & 0 deletions HackLibTests/CmdArgsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ TEST(CmdArgsTests, Missing)
{ "foo", typeid(std::nullopt), "Foos, not bars. Definetely not foobars" },
{ "bar", typeid(std::nullopt), "Bars, not foos. Definetely not barfoos" }
}), CmdArgs::Exception);

const CmdArgs args({ "alpha", "bravo" },
{
{ "alpha", typeid(std::nullopt), "A" },
{ "bravo", typeid(std::nullopt), "B" }
});

EXPECT_THROW(args.Value<bool>("charlie"), CmdArgs::Exception);
EXPECT_THROW(args.Value<int>("charlie"), CmdArgs::Exception);

EXPECT_TRUE(args.Value<bool>("charlie", true));
}

TEST(CmdArgsTests, ContainsValuedArgument)
Expand Down

0 comments on commit bcef98e

Please sign in to comment.