正如你們所知,我可以在 Power Shell 視窗中使用 clear 或 cls。
但是我發現 clear(5) 或 cls(5) 有效,也可以清除螢屏(任何數字都有效,包括負數)。但是如果我嘗試類似 clear(abc) 或 clear("abc") 它會拋出錯誤
abc:術語“abc”未被識別為 cmdlet、函式、腳本檔案或可執行程式的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確并重試。
為什么它接受一個數字,如果該數字沒有做任何事情(不是要清除的行數或任何東西),為什么會拋出非數字錯誤?
uj5u.com熱心網友回復:
基于Santiago Squarzon的評論:Clear-Host命令是一個函式。你可以通過運行看到這一點:
Get-Command Clear-Host -OutVariable cmd
輸出
CommandType Name
----------- ----
Function Clear-Host
由于函式是用 PowerShell 撰寫的,因此您還可以查看內容(定義):
$cmd.Definition
輸出 (Windows PowerShell 5.1)
$RawUI = $Host.UI.RawUI
$RawUI.CursorPosition = @{X=0;Y=0}
$RawUI.SetBufferContents(
@{Top = -1; Bottom = -1; Right = -1; Left = -1},
@{Character = ' '; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor})
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225747
# .ExternalHelp System.Management.Automation.dll-help.xml
輸出(PowerShell 7.1.3,在 Linux 上)
[Console]::Write((
& (Get-Command -CommandType Application clear | Select-Object -First 1).Definition
))
# .Link
# https://go.microsoft.com/fwlink/?LinkID=2096480
# .ExternalHelp System.Management.Automation.dll-help.xml
您可以查看about_Functions_Advanced以閱讀有關此內容[CmdletBinding()]的更多資訊,但如果沒有 ,它不是“高級”函式,因此它不會以相同的方式驗證其引數。
相反,該函式可以訪問其未命名的引數,$args但正如您在定義中看到的那樣,它不能。
Get-Help Clear-Host會告訴你它不需要任何引數:
NAME
Clear-Host
SYNOPSIS
SYNTAX
Clear-Host [<CommonParameters>]
DESCRIPTION
RELATED LINKS
https://go.microsoft.com/fwlink/?LinkID=225747
REMARKS
To see the examples, type: "get-help Clear-Host -examples".
For more information, type: "get-help Clear-Host -detailed".
For technical information, type: "get-help Clear-Host -full".
For online help, type: "get-help Clear-Host -online"
即使是命名引數的非高級函式也會讓它們顯示在幫助輸出中:
function Test-Thing($one, $two) {}
Get-Help Test-Thing
輸出
NAME
Test-Thing
SYNTAX
Test-Thing [[-one] <Object>] [[-two] <Object>]
ALIASES
None
REMARKS
None
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473745.html
