Fix passing cursor position to findSuggestions()
This commit is contained in:
parent
5eb3e2aa75
commit
24720bbbc1
|
@ -938,9 +938,11 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the first argument (after executable name) is "--bash-completion-for", bash completion for the following arguments is requested
|
// check for completion mode: if first arg (after executable name) is "--bash-completion-for", bash completion for the following arguments is requested
|
||||||
bool completionMode = !strcmp(*++argv, "--bash-completion-for");
|
const bool completionMode = !strcmp(*++argv, "--bash-completion-for");
|
||||||
unsigned int currentWordIndex;
|
|
||||||
|
// determine the index of the current word for completion and the number of arguments to be passed to ArgumentReader
|
||||||
|
unsigned int currentWordIndex, argcForReader;
|
||||||
if (completionMode) {
|
if (completionMode) {
|
||||||
// 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 {
|
||||||
|
@ -951,21 +953,26 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
|
||||||
} catch (const ConversionException &) {
|
} catch (const ConversionException &) {
|
||||||
currentWordIndex = static_cast<unsigned int>(argc - 1);
|
currentWordIndex = static_cast<unsigned int>(argc - 1);
|
||||||
}
|
}
|
||||||
|
argcForReader = min(static_cast<unsigned int>(argc), currentWordIndex + 1);
|
||||||
|
} else {
|
||||||
|
argcForReader = static_cast<unsigned int>(argc);
|
||||||
}
|
}
|
||||||
|
|
||||||
// read specified arguments
|
// read specified arguments
|
||||||
ArgumentReader reader(*this, argv,
|
ArgumentReader reader(*this, argv, argv + argcForReader, completionMode);
|
||||||
argv + (completionMode ? min(static_cast<unsigned int>(argc), currentWordIndex + 1) : static_cast<unsigned int>(argc)), completionMode);
|
|
||||||
const bool allArgsProcessed(reader.read());
|
const bool allArgsProcessed(reader.read());
|
||||||
NoColorArgument::apply();
|
NoColorArgument::apply();
|
||||||
|
|
||||||
|
// fail when not all arguments could be processed, except when in completion mode
|
||||||
if (!completionMode && !allArgsProcessed) {
|
if (!completionMode && !allArgsProcessed) {
|
||||||
const auto suggestions(findSuggestions(argc, argv, currentWordIndex, reader));
|
const auto suggestions(findSuggestions(argc, argv, static_cast<unsigned int>(argc - 1), reader));
|
||||||
throw Failure(argsToString("The specified argument \"", *reader.argv, "\" is unknown.", suggestions));
|
throw Failure(argsToString("The specified argument \"", *reader.argv, "\" is unknown.", suggestions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print Bash completion and prevent the applicaton to continue with the regular execution
|
||||||
if (completionMode) {
|
if (completionMode) {
|
||||||
printBashCompletion(argc, argv, currentWordIndex, reader);
|
printBashCompletion(argc, argv, currentWordIndex, reader);
|
||||||
exitFunction(0); // prevent the applicaton to continue with the regular execution
|
exitFunction(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -199,6 +199,16 @@ void ArgumentParserTests::testParsing()
|
||||||
CPPUNIT_ASSERT_EQUAL("The specified argument \"album\" is unknown.\nDid you mean get or help?"s, string(e.what()));
|
CPPUNIT_ASSERT_EQUAL("The specified argument \"album\" is unknown.\nDid you mean get or help?"s, string(e.what()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// error about unknown argument: mistake in final argument
|
||||||
|
const char *argv18[] = { "tageditor", "get", "album", "title", "diskpos", "--verbose", "--fi" };
|
||||||
|
try {
|
||||||
|
parser.resetArgs();
|
||||||
|
parser.parseArgs(7, argv18);
|
||||||
|
CPPUNIT_FAIL("Exception expected.");
|
||||||
|
} catch (const Failure &e) {
|
||||||
|
CPPUNIT_ASSERT_EQUAL("The specified argument \"--fi\" is unknown.\nDid you mean files or no-color?"s, string(e.what()));
|
||||||
|
}
|
||||||
|
|
||||||
// warning about unknown argument
|
// warning about unknown argument
|
||||||
parser.setUnknownArgumentBehavior(UnknownArgumentBehavior::Warn);
|
parser.setUnknownArgumentBehavior(UnknownArgumentBehavior::Warn);
|
||||||
// redirect stderr to check whether warnings are printed correctly
|
// redirect stderr to check whether warnings are printed correctly
|
||||||
|
|
Loading…
Reference in New Issue