Commit 9d634955 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Removing the radixio implementation of commandline.

parent a64b83a4
Pipeline #13535 passed with stages
in 7 minutes and 45 seconds
......@@ -2,7 +2,6 @@ TRIBITS_SUBPACKAGE(io)
SET(SOURCE
commandline.cc
csvfile.cc
cfgfile.cc
eafstream.cc
......@@ -15,8 +14,6 @@ SET(SOURCE
)
SET(HEADERS
commandline.hh
commandline.i.hh
csvfile.hh
cfgfile.hh
eafstream.hh
......
/*
* @file: commandline.cc
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*
* Created on July 3, 2013, 2:49 PM
*/
#include <cstdlib>
#include <iostream>
#include "radixio/commandline.hh"
namespace radix
{
CommandLine::CommandLine(int argc, char **argv)
: mArgc(argc)
, mArgv(argv)
{
this->mExecutable = std::string(argv[0]);
for (int i = 1; i < argc; i++)
{
std::string temp = std::string(argv[i]);
//
// Check for '-' as first character
//
if (temp.size() > 0)
{
if (temp.at(0) == '-')
{
//
// Check for flag=value
//
size_t index = temp.find('=');
if (index == std::string::npos) // we didn't find equal sign
{
//
// This is just a flag with empty value
//
this->mFlagToValues[temp] = std::string("");
}
else
{
//
// We found equal sign, so we must divide into flag and value
//
std::string flag = temp.substr(0, index);
//
// Check to make sure empty string wasn't provided
// flag=''
std::string value = "";
if (temp.size() - 1 != index)
{
value = temp.substr(index + 1, temp.size() - 1);
}
this->mFlagToValues.insert(std::make_pair(flag, value));
} // else
} // if = '-'
else
{
mArgs.push_back(temp);
}
} // if size > 0
} // loop
} // constructor
CommandLine::CommandLine(const CommandLine &orig)
: mArgc(orig.mArgc)
, mArgv(orig.mArgv)
, mFlagToValues(orig.mFlagToValues)
, mArgs(orig.mArgs)
{
}
CommandLine::~CommandLine() {}
std::string CommandLine::toString() const
{
std::stringstream ss;
ss << "executable: " << executable() << std::endl;
ss << "options: " << std::endl;
std::map<std::string, std::string>::const_iterator iter;
for (iter = mFlagToValues.begin(); iter != mFlagToValues.end(); ++iter)
{
ss << iter->first << "=" << iter->second << std::endl;
}
ss << "arguments: " << std::endl;
const std::vector<std::string> &args = arguments();
for (size_t i = 0; i < args.size(); ++i)
{
ss << (i + 1) << ". " << args[i] << std::endl;
}
return ss.str();
}
bool CommandLine::exists(std::string flag) const
{
std::map<std::string, std::string>::const_iterator iter;
iter = mFlagToValues.find(flag);
if (iter == mFlagToValues.end()) return false;
return true;
} // exists
const std::vector<std::string> &CommandLine::arguments() const
{
return mArgs;
} // arguments
int CommandLine::valueAsInt(std::string flag) const
{
std::map<std::string, std::string>::const_iterator iter;
int result = 0;
iter = this->mFlagToValues.find(flag);
if (iter == this->mFlagToValues.end()) return -1;
try
{
result = std::atoi(iter->second.c_str());
}
catch (...)
{
return -1;
}
return result;
} // value
float CommandLine::valueAsFloat(std::string flag) const
{
std::map<std::string, std::string>::const_iterator iter;
float result = 0.0f;
iter = this->mFlagToValues.find(flag);
if (iter == this->mFlagToValues.end()) return -1;
try
{
result = std::atof(iter->second.c_str());
}
catch (...)
{
return -1.0f;
}
return result;
} // valueAsFloat
std::string CommandLine::value(std::string flag) const
{
std::map<std::string, std::string>::const_iterator iter;
iter = this->mFlagToValues.find(flag);
if (iter == this->mFlagToValues.end()) return "";
return iter->second;
} // value
std::string CommandLine::value(std::string flag,
std::string default_value) const
{
std::map<std::string, std::string>::const_iterator iter;
iter = this->mFlagToValues.find(flag);
if (iter == this->mFlagToValues.end())
{
return default_value;
}
else
{
return iter->second;
}
} // value
std::string CommandLine::executable() const { return this->mExecutable; }
// specialize boolean so we can handle yes/true/on and no/false/off
template <>
bool CommandLine::valueAs(std::string flag, bool default_value) const
{
if (exists(flag))
{
std::string sv = value(flag);
if (sv.compare("yes") == 0 || sv.compare("true") == 0 ||
sv.compare("on") == 0 || sv.compare("1") == 0)
return true;
if (sv.compare("no") == 0 || sv.compare("false") == 0 ||
sv.compare("off") == 0 || sv.compare("0") == 0)
return false;
}
return default_value;
}
} // namespace radix
/*
* @file: commandline.hh
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*
* Created on July 3, 2013, 2:49 PM
*/
#ifndef RADIX_RADIXIO_COMMANDLINE_HH_
#define RADIX_RADIXIO_COMMANDLINE_HH_
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "radixcore/visibility.hh"
namespace radix
{
/**
* @class CommandLine
* @brief Simple class designed to decipher commandline arguments
* Provides ability to check flag existences.
* Provides ability to get flag=value options
* Flags are designated with '-' prefix
* All non flag prefixed values are stored as arguments
*/
class RADIX_PUBLIC CommandLine
{
public:
CommandLine(int argc, char **argv);
CommandLine(const CommandLine &orig);
virtual ~CommandLine();
/**
* Does the flag exist
* @param flag
* @return
*/
virtual bool exists(std::string flag) const;
/**
* @brief Get the value of flag as int
* @param flag std::string
* @return int
*/
int valueAsInt(std::string flag) const;
/**
* @brief Get the value of flag as float
* @param flag std::string
* @return float
*/
float valueAsFloat(std::string flag) const;
/**
* @brief Get the value of flag as std::string
* @param flag std::string
* @return std::string
*/
std::string value(std::string flag) const;
std::string value(std::string flag, std::string default_value) const;
/**
* @brief Templated get the value of flag with default
* @param flag std::string
* @param default_value T
* @return T
*/
template <class T>
T valueAs(std::string flag, T default_value) const;
/**
* @brief Get arguments on the commandline which were not flag=value
* @return const std::vector<std::string>&
*/
virtual const std::vector<std::string> &arguments() const;
/**
* @brief Get the executable path
* @return std::string
*/
std::string executable() const;
/**
* @brief Get the contents as a string
* @return std::string
*/
std::string toString() const;
/**
* @brief argc inline argc()
* @return int number of arguments
*/
int argc() const { return mArgc; }
/**
* @brief argv inline argv()
* @return char array of commandline arguments
*/
char **argv() const { return mArgv; }
private:
int mArgc;
char **mArgv;
/** Executable path*/
std::string mExecutable;
/** Flag to values map*/
std::map<std::string, std::string> mFlagToValues;
/** List of arguments which were not flag=value*/
std::vector<std::string> mArgs;
};
} // namespace radix
#include "radixio/commandline.i.hh"
#endif /* RADIX_RADIXIO_COMMANDLINE_HH_ */
#include <sstream>
namespace radix
{
template <class T>
T CommandLine::valueAs(std::string flag, T default_value) const
{
T tvalue;
if (exists(flag) && std::istringstream(value(flag)) >> tvalue)
{
return tvalue;
}
else
{
return default_value;
}
}
} // namespace radix
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment