Invoke-Formatter
根據輸入設定或預設設定格式化腳本文字。
語法
Invoke-Formatter
[-ScriptDefinition] <string>
[[-Settings] <Object>]
[[-Range] <int[]>]
[<CommonParameters>]
Description
Invoke-Formatter
Cmdlet 會採用字串輸入,並根據定義的設定來格式化它。 如果未提供 Settings 參數,Cmdlet 會假設預設程式代碼格式設定如 Settings/CodeFormatting.psd1
中所定義。
範例
範例 1 - 使用預設設定格式化輸入文稿文字
$scriptDefinition = @'
function foo {
"hello"
}
'@
Invoke-Formatter -ScriptDefinition $scriptDefinition
function foo {
"hello"
}
範例 2 - 使用哈希表中定義的設定來格式化輸入腳本
$scriptDefinition = @'
function foo {
"hello"
}
'@
$settings = @{
IncludeRules = @("PSPlaceOpenBrace", "PSUseConsistentIndentation")
Rules = @{
PSPlaceOpenBrace = @{
Enable = $true
OnSameLine = $false
}
PSUseConsistentIndentation = @{
Enable = $true
}
}
}
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
function foo
{
"hello"
}
範例 3 - 使用定義 '.psd1' 檔案的設定來格式化輸入腳本文字
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings /path/to/settings.psd1
參數
-Range
應該在其中進行格式設定的範圍。 此參數的值必須是四個整數的陣列。 這些數字必須大於 0。 四個整數會依此順序表示下列四個值:
- 起始行號
- 起始數據行編號
- 結束行號
- 結束數據行編號
類型: | Int32[] |
Position: | 3 |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-ScriptDefinition
要格式化為字串之腳本的文字。 這不是 ScriptBlock 物件。
類型: | String |
Position: | 1 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-Settings
包含設定的 PowerShell 資料檔 (.psd1
) 的設定哈希表或路徑。
類型: | Object |
Position: | 2 |
預設值: | CodeFormatting |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
輸出
格式化的字串結果。