Skip to content

Commit

Permalink
Document CliOptions class.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Feb 22, 2024
1 parent ec2411a commit d5188a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
23 changes: 13 additions & 10 deletions resolve/utilities/params/CliOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ReSolve
: argc_(argc),
argv_(argv)
{
appName_ = argv_[0];
app_name_ = argv_[0];
parse();
}

Expand All @@ -20,7 +20,7 @@ namespace ReSolve

std::string CliOptions::getAppName() const
{
return appName_;
return app_name_;
}

bool CliOptions::hasKey(const std::string& key) const
Expand All @@ -30,17 +30,17 @@ namespace ReSolve

CliOptions::Option* CliOptions::getParamFromKey(const std::string& key) const
{
const Options::const_iterator i = options_.find(key);
const OptionsList::const_iterator i = options_.find(key);
CliOptions::Option* opt = 0;
if (i != options_.end()) {
opt = new CliOptions::Option((*i).first, (*i).second);
}
return opt;
}

void CliOptions::printOptions() const
void CliOptions::printOptionsList() const
{
Options::const_iterator m = options_.begin();
OptionsList::const_iterator m = options_.begin();
int i = 0;
if (options_.empty()) {
std::cout << "No parameters\n";
Expand All @@ -59,6 +59,8 @@ namespace ReSolve
/**
* @brief Parse command line input and store it in a map
*
* @pre Pointer to command line options is copied to `argv_`
* @post Command line options are parsed and stored in map `options_`
*/
void CliOptions::parse()
{
Expand All @@ -69,7 +71,7 @@ namespace ReSolve
const std::string p = *i;
if (option->first == "" && p[0] == '-')
{
// Set option ID
// Set option ID and continue
option->first = p;
if (i == this->last())
{
Expand All @@ -81,10 +83,11 @@ namespace ReSolve
else if (option->first != "" && p[0] == '-')
{
// Option ID has been set in prior cycle, string p is also option ID.
option->second = "null"; /* or leave empty? */
// Leave option value empty, since what follows is the next option ID.
option->second = "";
// Set option without parameter value
options_.insert(Option(option->first, option->second));
// Set parameter ID for the next option
// Set parameter ID for the next option and continue
option->first = p;
option->second = "";
if (i == this->last())
Expand All @@ -96,11 +99,11 @@ namespace ReSolve
}
else if (option->first != "")
{
// String p contains parameter value
// String p does not start with '-', contains parameter value
option->second = p;
// Set option with parameter value
options_.insert(Option(option->first, option->second));
// Reset option to receive the next entry
// Reset 'option' pair to receive the next entry and continue
option->first = "";
option->second = "";
continue;
Expand Down
17 changes: 9 additions & 8 deletions resolve/utilities/params/CliOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@ namespace ReSolve
/**
* @brief Parser for command line input
*
* @note Based on StackOverflow answer by Luca Davanzo
*/
class CliOptions
{
public:
using Option = std::pair<std::string, std::string>;
CliOptions(int argc, char *argv[]);
CliOptions(int argc, char* argv[]);
virtual ~CliOptions();
std::string getAppName() const;
bool hasKey(const std::string&) const;
Option* getParamFromKey(const std::string&) const;
void printOptions() const;
void printOptionsList() const;
private:
using Options = std::map<std::string, std::string>;
using OptionsList = std::map<std::string, std::string>;
void parse();
const char* const *begin() const;
const char* const *end() const;
const char* const *last() const;
Options options_;
const char* const* begin() const;
const char* const* end() const;
const char* const* last() const;
OptionsList options_;
int argc_;
char** argv_;
std::string appName_;
std::string app_name_;
};

} // namespace ReSolve

0 comments on commit d5188a0

Please sign in to comment.