Validate interactive parameter values after they are entered
See original GitHub issueSummary of the new feature/enhancement
Parameters used interactively should be validated immediately and allow/require the user to provide valid input. That is what the user must do anyway.
Proposed technical implementation details (optional)
It would be helpful if parameter validation would lead the interactive user to success sooner rather than later.
The following script requuires a two (2) valid paths. If the paths are entered interactively, the user does not know of a failed validation until after ALL parameters are entered.
PS C:\src\t> Get-Content -Path '.\parammessage.ps1'
[cmdletbinding()]
Param (
[Parameter(Mandatory=$true, HelpMessage='Enter a valid path 1')]
[ValidateScript({ Test-Path $_ })]
[string]$Path1
,[Parameter(Mandatory=$true, HelpMessage='Enter a valid path 2')]
[ValidateScript({ Test-Path $_ })]
[string]$Path2
)
Write-Host "+++$Path1==="
Write-Host "+++$Path2==="
PS C:\src\t> ($PSVersionTable.PSVersion).ToString()
7.0.0-preview.3
PS C:\src\t> .\parammessage.ps1
cmdlet parammessage.ps1 at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Path1: asdf
Path2: C:\
C:\src\t\parammessage.ps1 : Cannot validate argument on parameter 'Path1'. The " Test-Path $_ " validation script for the argument with value "asdf" did not return a result of True. Determine why the validation script failed, and then try the command again.
At line:1 char:1
+ .\parammessage.ps1
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [parammessage.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,parammessage.ps1
What I would like to see is the interactive prompt use the HelpMessage specified and give the user an opportunity to enter valid information if needed.
PS C:\src\t> .\parammessage.ps1
cmdlet parammessage.ps1 at command pipeline position 1
Enter a valid path 1
Path1: asdf
Entry is not valid.
Enter a valid path 1
Path1:
If the entry for Path1 is not valid, the script will fail. I do not see benefit in requiring ALL parameters to be entered before informing the user that one will not pass validation.
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (3 by maintainers)
Many thanks to @KirkMunro and @iSazonov. I finally got to where I could build and have found my way down to .\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHostUserInterfacePrompt.cs.
So you mean when a command has mandatory parameters that are not provided by a user, resulting in individual prompts for the mandatory parameters, PowerShell should perform parameter validation on the values entered at the prompt when they are entered, and re-prompt the user for the parameter if they provided a value that is invalid, to guide users toward a path of successful invocation? That makes sense to me.