Skip to content
Snippets Groups Projects
Commit 59846a51 authored by whh8b's avatar whh8b
Browse files

Fix bugs, add output options in ZiprOptions_t::Parse

1. Did not properly detect missing required values
for arguments

2. Could not handle parsing without a error and
warn IO handle.
parent a83f5d6b
No related branches found
No related tags found
No related merge requests found
...@@ -106,17 +106,20 @@ ZiprOptions_t::ZiprOptions_t(int argc, char **argv) { ...@@ -106,17 +106,20 @@ ZiprOptions_t::ZiprOptions_t(int argc, char **argv) {
} }
} }
bool ZiprOptions_t::Parse(ostream &error, ostream &warn) { bool ZiprOptions_t::Parse(ostream *error, ostream *warn) {
vector<string>::const_iterator it, it_end = m_arguments.end(); vector<string>::const_iterator it, it_end = m_arguments.end();
bool success = true;
for (it = m_arguments.begin(); it != it_end; it++) { for (it = m_arguments.begin(); it != it_end; it++) {
string ns, key, argument = *it; string ns, key, argument = *it;
string::size_type location = 0; string::size_type location = 0;
ZiprOptionsNamespace_t *option_ns; ZiprOptionsNamespace_t *option_ns;
ZiprOption_t *option_option; ZiprOption_t *option_option;
bool next_is_option_value = false;
if (0 != (location = argument.find_first_of("--"))) { if (0 != (location = argument.find_first_of("--"))) {
warn << "Warning: " << argument << " does not start with --" << endl; if (warn)
*warn << "Warning: " << argument << " does not start with --" << endl;
continue; continue;
} }
#if IMPLEMENTATION_DEBUG #if IMPLEMENTATION_DEBUG
...@@ -124,7 +127,8 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) { ...@@ -124,7 +127,8 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) {
#endif #endif
argument = argument.substr(location+2, string::npos); argument = argument.substr(location+2, string::npos);
if (string::npos == (location = argument.find_first_of(":"))) { if (string::npos == (location = argument.find_first_of(":"))) {
warn << "Warning: " << argument << " going in global namespace." << endl; if (warn)
*warn << "Warning: " << argument << " going in global namespace."<<endl;
ns = "global"; ns = "global";
location = -1; location = -1;
} else { } else {
...@@ -139,26 +143,39 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) { ...@@ -139,26 +143,39 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) {
cout << "key: " << key << endl; cout << "key: " << key << endl;
#endif #endif
if (!(option_ns = Namespace(ns))) { if (!(option_ns = Namespace(ns))) {
error << "Invalid namespace: " << ns << endl; if (error)
return false; *error << "Invalid namespace: " << ns << endl;
success = false;
continue;
//return false;
} }
if (!(option_option = option_ns->OptionByKey(key))) { if (!(option_option = option_ns->OptionByKey(key))) {
error << "Error: namespace " if (error)
<< ns *error << "Error: namespace "
<< " does not accept key " << ns
<< key << endl; << " does not accept key "
return false; << key << endl;
success = false;
continue;
//return false;
} }
/* /*
* By default, options need and take values. Some, though, * By default, options need and take values. Some, though,
* take values but don't need them. Finally, some neither * take values but don't need them. Finally, some neither
* take nor need values. * take nor need values.
*/ */
if (((it+1) != it_end) &&
(0 != (location = (*(it+1)).find_first_of("--")))) {
next_is_option_value = true;
}
if (option_option->NeedsValue()) { if (option_option->NeedsValue()) {
if ((it + 1) == it_end) if ((it + 1) == it_end || !next_is_option_value)
{ {
error << ns << ":" << key << " is missing value." << endl; if (error)
return false; *error << ns << ":" << key << " is missing value." << endl;
success = false;
continue;
//return false;
} }
option_option->SetValue(*(++it)); option_option->SetValue(*(++it));
} else if (option_option->TakesValue()) { } else if (option_option->TakesValue()) {
...@@ -167,8 +184,7 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) { ...@@ -167,8 +184,7 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) {
* If it does, we consider it the next option * If it does, we consider it the next option
* and not the value to the previous option. * and not the value to the previous option.
*/ */
if (((it+1) != it_end) && if (next_is_option_value) {
(0 != (location = (*(it+1)).find_first_of("--")))) {
option_option->SetValue(*(++it)); option_option->SetValue(*(++it));
} else { } else {
option_option->Set(); option_option->Set();
...@@ -177,7 +193,7 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) { ...@@ -177,7 +193,7 @@ bool ZiprOptions_t::Parse(ostream &error, ostream &warn) {
option_option->Set(); option_option->Set();
} }
} }
return true; return success;
} }
ZiprOptionsNamespace_t *ZiprOptions_t::Namespace(string ns) { ZiprOptionsNamespace_t *ZiprOptions_t::Namespace(string ns) {
......
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