Ignore file system errors when printing bash completion
This commit is contained in:
parent
0f56d25251
commit
eadb261c56
|
@ -1508,44 +1508,47 @@ void ArgumentParser::printBashCompletion(int argc, const char *const *argv, unsi
|
||||||
// -> completion for files and dirs
|
// -> completion for files and dirs
|
||||||
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
|
#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
|
||||||
if (completionInfo.completeFiles || completionInfo.completeDirs) {
|
if (completionInfo.completeFiles || completionInfo.completeDirs) {
|
||||||
using namespace std::filesystem;
|
try {
|
||||||
const auto replace = "'"s, with = "'\"'\"'"s;
|
const auto replace = "'"s, with = "'\"'\"'"s;
|
||||||
const auto useActualDir = argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening;
|
const auto useActualDir = argc && currentWordIndex <= completionInfo.lastSpecifiedArgIndex && opening;
|
||||||
const auto dirEntries = [&] {
|
const auto dirEntries = [&] {
|
||||||
directory_iterator i;
|
filesystem::directory_iterator i;
|
||||||
if (useActualDir) {
|
if (useActualDir) {
|
||||||
i = directory_iterator(actualDir);
|
i = filesystem::directory_iterator(actualDir);
|
||||||
findAndReplace(actualDir, replace, with);
|
findAndReplace(actualDir, replace, with);
|
||||||
} else {
|
} else {
|
||||||
i = directory_iterator(".");
|
i = filesystem::directory_iterator(".");
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}();
|
}();
|
||||||
for (const auto &dirEntry : dirEntries) {
|
for (const auto &dirEntry : dirEntries) {
|
||||||
if (!completionInfo.completeDirs && dirEntry.is_directory()) {
|
if (!completionInfo.completeDirs && dirEntry.is_directory()) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!completionInfo.completeFiles && !dirEntry.is_directory()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
auto dirEntryName = dirEntry.path().filename().string();
|
|
||||||
auto hasStartingQuote = false;
|
|
||||||
if (useActualDir) {
|
|
||||||
if (!startsWith(dirEntryName, actualFile)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cout << '\'';
|
if (!completionInfo.completeFiles && !dirEntry.is_directory()) {
|
||||||
hasStartingQuote = true;
|
continue;
|
||||||
if (actualDir != ".") {
|
|
||||||
cout << actualDir;
|
|
||||||
}
|
}
|
||||||
|
auto dirEntryName = dirEntry.path().filename().string();
|
||||||
|
auto hasStartingQuote = false;
|
||||||
|
if (useActualDir) {
|
||||||
|
if (!startsWith(dirEntryName, actualFile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cout << '\'';
|
||||||
|
hasStartingQuote = true;
|
||||||
|
if (actualDir != ".") {
|
||||||
|
cout << actualDir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
findAndReplace(dirEntryName, replace, with);
|
||||||
|
if (!hasStartingQuote) {
|
||||||
|
cout << '\'';
|
||||||
|
}
|
||||||
|
cout << dirEntryName << '\'' << ' ';
|
||||||
|
haveFileOrDirCompletions = true;
|
||||||
}
|
}
|
||||||
findAndReplace(dirEntryName, replace, with);
|
} catch (const filesystem::filesystem_error &) {
|
||||||
if (!hasStartingQuote) {
|
// ignore filesystem errors; there's no good way to report errors when printing bash completion
|
||||||
cout << '\'';
|
|
||||||
}
|
|
||||||
cout << dirEntryName << '\'' << ' ';
|
|
||||||
haveFileOrDirCompletions = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue