bash completion: Fix case when no current word index specified
This commit is contained in:
parent
e4709099d2
commit
d7cf4312bf
|
@ -683,12 +683,12 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
|
||||||
// the first argument after "--bash-completion-for" is the index of the current word
|
// the first argument after "--bash-completion-for" is the index of the current word
|
||||||
try {
|
try {
|
||||||
currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
|
currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
|
||||||
} catch (const ConversionException &) {
|
|
||||||
currentWordIndex = static_cast<unsigned int>(argc);
|
|
||||||
}
|
|
||||||
if (argc) {
|
if (argc) {
|
||||||
++argv, --argc;
|
++argv, --argc;
|
||||||
}
|
}
|
||||||
|
} catch (const ConversionException &) {
|
||||||
|
currentWordIndex = static_cast<unsigned int>(argc - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// read specified arguments
|
// read specified arguments
|
||||||
|
|
|
@ -599,6 +599,28 @@ void ArgumentParserTests::testBashCompletion()
|
||||||
cout.rdbuf(regularCoutBuffer);
|
cout.rdbuf(regularCoutBuffer);
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<string::size_type>(0), buffer.str().find("COMPREPLY=('--fields' "));
|
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 (...) {
|
} catch (...) {
|
||||||
cout.rdbuf(regularCoutBuffer);
|
cout.rdbuf(regularCoutBuffer);
|
||||||
throw;
|
throw;
|
||||||
|
|
Loading…
Reference in New Issue