From d7cf4312bfaccc6cdabc89737d18e80dd939fc95 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 28 Jul 2017 18:24:52 +0200 Subject: [PATCH] bash completion: Fix case when no current word index specified --- application/argumentparser.cpp | 8 ++++---- tests/argumentparsertests.cpp | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/application/argumentparser.cpp b/application/argumentparser.cpp index fa72023..85a242f 100644 --- a/application/argumentparser.cpp +++ b/application/argumentparser.cpp @@ -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(*(++argv)) : 0); + if (argc) { + ++argv, --argc; + } } catch (const ConversionException &) { - currentWordIndex = static_cast(argc); - } - if (argc) { - ++argv, --argc; + currentWordIndex = static_cast(argc - 1); } } diff --git a/tests/argumentparsertests.cpp b/tests/argumentparsertests.cpp index 7beb63e..6b6cc3f 100644 --- a/tests/argumentparsertests.cpp +++ b/tests/argumentparsertests.cpp @@ -599,6 +599,28 @@ void ArgumentParserTests::testBashCompletion() cout.rdbuf(regularCoutBuffer); CPPUNIT_ASSERT_EQUAL(static_cast(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;