ShaunLawrie / PwshSpectreConsole

👻 PwshSpectreConsole is a PowerShell wrapper for the awesome Spectre.Console library
https://pwshspectreconsole.com/
MIT License
117 stars 7 forks source link

[BUG] Read-SpectreMultiSelection throws an InvalidOperation when invoked. #18

Closed futuremotiondev closed 10 months ago

futuremotiondev commented 11 months ago

Hopefully you can reproduce.

Clear-Host

$readSpectreMultiSelectionSplat = @{
    Title = "Select versions of Node.js you wish to install to."
    Choices = "21.5.0", "21.3.0", "20.10.0", "19.9.0", "18.19.0"
    Color = "#8e95ff"
    PageSize = 6
}

Read-SpectreMultiSelection @readSpectreMultiSelectionSplat

When I run, I get:

InvalidOperation: C:\Users\futur\Documents\PowerShell\Modules\PwshSpectreConsole\1.4.1\public\prompts\Read-SpectreMultiSelection.ps1:58:54.1\public\prompts\Read-SpectreMultiSelection.ps1:58:5
Line |
  58 |      $spectrePrompt.AllowEmpty = $AllowEmpty
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                
     | The property 'AllowEmpty' cannot be found on this object. Verify that the property exists and can be set.

Code_JhaUalKJCk

futuremotiondev commented 11 months ago

OK, I think I figured out what you were trying to do, and I can fix it if you want.

The variable $spectrePrompt is of type [Spectre.Console.MultiSelectionPromptExtensions]

You can get a lowdown on the class here: https://spectreconsole.net/api/spectre.console/multiselectionpromptextensions/

Specifically these properties: https://spectreconsole.net/api/spectre.console/multiselectionpromptextensions/7a395d1e https://spectreconsole.net/api/spectre.console/multiselectionpromptextensions/99e03267

What you want is:

$spectrePrompt.Required = -not $AllowNoChoice

or

$spectrePrompt.Required = $RequireAChoice

It just comes down to how you want to word it. And then obviously you need to change the param from [switch] $AllowEmpty to [switch] $RequireAChoice

If you want I can write a quick PR.

ShaunLawrie commented 10 months ago

Fixed in https://github.com/ShaunLawrie/PwshSpectreConsole/pull/19