Skip to content

Commit 92853b9

Browse files
Adding example for command validation handler in PSRL docs (#10283)
* Adding example for command validation handler in PSRL docs * Editorial changes --------- Co-authored-by: Sean Wheeler <[email protected]>
1 parent 00ee10d commit 92853b9

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

reference/5.1/PSReadLine/Set-PSReadLineOption.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadline
5-
ms.date: 07/11/2023
5+
ms.date: 07/19/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -169,6 +169,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
169169
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
170170
handler returns `$true` and PSReadLine saves the command in history.
171171

172+
### Example 8: Use CommandValidationHandler to validate a command before its executed
173+
174+
This example shows how to use the **CommandValidationHandler** parameter to run a validate a command
175+
before it's executed. The example specifically checks for the command `git` with the sub command
176+
`cmt` and replaces that with the full name `commit`. This way you can create shorthand aliases for
177+
subcommands.
178+
179+
```powershell
180+
# Load the namespace so you can use the [CommandAst] object type
181+
using namespace System.Management.Automation.Language
182+
183+
Set-PSReadLineOption -CommandValidationHandler {
184+
param([CommandAst]$CommandAst)
185+
186+
switch ($CommandAst.GetCommandName()) {
187+
'git' {
188+
$gitCmd = $CommandAst.CommandElements[1].Extent
189+
switch ($gitCmd.Text) {
190+
'cmt' {
191+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
192+
$gitCmd.StartOffset, $gitCmd.EndOffset - $gitCmd.StartOffset, 'commit')
193+
}
194+
}
195+
}
196+
}
197+
}
198+
# This checks the validation script when you hit enter
199+
Set-PSReadLineKeyHandler -Chord Enter -Function ValidateAndAcceptLine
200+
```
201+
172202
## PARAMETERS
173203

174204
### -AddToHistoryHandler

reference/7.2/PSReadLine/Set-PSReadLineOption.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 07/11/2023
5+
ms.date: 07/19/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170170
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171171
handler returns `$true` and PSReadLine saves the command in history.
172172

173+
### Example 8: Use CommandValidationHandler to validate a command before its executed
174+
175+
This example shows how to use the **CommandValidationHandler** parameter to run a validate a command
176+
before it's executed. The example specifically checks for the command `git` with the sub command
177+
`cmt` and replaces that with the full name `commit`. This way you can create shorthand aliases for
178+
subcommands.
179+
180+
```powershell
181+
# Load the namespace so you can use the [CommandAst] object type
182+
using namespace System.Management.Automation.Language
183+
184+
Set-PSReadLineOption -CommandValidationHandler {
185+
param([CommandAst]$CommandAst)
186+
187+
switch ($CommandAst.GetCommandName()) {
188+
'git' {
189+
$gitCmd = $CommandAst.CommandElements[1].Extent
190+
switch ($gitCmd.Text) {
191+
'cmt' {
192+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
193+
$gitCmd.StartOffset, $gitCmd.EndOffset - $gitCmd.StartOffset, 'commit')
194+
}
195+
}
196+
}
197+
}
198+
}
199+
# This checks the validation script when you hit enter
200+
Set-PSReadLineKeyHandler -Chord Enter -Function ValidateAndAcceptLine
201+
```
202+
173203
## PARAMETERS
174204

175205
### -AddToHistoryHandler

reference/7.3/PSReadLine/Set-PSReadLineOption.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 07/11/2023
5+
ms.date: 07/19/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.3&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170170
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171171
handler returns `$true` and PSReadLine saves the command in history.
172172

173+
### Example 8: Use CommandValidationHandler to validate a command before its executed
174+
175+
This example shows how to use the **CommandValidationHandler** parameter to run a validate a command
176+
before it's executed. The example specifically checks for the command `git` with the sub command
177+
`cmt` and replaces that with the full name `commit`. This way you can create shorthand aliases for
178+
subcommands.
179+
180+
```powershell
181+
# Load the namespace so you can use the [CommandAst] object type
182+
using namespace System.Management.Automation.Language
183+
184+
Set-PSReadLineOption -CommandValidationHandler {
185+
param([CommandAst]$CommandAst)
186+
187+
switch ($CommandAst.GetCommandName()) {
188+
'git' {
189+
$gitCmd = $CommandAst.CommandElements[1].Extent
190+
switch ($gitCmd.Text) {
191+
'cmt' {
192+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
193+
$gitCmd.StartOffset, $gitCmd.EndOffset - $gitCmd.StartOffset, 'commit')
194+
}
195+
}
196+
}
197+
}
198+
}
199+
# This checks the validation script when you hit enter
200+
Set-PSReadLineKeyHandler -Chord Enter -Function ValidateAndAcceptLine
201+
```
202+
173203
## PARAMETERS
174204

175205
### -AddToHistoryHandler

reference/7.4/PSReadLine/Set-PSReadLineOption.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.PSReadLine2.dll-Help.xml
33
Locale: en-US
44
Module Name: PSReadLine
5-
ms.date: 07/11/2023
5+
ms.date: 07/19/2023
66
online version: https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.4&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170170
returning the `SkipAdding` **AddToHistory** enum. If the command doesn't start with `git`, the
171171
handler returns `$true` and PSReadLine saves the command in history.
172172

173+
### Example 8: Use CommandValidationHandler to validate a command before its executed
174+
175+
This example shows how to use the **CommandValidationHandler** parameter to run a validate a command
176+
before it's executed. The example specifically checks for the command `git` with the sub command
177+
`cmt` and replaces that with the full name `commit`. This way you can create shorthand aliases for
178+
subcommands.
179+
180+
```powershell
181+
# Load the namespace so you can use the [CommandAst] object type
182+
using namespace System.Management.Automation.Language
183+
184+
Set-PSReadLineOption -CommandValidationHandler {
185+
param([CommandAst]$CommandAst)
186+
187+
switch ($CommandAst.GetCommandName()) {
188+
'git' {
189+
$gitCmd = $CommandAst.CommandElements[1].Extent
190+
switch ($gitCmd.Text) {
191+
'cmt' {
192+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
193+
$gitCmd.StartOffset, $gitCmd.EndOffset - $gitCmd.StartOffset, 'commit')
194+
}
195+
}
196+
}
197+
}
198+
}
199+
# This checks the validation script when you hit enter
200+
Set-PSReadLineKeyHandler -Chord Enter -Function ValidateAndAcceptLine
201+
```
202+
173203
## PARAMETERS
174204

175205
### -AddToHistoryHandler

0 commit comments

Comments
 (0)