簡體   English   中英

使用 Visual Studio Code 查看我的 function 的文檔字符串

[英]See the docstring of my function with Visual Studio Code

我是 Visual Studio Code 的新手,我開始使用它來開發 PowerShell 腳本。

函數聲明

我看到,當我將 cursor 放在 function 的名稱上時,我得到了 function 參數的概述,但當我使用我的自定義功能時,它不會。

如何為我的 PowerShell 函數聲明可以在 Visual Studio Code 概述中顯示的文檔?

我試圖這樣做:

<#
.Description
Get-Function displays the name and syntax of all functions in the session.
#>
function test([string]$print){}


<# Setup the working directories if they do not exist #>
If(!(Test-Path $WORKING_DIRECTORY)) {test}

但這似乎不起作用。

非常感謝

在描述中添加.PARAMETER對我有用。 另請注意,我相信您必須保存並運行一次腳本才能顯示。

<#
.Description
Get-Function displays the name and syntax of all functions in the session.
.PARAMETER  
#>

function test([string]$print){Write-Host $print}

If(Test-Path $env:userprofile) {test -print "Test123"}

在此處輸入圖像描述

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help?view=powershell-6

我之前的回答是不正確的,盡管閱讀基於評論的幫助對於通常記錄您的 cmdlet 仍然是一個好主意。

為了讓vscode能夠了解您的 function 定義,需要在其內部 Powershell 主機中定義 function。 如果您可以打開Powershell Integrated終端,您可以點源(運行類似. myScript.ps1的腳本以讀取 function 定義。您可能需要確保在運行任何代碼執行之前該腳本不是點源的(基本上將您的運行時代碼放在 if 塊中,檢查腳本是否是點源代碼,但將您的 function 定義保留在此條件之外)。

Powershell Integrated終端中對腳本進行點源處理后,您將獲得所需的使用工具提示彈出窗口。 要使其“自動”,請從vsvode終端解析$profile中的點源腳本。

這不是特別理想,我希望找到一個更簡化的解決方案,因為必須對我正在使用的每個腳本進行點源處理很麻煩。

稍后我可能會回來更新一些基本的基礎知識,但您要閱讀的是 Powershell 的基於評論的幫助 這些是特殊格式的注釋,在使用Get-Help或第三方文檔解決方案(例如 Visual Studio Code 的工具提示)時,會以更美觀的方式解析和顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM