generated from PSModule/Template-PSModule
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Get-LineComment filter to extract inline comments from PowerShell…
… code lines
- Loading branch information
1 parent
ad2089b
commit d1a6a5f
Showing
2 changed files
with
44 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
filter Get-LineComment { | ||
<# | ||
.SYNOPSIS | ||
Extracts the inline comment from a single line of PowerShell code. | ||
.DESCRIPTION | ||
Parses a given line of PowerShell code and extracts any inline comment. | ||
If an inline comment exists, the function returns the comment text; otherwise, it returns nothing. | ||
.EXAMPLE | ||
'Get-Process # This retrieves all processes' | Get-LineComment | ||
Returns: '# This retrieves all processes' | ||
.EXAMPLE | ||
'Write-Host "Hello World"' | Get-LineComment | ||
Returns: $null (no comment found) | ||
.LINK | ||
https://psmodule.io/AST/Functions/Lines/Get-LineComment/ | ||
#> | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute( | ||
'PSUseShouldProcessForStateChangingFunctions', '', | ||
Justification = 'Does not change state' | ||
)] | ||
[OutputType([string])] | ||
[CmdletBinding()] | ||
param ( | ||
# Input line of PowerShell code from which to extract the comment. | ||
[Parameter( | ||
Mandatory, | ||
ValueFromPipeline | ||
)] | ||
[string] $Line | ||
) | ||
|
||
# Parse the line using the PowerShell parser to obtain its tokens. | ||
$tokens = $null | ||
$null = [System.Management.Automation.Language.Parser]::ParseInput($Line, [ref]$tokens, [ref]$null) | ||
|
||
# Find comment token(s) in the line. | ||
($tokens | Where-Object { $_.Kind -eq 'Comment' }).Extent.Text | ||
} |
This file was deleted.
Oops, something went wrong.