File tree Expand file tree Collapse file tree 4 files changed +124
-4
lines changed Expand file tree Collapse file tree 4 files changed +124
-4
lines changed Original file line number Diff line number Diff line change 2
2
external help file : Microsoft.PowerShell.PSReadLine2.dll-Help.xml
3
3
Locale : en-US
4
4
Module Name : PSReadline
5
- ms.date : 07/11 /2023
5
+ ms.date : 07/19 /2023
6
6
online version : https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-5.1&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
8
8
title : Set-PSReadLineOption
@@ -169,6 +169,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
169
169
returning the ` SkipAdding ` ** AddToHistory** enum. If the command doesn't start with ` git ` , the
170
170
handler returns ` $true ` and PSReadLine saves the command in history.
171
171
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
+
172
202
## PARAMETERS
173
203
174
204
### -AddToHistoryHandler
Original file line number Diff line number Diff line change 2
2
external help file : Microsoft.PowerShell.PSReadLine2.dll-Help.xml
3
3
Locale : en-US
4
4
Module Name : PSReadLine
5
- ms.date : 07/11 /2023
5
+ ms.date : 07/19 /2023
6
6
online version : https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.2&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
8
8
title : Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170
170
returning the ` SkipAdding ` ** AddToHistory** enum. If the command doesn't start with ` git ` , the
171
171
handler returns ` $true ` and PSReadLine saves the command in history.
172
172
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
+
173
203
## PARAMETERS
174
204
175
205
### -AddToHistoryHandler
Original file line number Diff line number Diff line change 2
2
external help file : Microsoft.PowerShell.PSReadLine2.dll-Help.xml
3
3
Locale : en-US
4
4
Module Name : PSReadLine
5
- ms.date : 07/11 /2023
5
+ ms.date : 07/19 /2023
6
6
online version : https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.3&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
8
8
title : Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170
170
returning the ` SkipAdding ` ** AddToHistory** enum. If the command doesn't start with ` git ` , the
171
171
handler returns ` $true ` and PSReadLine saves the command in history.
172
172
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
+
173
203
## PARAMETERS
174
204
175
205
### -AddToHistoryHandler
Original file line number Diff line number Diff line change 2
2
external help file : Microsoft.PowerShell.PSReadLine2.dll-Help.xml
3
3
Locale : en-US
4
4
Module Name : PSReadLine
5
- ms.date : 07/11 /2023
5
+ ms.date : 07/19 /2023
6
6
online version : https://learn.microsoft.com/powershell/module/psreadline/set-psreadlineoption?view=powershell-7.4&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
8
8
title : Set-PSReadLineOption
@@ -170,6 +170,36 @@ The scriptblock returns `$false` if the command started with `git`. This has the
170
170
returning the ` SkipAdding ` ** AddToHistory** enum. If the command doesn't start with ` git ` , the
171
171
handler returns ` $true ` and PSReadLine saves the command in history.
172
172
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
+
173
203
## PARAMETERS
174
204
175
205
### -AddToHistoryHandler
You can’t perform that action at this time.
0 commit comments