编程语言
首页 > 编程语言> > 如何让C#Cmdlet参数HelpMessage显示在`Get-Help`中

如何让C#Cmdlet参数HelpMessage显示在`Get-Help`中

作者:互联网

我已经启动了PowerShell cmdlet,并希望为参数提供帮助消息.我已经尝试过使用ParameterAttribute.HelpMessage:

[Cmdlet(VerbsCommon.Get, "Workspace", SupportsShouldProcess = true)]
public class GetWorkspace : PSCmdlet
{
    [Parameter(
        Mandatory = true,
        Position = 1,
        HelpMessage = "The path to the root directory of the workspace.")]
    public string Path { get; set; }

    protected override void ProcessRecord()
    {
        base.ProcessRecord();
    }
}

但是,当我使用PowerShell Get-Help命令时,它不会显示参数的HelpMessage:

get-help Get-Workspace -det

NAME
    Get-Workspace

SYNTAX
    Get-Workspace [-Path] <string> [-WhatIf] [-Confirm]  [<CommonParameters>]


PARAMETERS
    -Confirm

    -Path <string>

    -WhatIf

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).


ALIASES
    None


REMARKS
    None

在PowerShell脚本中编写cmdlet时,我可以使用以下语法为参数添加帮助消息:

<#
.Parameter Path
The local path to the root folder of the workspace.
#>

但是C#cmdlet中的等价物是什么?

解决方法:

你这样做是绝对正确的!

您只需要检查-Full视图或使用-Parameter获取特定参数的帮助:

PS C:\> Get-Help Get-Workspace -Parameter Path

-Path <string>
    The path to the root directory of the workspace.

    Required?                    true
    Position?                    1
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      None
    Dynamic?                     false

标签:c,powershell,cmdlet
来源: https://codeday.me/bug/20190609/1203087.html