main: avoid crashing when aborting completions

To print completions lix created a Finally object containing the actual function,
so the function was executed by the destructor of the class.
Unfortunately  aborting autocomplete by sending a SIGINT signal
(i.E. by pressing C-c) leads to an exception, that finally cant return or eat,
when throwing its own exception.

To avoid crashing when using auto complete let the function "mainWrapped" execute
the autocomplete code directly before returning.
This avoids creating the "Finally" object and instead moves the codeblock next to
the check to return when "arg.completions" is called.

Change-Id: Id333a60ad43c6095e8866f6953af78d51fd43b64
This commit is contained in:
git@71rd.net
2025-05-26 16:44:44 +00:00
parent 826cfbc41b
commit e360cf3a28
+13 -17
View File
@@ -562,29 +562,25 @@ void mainWrapped(AsyncIoRoot & aio, int argc, char * * argv)
return;
}
Finally printCompletions([&]()
{
if (args.completions) {
switch (args.completions->type) {
case Completions::Type::Normal:
logger->cout("normal"); break;
case Completions::Type::Filenames:
logger->cout("filenames"); break;
case Completions::Type::Attrs:
logger->cout("attrs"); break;
}
for (auto & s : args.completions->completions)
logger->cout(s.completion + "\t" + trim(s.description));
}
});
try {
args.parseCmdline({argv + 1, argv + argc});
} catch (UsageError &) {
if (!args.helpRequested && !args.completions) throw;
}
if (args.completions) return;
if (args.completions) {
switch (args.completions->type) {
case Completions::Type::Normal:
logger->cout("normal"); break;
case Completions::Type::Filenames:
logger->cout("filenames"); break;
case Completions::Type::Attrs:
logger->cout("attrs"); break;
}
for (auto & s : args.completions->completions)
logger->cout(s.completion + "\t" + trim(s.description));
return;
}
if (args.helpRequested) {
std::vector<std::string> subcommand;