-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First take on CLI parameter setting.
- Loading branch information
Showing
11 changed files
with
299 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
]] | ||
|
||
add_subdirectory(logger) | ||
add_subdirectory(params) | ||
add_subdirectory(version) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#[[ | ||
@brief Build ReSolve function that returns version at runtime | ||
@author Slaven Peles <[email protected]> | ||
]] | ||
|
||
set(Options_SRC | ||
CliOptions.cpp | ||
) | ||
|
||
set(Options_HEADER_INSTALL | ||
CliOptions.hpp | ||
) | ||
|
||
# Build shared library ReSolve | ||
add_library(resolve_options OBJECT ${Options_SRC}) | ||
set_property(TARGET resolve_options PROPERTY POSITION_INDEPENDENT_CODE ON) | ||
|
||
target_include_directories(resolve_options PUBLIC | ||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> | ||
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}> | ||
$<INSTALL_INTERFACE:include> | ||
) | ||
|
||
install(FILES ${Options_HEADER_INSTALL} DESTINATION include/resolve/utilities/options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include <iostream> | ||
|
||
#include "CliOptions.hpp" | ||
|
||
|
||
namespace ReSolve | ||
{ | ||
|
||
CliOptions::CliOptions(int argc, char* argv[]) | ||
: argc_(argc), | ||
argv_(argv) | ||
{ | ||
appName_ = argv_[0]; | ||
parse(); | ||
} | ||
|
||
CliOptions::~CliOptions() | ||
{ | ||
} | ||
|
||
std::string CliOptions::getAppName() const | ||
{ | ||
return appName_; | ||
} | ||
|
||
bool CliOptions::hasKey(const std::string& key) const | ||
{ | ||
return options_.find(key) != options_.end(); | ||
} | ||
|
||
CliOptions::Option* CliOptions::getParamFromKey(const std::string& key) const | ||
{ | ||
const Options::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 | ||
{ | ||
Options::const_iterator m = options_.begin(); | ||
int i = 0; | ||
if (options_.empty()) { | ||
std::cout << "No parameters\n"; | ||
} | ||
for (; m != options_.end(); m++, ++i) { | ||
std::cout << "Parameter [" << i << "] [" | ||
<< (*m).first << " " | ||
<< (*m).second << "]\n"; | ||
} | ||
} | ||
|
||
// | ||
// Private methods | ||
// | ||
|
||
/** | ||
* @brief Parse command line input and store it in a map | ||
* | ||
*/ | ||
void CliOptions::parse() | ||
{ | ||
Option* option = new std::pair<std::string, std::string>(); | ||
// Loop over argv entries skipping the first one (executable name) | ||
for (const char* const* i = this->begin() + 1; i != this->end(); i++) | ||
{ | ||
const std::string p = *i; | ||
if (option->first == "" && p[0] == '-') | ||
{ | ||
// Set option ID | ||
option->first = p; | ||
if (i == this->last()) | ||
{ | ||
// If this is last entry, there is nothing else to do; set option. | ||
options_.insert(Option(option->first, option->second)); | ||
} | ||
continue; | ||
} | ||
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? */ | ||
// Set option without parameter value | ||
options_.insert(Option(option->first, option->second)); | ||
// Set parameter ID for the next option | ||
option->first = p; | ||
option->second = ""; | ||
if (i == this->last()) | ||
{ | ||
// If this is last entry, there is nothing else to do; set option. | ||
options_.insert(Option(option->first, option->second)); | ||
} | ||
continue; | ||
} | ||
else if (option->first != "") | ||
{ | ||
// String p 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 | ||
option->first = ""; | ||
option->second = ""; | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
const char* const *CliOptions::begin() const | ||
{ | ||
return argv_; | ||
} | ||
|
||
const char* const *CliOptions::end() const | ||
{ | ||
return argv_ + argc_; | ||
} | ||
|
||
const char* const *CliOptions::last() const | ||
{ | ||
return argv_ + argc_ - 1; | ||
} | ||
|
||
|
||
} // namespace ReSolve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace ReSolve | ||
{ | ||
|
||
/** | ||
* @brief Parser for command line input | ||
* | ||
*/ | ||
class CliOptions | ||
{ | ||
public: | ||
using Option = std::pair<std::string, std::string>; | ||
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; | ||
private: | ||
using Options = 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_; | ||
int argc_; | ||
char** argv_; | ||
std::string appName_; | ||
}; | ||
|
||
} // namespace ReSolve |
Oops, something went wrong.