有趣的問題......有沒有這樣的功能屬性,AFAIK。但我認爲,採用基於評論的幫助屬性(可能甚至根本不知道,但我不太確定)是一種半傻的方式。
<#
.FUNCTIONALITY
TEST1
#>
function Do-Something1
{}
<#
.FUNCTIONALITY
TEST2
#>
function Do-Something2
{}
Get-ChildItem Function: | %{
$fun = $_.Name
try {
Get-Help $fun -Functionality TEST* | %{
switch($_.Functionality) {
'TEST1' { "$fun is for test 1" }
'TEST2' { "$fun is for test 2" }
}
}
}
catch {}
}
輸出:
Do-Something1 is for test 1
Do-Something2 is for test 2
也許這種做法可能會在某些情況下非常有用。
另見COMMENT-HELP基於關鍵字的幫助:
man about_Comment_Based_Help
UPDATE 雖然上面的答案被接受,我仍然無法與它很高興。這是另一種絕對不是黑客行爲的方法。它也有一個優點,請參閱評論。這種方法使用傳統名稱的額外別名。
# Functions to be used in tests, with any names
function Do-Something1 { "Something1..." }
function Do-Something2 { "Something2..." }
# Aliases that define tests, conventional names are UnitTest-*.
# Note: one advantage is that an alias can be defined anywhere,
# right where a function is defined or somewhere else. The latter
# is suitable in scenarios when we cannot modify the source files
# (or just do not want to).
Set-Alias UnitTest-Do-Something1 Do-Something1
Set-Alias UnitTest-Do-Something2 Do-Something2
# Get UnitTest-* aliases and extract function names for tests.
Get-Alias UnitTest-* | Select-Object -ExpandProperty Definition
# Or we can just invoke aliases themselves.
Get-Alias UnitTest-* | % { & $_}
感謝的建議得到模塊的各種信息,但是這不是我的觀點。我已經在無法重命名的地方創建了傳播和宏集(因爲有很多其他依賴的宏正在使用它們) – 2011-01-11 18:35:50