Improve exception if a (multi)selection prompt is used incorrectly

Before this commit, the selection prompt would throw an `InvalidOperationException` (Sequence contains no elements) and the multi selection prompt would throw an `ArgumentOutOfRangeException` (Index was out of range. Must be non-negative and less than the size of the collection.)

Both would occur because the prompts were never meant to be empty.
This commit is contained in:
Cédric Luthi
2024-09-10 14:01:32 +02:00
committed by Patrik Svensson
parent b470af11f7
commit c70a8b8fc5
3 changed files with 38 additions and 0 deletions

View File

@ -42,6 +42,11 @@ internal sealed class ListPrompt<T>
}
var nodes = tree.Traverse().ToList();
if (nodes.Count == 0)
{
throw new InvalidOperationException("Cannot show an empty selection prompt. Please call the AddChoice() method to configure the prompt.");
}
var state = new ListPromptState<T>(nodes, converter, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize), wrapAround, selectionMode, skipUnselectableItems, searchEnabled);
var hook = new ListPromptRenderHook<T>(_console, () => BuildRenderable(state));