fix(command_line): Filter positional args

This commit is contained in:
Michael Carlberg 2017-01-20 02:30:05 +01:00
parent e1483c3f65
commit 5ad0081e76
2 changed files with 9 additions and 6 deletions

View File

@ -86,7 +86,7 @@ namespace command_line {
* Test if a positional argument is defined at given index
*/
bool parser::has(size_t index) const {
return m_posargs.size() > index;
return m_posargs.size() >= std::max(1_z, index);
}
/**
@ -103,7 +103,7 @@ namespace command_line {
* Get the positional argument at given index
*/
string parser::get(size_t index) const {
return index < m_posargs.size() ? m_posargs[index] : "";
return has(index) ? m_posargs[std::max(1_z, index) - 1] : "";
}
/**
@ -150,9 +150,9 @@ namespace command_line {
string value;
if (input_next.empty() && opt.compare(0, 2, "--") != 0) {
throw value_error("Undefined argument for option " + opt);
throw value_error("Missing argument for option " + opt);
} else if ((pos = opt.find('=')) == string::npos && opt.compare(0, 2, "--") == 0) {
throw value_error("Undefined argument for option " + opt);
throw value_error("Missing argument for option " + opt);
} else if (pos == string::npos && !input_next.empty()) {
value = input_next;
} else {
@ -171,6 +171,7 @@ namespace command_line {
* Parse and validate passed arguments and flags
*/
void parser::parse(const string& input, const string& input_next) {
auto skipped = m_skipnext;
if (m_skipnext) {
m_skipnext = false;
if (!input_next.empty()) {
@ -191,7 +192,9 @@ namespace command_line {
}
}
if (input[0] != '-') {
if (skipped) {
return;
} else if (input[0] != '-') {
m_posargs.emplace_back(input);
} else {
throw argument_error("Unrecognized option " + input);

View File

@ -97,7 +97,7 @@ int main(int argc, char** argv) {
string confpath;
// Make sure a bar name is passed in
if (!cli->has(0)) {
if (!cli->has(1)) {
cli->usage();
return EXIT_FAILURE;
}