bash completion: Fix case when no current word index specified

This commit is contained in:
Martchus 2017-07-28 18:24:52 +02:00
parent e4709099d2
commit d7cf4312bf
2 changed files with 26 additions and 4 deletions

View File

@ -683,11 +683,11 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
// the first argument after "--bash-completion-for" is the index of the current word
try {
currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
if (argc) {
++argv, --argc;
}
} catch (const ConversionException &) {
currentWordIndex = static_cast<unsigned int>(argc);
}
if (argc) {
++argv, --argc;
currentWordIndex = static_cast<unsigned int>(argc - 1);
}
}

View File

@ -599,6 +599,28 @@ void ArgumentParserTests::testBashCompletion()
cout.rdbuf(regularCoutBuffer);
CPPUNIT_ASSERT_EQUAL(static_cast<string::size_type>(0), buffer.str().find("COMPREPLY=('--fields' "));
// override exit function to prevent readArgs() from terminating the test run
exitFunction = [](int) {};
// call completion via readArgs() with current word index
const char *const argv10[] = { "/some/path/tageditor", "--bash-completion-for", "0" };
buffer.str(string());
cout.rdbuf(buffer.rdbuf());
parser.resetArgs();
parser.readArgs(3, argv10);
cout.rdbuf(regularCoutBuffer);
CPPUNIT_ASSERT(!strcmp("/some/path/tageditor", parser.executable()));
CPPUNIT_ASSERT_EQUAL("COMPREPLY=('display-file-info' 'get' 'set' '--help' )\n"s, buffer.str());
// call completion via readArgs() without current word index
const char *const argv11[] = { "/some/path/tageditor", "--bash-completion-for", "ge" };
buffer.str(string());
cout.rdbuf(buffer.rdbuf());
parser.resetArgs();
parser.readArgs(3, argv11);
cout.rdbuf(regularCoutBuffer);
CPPUNIT_ASSERT_EQUAL("COMPREPLY=('get' )\n"s, buffer.str());
} catch (...) {
cout.rdbuf(regularCoutBuffer);
throw;