着色求助输出:如何使用正则判处选择以连字符( - )开头的精确字符串,并以字母结尾
我目前正在尝试为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" `
}
换句话说,我需要匹配以“ a”的字符串的正则态度 - ”并以字母结尾。
如果有人可以帮助我,我真的很感激。提前致谢。
I'm currently trying to color my PowerShell's Get-Help
cmdlet output. I successfully colored the output that shows the name of the cmdlet that I'm trying to use Get-Help
on. I've also managed to color the output that shows all the headings of manual page. However, I'm unable to consistently color the output of the options shown on the manual page as you can see below:
#!/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]+quot;, "$GREEN`$0$RESET" `
-creplace "\b$WORD\b", "$YELLOW`$0$RESET" `
-replace "-[a-z]*\b", "$CYAN`$0$RESET" `
}
In other words, I need the regex that matches a string that starts with a "-" and ends with an alphabet.
I would really appreciate if someone could help me with this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您可以将其放入
$ profile
文件中,以自动化着Get-Help
的输出。它解决了使用REGEX\ b- \ w+
(请参阅 REGEX101演示)。输出:
备注:
Microsoft.powershell.core \ get-help
)来调用原始命令。要获取模块前缀,请键入(get-command thecommand).modulename
。$ PSSTYLE
变量作为获取ANSI逃生代码的方便方式。- ?
参数调用命令时,这甚至可以工作,因为此调用get-help
内部。This is something you could put into your
$Profile
file to automatically colorize output ofGet-Help
. It fixes the problem of colorizing the parameters using RegEx\B-\w+
(see regex101 demo).Output:
Remarks:
Microsoft.PowerShell.Core\Get-Help
. To get the module prefix, type(Get-Command TheCommand).ModuleName
.$PSStyle
variable as a handy way to get ANSI escape codes for coloring.-?
parameter, as this callsGet-Help
internally.