Skip to content
Snippets Groups Projects
Commit e05d94ec authored by Michael Davis's avatar Michael Davis
Browse files

[cta_frontend] Parses command line options

parent f196d8ad
No related branches found
No related tags found
No related merge requests found
......@@ -91,19 +91,38 @@ void CtaAdminCmd::parseOptions(int start, int argc, const char *const *const arg
{
for(int i = start; i < argc; ++i)
{
int opt_num = i-start;
cmd_val_t::const_iterator opt_it;
// Scan options for a match
for(opt_it = options.begin(); opt_it != options.end(); ++opt_it) {
// Special case of OPT_CMD type has an implicit key
if(opt_num-- == 0 && opt_it->is_cmd()) break;
if(*opt_it == argv[i]) break;
}
if(opt_it == options.end()) throwUsage(std::string("Invalid option: ") + argv[i]);
if(opt_it == options.end()) {
throwUsage(std::string("Invalid option: ") + argv[i]);
}
if((i += opt_it->num_params()) == argc) {
throwUsage(std::string(argv[i-1]) + " expects a parameter");
}
addOption(opt_it->get_key(), argv[i]);
}
}
void CtaAdminCmd::addOption(const std::string &key, const std::string &value)
{
std::cout << "Setting " << key << " = " << value << std::endl;
}
void CtaAdminCmd::throwUsage(const std::string &error_txt) const
{
std::stringstream help;
......
......@@ -40,6 +40,11 @@ private:
*/
void parseOptions(int start, int argc, const char *const *const argv, const cmd_val_t &options);
/*!
* Add a valid option to the protocol buffer
*/
void addOption(const std::string &key, const std::string &value);
/*!
* Throw an exception with usage help
*/
......
......@@ -58,6 +58,30 @@ public:
return option;
}
/*!
* Check if the option is a command.
*
* Commands take no key, only a value
*/
bool is_cmd() const { return m_type == OPT_CMD; }
/*!
* Check if the supplied key matches the option
*/
bool operator==(const std::string &option) const {
return option == m_short_opt || option == m_long_opt;
}
/*!
* Return the key for this option
*/
const std::string &get_key() const { return m_lookup_key; }
/*!
* Return the number of parameters expected after this option
*/
int num_params() const { return m_type == OPT_CMD || m_type == OPT_FLAG ? 0 : 1; }
/*!
* Return per-option help string
*/
......@@ -69,13 +93,6 @@ public:
return help;
}
/*!
* Check for a matching option string
*/
bool operator==(const std::string &option) const {
return option == m_short_opt || option == m_long_opt;
}
private:
option_t m_type; //!< Option type
std::string m_lookup_key; //!< Key to map option string to Protocol Buffer enum
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment