Adds command line arguments processor
parent
28da7de122
commit
6efe491453
@ -0,0 +1,123 @@
|
||||
/******************************************************************************
|
||||
* File - args.cpp
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/01 (y/m/d)
|
||||
* Mod Date - 2021/09/01 (y/m/d)
|
||||
* Description - Processes command line arguments.
|
||||
******************************************************************************/
|
||||
|
||||
#include "Args.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
|
||||
Args::Args(int argc, char** argv, char switch_char, std::vector<SwitchDesc>& switches)
|
||||
: mNumArgs(argc), mSwitchChar(switch_char)
|
||||
{
|
||||
for (int i = 0; i < switches.size(); i++)
|
||||
{
|
||||
mSwitchHasValue.insert( { switches[i].symbol, switches[i].expect_value } );
|
||||
}
|
||||
|
||||
for (int i = 0; i < mNumArgs; i++)
|
||||
{
|
||||
mArgs.push_back(std::string(argv[i]));
|
||||
}
|
||||
|
||||
Process();
|
||||
}
|
||||
|
||||
const std::vector<std::string>& Args::GetPositionalArgs() const
|
||||
{
|
||||
return mPositionalArgs;
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string>& Args::GetSwitchArgs() const
|
||||
{
|
||||
return mSwitchArgs;
|
||||
}
|
||||
|
||||
int Args::GetNumPositionalArgs() const
|
||||
{
|
||||
return mPositionalArgs.size();
|
||||
}
|
||||
|
||||
// Does no error checking.
|
||||
const std::string& Args::GetArgAtPosition(int pos) const
|
||||
{
|
||||
return mPositionalArgs[pos];
|
||||
}
|
||||
|
||||
bool Args::ContainsSwitch(std::string s) const
|
||||
{
|
||||
return mSwitchArgs.find(s) != mSwitchArgs.end();
|
||||
}
|
||||
|
||||
// Does no error checking.
|
||||
const std::string& Args::GetSwitchValue(std::string s) const
|
||||
{
|
||||
return mSwitchArgs.find(s)->second;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& Args::GetRawArgs() const
|
||||
{
|
||||
return mArgs;
|
||||
}
|
||||
|
||||
void Args::Process()
|
||||
{
|
||||
for (int i = 0; i < mNumArgs; i++)
|
||||
{
|
||||
bool isSwitch = (mArgs[i][0] == mSwitchChar);
|
||||
|
||||
if (isSwitch)
|
||||
{
|
||||
int curPos = 1;
|
||||
std::ostringstream iss;
|
||||
|
||||
// This is a switch need to store everything after the switch char
|
||||
while (curPos < mArgs[i].size())
|
||||
{
|
||||
iss << mArgs[i][curPos];
|
||||
curPos++;
|
||||
}
|
||||
|
||||
// Store the switch
|
||||
std::string skey = iss.str();
|
||||
std::string svalue = "";
|
||||
|
||||
// check that this is a valid switch
|
||||
auto siter = mSwitchHasValue.find(skey);
|
||||
if (siter != mSwitchHasValue.end())
|
||||
{
|
||||
// Need to also consume the next arg in the vector
|
||||
// if this switch has a value
|
||||
if (siter->second)
|
||||
{
|
||||
i++;
|
||||
curPos = 0;
|
||||
iss.str("");
|
||||
iss.clear();
|
||||
|
||||
while (curPos < mArgs[i].size())
|
||||
{
|
||||
iss << mArgs[i][curPos];
|
||||
curPos++;
|
||||
}
|
||||
|
||||
svalue = iss.str();
|
||||
}
|
||||
|
||||
mSwitchArgs.insert( { skey, svalue } );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mPositionalArgs.push_back(mArgs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
/******************************************************************************
|
||||
* File - args.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2021/09/01 (y/m/d)
|
||||
* Mod Date - 2021/09/01 (y/m/d)
|
||||
* Description - Processes command line arguments.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef ARGS_H_
|
||||
#define ARGS_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
class Args
|
||||
{
|
||||
public:
|
||||
|
||||
struct SwitchDesc
|
||||
{
|
||||
std::string symbol;
|
||||
bool expect_value;
|
||||
};
|
||||
|
||||
public:
|
||||
Args(int argc, char** argv, char switch_char, std::vector<SwitchDesc>& switches);
|
||||
|
||||
const std::vector<std::string>& GetPositionalArgs() const;
|
||||
const std::map<std::string, std::string>& GetSwitchArgs() const;
|
||||
|
||||
int GetNumPositionalArgs() const;
|
||||
const std::string& GetArgAtPosition(int pos) const;
|
||||
|
||||
bool ContainsSwitch(std::string) const;
|
||||
const std::string& GetSwitchValue(std::string) const;
|
||||
|
||||
const std::vector<std::string>& GetRawArgs() const;
|
||||
|
||||
private:
|
||||
std::vector<std::string> mPositionalArgs;
|
||||
std::map<std::string, std::string> mSwitchArgs;
|
||||
|
||||
std::map<std::string, bool> mSwitchHasValue;
|
||||
|
||||
// Raw args
|
||||
char mSwitchChar;
|
||||
int mNumArgs;
|
||||
std::vector<std::string> mArgs;
|
||||
|
||||
private: // Helpers
|
||||
void Process();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ARGS_H_
|
||||
Loading…
Reference in New Issue