Implemented abbreviations
This commit is contained in:
parent
c5d9a0b871
commit
a3520f555d
BIN
Hazelnupp.vpp
BIN
Hazelnupp.vpp
Binary file not shown.
@ -23,6 +23,8 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
|
||||
// Populate raw arguments
|
||||
PopulateRawArgs(argc, argv);
|
||||
|
||||
// Expand abbreviations
|
||||
ExpandAbbreviations();
|
||||
|
||||
executableName = std::string(rawArgs[0]);
|
||||
|
||||
@ -79,6 +81,22 @@ void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv)
|
||||
return;
|
||||
}
|
||||
|
||||
void Hazelnupp::ExpandAbbreviations()
|
||||
{
|
||||
for (std::string& arg : rawArgs)
|
||||
{
|
||||
// Is arg registered as an abbreviation?
|
||||
auto abbr = abbreviations.find(arg);
|
||||
if (abbr != abbreviations.end())
|
||||
{
|
||||
// Yes: replace arg with the long form
|
||||
arg = abbr->second;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool Hazelnupp::HasParam(const std::string& key) const
|
||||
{
|
||||
return parameters.find(key) != parameters.end();
|
||||
@ -135,7 +153,23 @@ const std::string& Hazelnupp::GetExecutableName() const
|
||||
return executableName;
|
||||
}
|
||||
|
||||
const Value* Hazelnupp::operator[](const std::string& key)
|
||||
const Value* Hazelnupp::operator[](const std::string& key) const
|
||||
{
|
||||
return parameters[key]->Value();
|
||||
return parameters.find(key)->second->Value();
|
||||
}
|
||||
|
||||
void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::string& target)
|
||||
{
|
||||
abbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const
|
||||
{
|
||||
return abbreviations.find(abbrev)->second;
|
||||
}
|
||||
|
||||
bool Hazelnupp::HasAbbreviation(const std::string& abbrev) const
|
||||
{
|
||||
return abbreviations.find(abbrev) != abbreviations.end();
|
||||
}
|
||||
|
@ -16,15 +16,28 @@ public:
|
||||
const std::string& GetExecutableName() const;
|
||||
|
||||
//! Will return the value given a key
|
||||
const Value* operator[](const std::string& key);
|
||||
const Value* operator[](const std::string& key) const;
|
||||
|
||||
//! Will check wether a parameter exists given a key, or not
|
||||
bool HasParam(const std::string& key) const;
|
||||
|
||||
// Abbreviations
|
||||
//! Will register an abbreviation (like -f for --force)
|
||||
void RegisterAbbreviation(const std::string& abbrev, const std::string& target);
|
||||
|
||||
//! Will return the long form of an abbreviation (like --force for -f)
|
||||
const std::string& GetAbbreviation(const std::string& abbrev) const;
|
||||
|
||||
//! Will check wether or not an abbreviation is registered
|
||||
bool HasAbbreviation(const std::string& abbrev) const;
|
||||
|
||||
private:
|
||||
//! Will translate the c-like args to an std::vector
|
||||
void PopulateRawArgs(const int argc, const char* const* argv);
|
||||
|
||||
//! Will replace all args matching an abbreviation with their long form (like -f for --force)
|
||||
void ExpandAbbreviations();
|
||||
|
||||
//! Will parse the next parameter. Returns the index of the next parameter.
|
||||
std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
|
||||
|
||||
@ -34,6 +47,9 @@ private:
|
||||
std::string executableName; //! The path of the executable. Always argv[0]
|
||||
std::unordered_map<std::string, Parameter*> parameters;
|
||||
|
||||
// These are abbreviations. Like, -f for --force.
|
||||
std::unordered_map<std::string, std::string> abbreviations;
|
||||
|
||||
std::vector<std::string> rawArgs;
|
||||
|
||||
// EvaluateValues
|
||||
|
@ -6,7 +6,7 @@
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
std::string arg0 = "meinpfad";
|
||||
std::string arg1 = "--word";
|
||||
std::string arg1 = "-w";
|
||||
std::string arg2 = "6669";
|
||||
std::string arg3 = "--alfred";
|
||||
std::string arg4 = "banane7";
|
||||
@ -19,11 +19,22 @@ int main(int argc, char** argv)
|
||||
arg4.data()
|
||||
};
|
||||
|
||||
Hazelnupp args(testArgv.size(), testArgv.data());
|
||||
//Hazelnupp args(argc, argv);
|
||||
Hazelnupp args;
|
||||
|
||||
args.RegisterAbbreviation("-w", "--word");
|
||||
|
||||
//args.Parse(testArgv.size(), testArgv.data());
|
||||
args.Parse(argc, argv);
|
||||
|
||||
if (args.HasParam("--word"))
|
||||
{
|
||||
int i = *(IntValue*)args["--word"];
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "No --word!" << std::endl;
|
||||
}
|
||||
|
||||
//std::cout << args.GetExecutableName() << std::endl;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user