powershell - 如何定义自定义命令的文档(使用 Get-Command 或 Get-Help)
问题描述
假设我有一个自定义命令:
function Search-ForStringInFile($string)
{
ls -Recurse | Select-String -Pattern "$string" -List | Select Path
}
而且我希望能够运行Get-Help Search-ForStringInFile
或Get-Command Search-ForStringInFile
获得命令功能的描述。
描述:在当前目录/下的所有文件中搜索关键字
我可以在function
添加此文档时使用特殊的注释语法吗?
解决方案
这称为基于注释的帮助。事实上,PowerShell ISE 有一个非常棒的代码片段,可以准确地完成您想做的事情。只需点击 Control+J 并选择“Cmdlet - 高级功能”来加载我将在下面提供的代码片段:
<#
.Synopsis
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
#>
function Verb-Noun
{
}
一旦您填写了上述每一项的值(请参阅此处的文档以了解所有可用字段)并按 F5,帮助将出现在您的函数中。
PS>Get-Help Verb-Noun
NAME
Verb-Noun
SYNOPSIS
Short description
SYNTAX
Verb-Noun [-Param1] <Object> [-Param2 <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
Verb-Noun [-Param3 <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
DESCRIPTION
Long description
PARAMETERS
-Param1 <Object>
Param1 help description
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false
-Param2 <Int32>
Param2 help description
Required? false
Position? named
Default value 0
Accept pipeline input? false
Accept wildcard characters? false
-Param3 <String>
Param3 help description
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-WhatIf [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Confirm [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
Inputs to this cmdlet (if any)
OUTPUTS
Output from this cmdlet (if any)
NOTES
General notes
-------------------------- EXAMPLE 1 --------------------------
PS C:\>Example of how to use this cmdlet
-------------------------- EXAMPLE 2 --------------------------
PS C:\>Another example of how to use this cmdlet
RELATED LINKS
因此,要向您自己的 cmdlet 添加帮助,您只需在函数中粘贴相同的注释块即可。您可以将其放置在以下三个位置之一:
- 在函数声明之前
- 函数声明后(ewww)
- 在你的功能结束时(不要被认为是,但选择这个选项在道德上是错误的)。
推荐阅读
- mysql - 在 MySQL 中使用 CAST() 将 VARCHAR 转换为 CHAR,但最终结果仍然是 VARCHAR 而不是 CHAR
- python - PyTorch 模型在新机器上第一次加载需要太多时间
- docker - docker no_proxy不生效
- android - KitKat 服务图标上的红色方块
- postgresql - Flyway 迁移成功,但 postgres 说没有使用 \dt 的关系
- javascript - 如何将项目从 1 个列表移动到 HTML 中的另一个列表
- python - 根据特定条件更改熊猫列中的第一次出现
- python - 通过 boto3 的 Amazon S3 批处理作业 - 请求无效
- linux - Vagrant 无法通过提供的 GCP 服务帐户进行身份验证
- javascript - 如何在 Ajax 刷新或提交到数据库后将计数器重置为 0,