fix(main): Exit on unrecognized positional arg

This commit is contained in:
Michael Carlberg 2017-01-24 06:58:45 +01:00
parent d6a34717bf
commit 13633f715d
2 changed files with 7 additions and 3 deletions

View File

@ -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() >= std::max(1_z, index); return m_posargs.size() > 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 has(index) ? m_posargs[std::max(1_z, index) - 1] : ""; return has(index) ? m_posargs[index] : "";
} }
/** /**

View File

@ -98,7 +98,11 @@ 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(1)) { if (!cli->has(0)) {
cli->usage();
return EXIT_FAILURE;
} else if (cli->has(1)) {
fprintf(stderr, "Unrecognized argument \"%s\"\n", cli->get(1).c_str());
cli->usage(); cli->usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }