fix(command_line): Filter positional args
This commit is contained in:
parent
e1483c3f65
commit
5ad0081e76
2 changed files with 9 additions and 6 deletions
|
@ -86,7 +86,7 @@ namespace command_line {
|
||||||
* Test if a positional argument is defined at given index
|
* Test if a positional argument is defined at given index
|
||||||
*/
|
*/
|
||||||
bool parser::has(size_t index) const {
|
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
|
* Get the positional argument at given index
|
||||||
*/
|
*/
|
||||||
string parser::get(size_t index) const {
|
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;
|
string value;
|
||||||
|
|
||||||
if (input_next.empty() && opt.compare(0, 2, "--") != 0) {
|
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) {
|
} 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()) {
|
} else if (pos == string::npos && !input_next.empty()) {
|
||||||
value = input_next;
|
value = input_next;
|
||||||
} else {
|
} else {
|
||||||
|
@ -171,6 +171,7 @@ namespace command_line {
|
||||||
* Parse and validate passed arguments and flags
|
* Parse and validate passed arguments and flags
|
||||||
*/
|
*/
|
||||||
void parser::parse(const string& input, const string& input_next) {
|
void parser::parse(const string& input, const string& input_next) {
|
||||||
|
auto skipped = m_skipnext;
|
||||||
if (m_skipnext) {
|
if (m_skipnext) {
|
||||||
m_skipnext = false;
|
m_skipnext = false;
|
||||||
if (!input_next.empty()) {
|
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);
|
m_posargs.emplace_back(input);
|
||||||
} else {
|
} else {
|
||||||
throw argument_error("Unrecognized option " + input);
|
throw argument_error("Unrecognized option " + input);
|
||||||
|
|
|
@ -97,7 +97,7 @@ int main(int argc, char** argv) {
|
||||||
string confpath;
|
string confpath;
|
||||||
|
|
||||||
// Make sure a bar name is passed in
|
// Make sure a bar name is passed in
|
||||||
if (!cli->has(0)) {
|
if (!cli->has(1)) {
|
||||||
cli->usage();
|
cli->usage();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue