我目前正在尝试为 PowerShell 的 Get-Help
cmdlet 输出着色。我成功地对显示我尝试使用 Get-Help
的 cmdlet 名称的输出进行了着色。我还设法对显示手册页所有标题的输出进行着色。但是,我无法对手册页上显示的选项的输出进行一致的着色,如下所示:
#!/usr/bin/env powershell
$GREEN = "$([char]0x1b)[92m"
$RED = "$([char]0x1b)[91m"
$CYAN = "$([char]0x1b)[96m"
$BLUE = "$([char]0x1b)[94m"
$YELLOW = "$([char]0x1b)[93m"
$PURPLE = "$([char]0x1b)[95m"
$RESET = "$([char]0x1b)[0m"
Get-Help @args > man_text.txt
$WORD = $args[0]
cat man_text.txt | `
% {$_ `
-creplace "^[A-Z \d\W]+$", "$GREEN`$0$RESET" `
-creplace "\b$WORD\b", "$YELLOW`$0$RESET" `
-replace "-[a-z]*\b", "$CYAN`$0$RESET" `
}
换句话说,我需要匹配以“-”开头并以字母表结尾的字符串的正则表达式。
如果有人能帮助我解决这个问题,我将非常感激。提前致谢。
最佳答案
您可以将其放入 $Profile
文件中,以自动为 Get-Help
的输出着色。它修复了使用 RegEx \B-\w+
对参数进行着色的问题(请参阅 regex101 demo )。
# Overrides the original Get-Help command to colorize its output.
Function Get-Help {
[CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='https://go.microsoft.com/fwlink/?LinkID=2096483')]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string] ${Name},
[string] ${Path},
[ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','Filter','ExternalScript','All','DefaultHelp','DscResource','Class','Configuration')]
[string[]] ${Category},
[Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
[switch] ${Detailed},
[Parameter(ParameterSetName='AllUsersView')]
[switch] ${Full},
[Parameter(ParameterSetName='Examples', Mandatory=$true)]
[switch] ${Examples},
[Parameter(ParameterSetName='Parameters', Mandatory=$true)]
[string[]] ${Parameter},
[string[]] ${Component},
[string[]] ${Functionality},
[string[]] ${Role},
[Parameter(ParameterSetName='Online', Mandatory=$true)]
[switch] ${Online},
[Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
[switch] ${ShowWindow}
)
process {
# Call the original Get-Help command by its fully qualified path.
$help = Microsoft.PowerShell.Core\Get-Help @PSBoundParameters
# Define the styles for colorization.
$style = @{
SECTION = $PSStyle.Formatting.FormatAccent
COMMAND = $PSStyle.Foreground.BrightYellow
PARAM = $PSStyle.Foreground.FromRgb(64,200,230)
}
# Escape the command name for use in RegEx
$commandNameEscaped = [regex]::Escape( $help.Name )
# Define a RegEx for doing the formatting. The names of the RegEx groups have to match the keys of the $style hashtable.
$regEx = @(
"(?m)(?<=^[ \t]*)(?<SECTION>[A-Z][A-Z \t\d\W]+$)"
"(?<COMMAND>\b$commandNameEscaped\b)"
"(?<PARAM>\B-\w+)"
) -join '|'
# Format the help object
$help | Out-String | ForEach-Object {
[regex]::Replace( $_, $regEx, {
# Get the RegEx group that has matched.
$matchGroup = $args.Groups.Where{ $_.Success }[ 1 ]
# Use the RegEx group name to select associated style for colorizing the match.
$style[ $matchGroup.Name ] + $matchGroup.Value + $PSStyle.Reset
})
}
}
}
输出:
备注:
关于regex - 对 Get-Help 输出 : how to use Regex to select exact string that starts with a hyphen(-) and ends with an alphabet 进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72525108/