Extending TabExpansion function Part 3
EDIT*: According to
/\/\o\/\/'s advice(refer to comment's section), i have modified the previous source to use
Get-Help instead of
Get-ChildItem to retrieve available
about_ help file names. (BTW, i will be modifying this script yet, again, so that "about_" would expand only when "Get-Help" or "Help" is already typed...(not to mess up File name expansion in $PsHome directory)
# Handle "about_*" help manuals
'\s*(about_[\w|\d]*)' {
$matched = $matches[1]
$helpFileNames = Get-Help $matched | Sort-Object Name | Select-Object Name
if ($helpFileNames.Length -ne $null) {
$helpFileNames | foreach {
# extract manual name(NOTE:same RegEx pattern)
$m = [RegEx]::Matches($_.Name, '\s*(about_[\w|\d]*)')
$m[0].Value
}
}
}
In this version, i have added functionality to expand "about_[tab]" manual files.
Now you can do;
- about_[Tab] -> about_Alias
- about_r[Tab] -> about_regular_expression
- about_regular_[Tab] -> about_regular_expression
- about_u[Tab] -> about_ubiquitous_parameters
I was a bit annoyed to type long manual names like
about_ubiquitous_parameters and
about_regular_expression (which is a manual i read often) all the time so decided to become *lazy*...
When you do
get-help about_alias,
See Also section mentions
about_variable manual.
If you try "about_v
[Tab]" it would not work because there is no manual file named
about_variable.help.txt in $pshome.
Now it should be apparent now how I have implemented my functionality.
It basically checks for manual file name that matches "about_blah*" against manual file names on $PSHOME directory.
switch -regex ($lastWord)
{
#
# Other functionalities...
#
# Handle "about_*" help manuals
'\s*(about_[\w\d]*)' {
$matched = $matches[1]
# Search for manual file name in $PSHOME
$helpFileNames = Get-ChildItem "$pshome/$($matched)*.help.txt" | Sort-Object Name | Select-Object Name
if ($helpFileNames -ne $null) {
$helpFileNames foreach {
# extract manual name(NOTE:same RegEx pattern)
$m = [RegEx]::Matches($_.Name, '\s*(about_[\w\d]*)')
$m[0].Value
}
}
}
# ...
Add above piece of code to "TabExpansion.ps1", go to wherever you have saved the file and ". .\TabExpansion.ps1" and you are good to go.
For previous blog posts(mine or
/\/\o\/\/'s)on "TabExpansion", go check out "
http://del.icio.us/powershell/TabExpansion"
Tags :
Monad msh PowerShell