2017-09-06 44 views
2

this about page中,有一個代碼塊(下面)顯示了$ForEach自動變量,但它也具有類似批處理子例程的語法。我無法找到關於這段代碼如何工作或調用什麼語言結構的文檔。我相信這是一個PowerShell v5新增功能,但閱讀發行說明也沒有幫助我。 :tokenLoop foreach ($token in $tokens)代表什麼?about_Foreach示例:PowerShell中的cmd子例程語法?

function Get-FunctionPosition { 
    [CmdletBinding()] 
    [OutputType('FunctionPosition')] 
    param(
    [Parameter(Position = 0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] 
    [ValidateNotNullOrEmpty()] 
    [Alias('PSPath')] 
    [System.String[]] 
    $Path 
) 

    process { 
    try { 
     $filesToProcess = if ($_ -is [System.IO.FileSystemInfo]) { 
     $_ 
     } 
     else { 
     Get-Item -Path $Path 
     } 
     foreach ($item in $filesToProcess) { 
     if ($item.PSIsContainer -or $item.Extension -notin @('.ps1', '.psm1')) { 
      continue 
     } 
     $tokens = $errors = $null 
     $ast = [System.Management.Automation.Language.Parser]::ParseFile($item.FullName, ([REF]$tokens), ([REF]$errors)) 
     if ($errors) { 
      Write-Warning "File '$($item.FullName)' has $($errors.Count) parser errors." 
     } 
     :tokenLoop foreach ($token in $tokens) { 
      if ($token.Kind -ne 'Function') { 
       continue 
      } 
      $position = $token.Extent.StartLineNumber 
      do { 
      if (-not $foreach.MoveNext()) { 
       break tokenLoop 
      } 
      $token = $foreach.Current 
      } until ($token.Kind -in @('Generic', 'Identifier')) 
      $functionPosition = [pscustomobject]@{ 
      Name  = $token.Text 
      LineNumber = $position 
      Path  = $item.FullName 
      } 
      Add-Member -InputObject $functionPosition -TypeName FunctionPosition -PassThru 
     } 
     } 
    } 
    catch { 
     throw 
    } 
    } 
} 
+3

你混合兩種結構討論;請參閱[about_break](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_break?view=powershell-5.1) –

+0

因此,它是一個循環的標籤,可用於休息聲明。你知道這是否一直是PowerShell的一部分,或者添加了特定的版本@JeffZeitlin?我在約頁上找不到「這是在X中添加的」。 – TheIncorrigible1

+1

@ TheIncorrigible1標籤自從v3.0版起就一直在ps(在v3語言規範中對它們進行了描述) –

回答

4

在PowerShell中 版本3.0和至多 (至少從版本2.0),下面的語句類型任選標記

  • switch
  • foreach
  • for
  • while
  • do

現在,這是什麼意思?這意味着您可以提供一個標籤名稱作爲標籤語句正文內的breakcontinue聲明的參數,並使流控制適用於標籤指示的聲明。

考慮這個例子:

foreach($Name in 'Alice','Bob','Charlie'){ 
    switch($Name.Length){ 
     {$_ -lt 4} { 
      # We don't have time for short names, go to the next person 
      continue 
     } 
     default { 
      Write-Host "$Name! What a beautiful name!" 
     } 
    } 

    Write-Host "Let's process $Name's data!" 
} 

你可能會想到「讓我們的過程[...]」字符串出現只有兩次,因爲我們continueBob的情況,但由於直接父語句一個switch,它實際上並不適用於foreach聲明。現在

,如果我們可以明確地指出,我們要繼續foreach循環而不是switch語句中,我們能夠避免:

:outer_loop 
foreach($Name in 'Alice','Bob','Charlie'){ 
    switch($Name.Length){ 
     {$_ -lt 4} { 
      # We don't have time for short names, go to the next person 
      continue outer_loop 
     } 
     default { 
      Write-Host "$Name! What a beautiful name!" 
     } 
    } 

    Write-Host "Let's process $Name's data!" 
} 

現在continue聲明實際上是繼續循環,而不是開關。

當你有嵌套循環結構時非常有用。


標籤進行簡單結合break一個while語句中的about_Break help topic

+0

IIRC,循環標籤自v1開始就在PowerShell中。他們當然在V2中工作。雖然我沒有v1在這裏確認。 – PetSerAl