added match.ps1 to Windows-Scripts to make quick strcmps easier in powershell
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
# First string to compare
|
||||
[Parameter(Position = 0)]
|
||||
[string]$String1,
|
||||
|
||||
# Second string to compare
|
||||
[Parameter(Position = 1)]
|
||||
[string]$String2,
|
||||
|
||||
# Optional switch to enable case-sensitive comparison
|
||||
[switch]$CaseSensitive,
|
||||
|
||||
# Help flag (-h, -Help)
|
||||
[Alias('h')]
|
||||
[switch]$Help
|
||||
)
|
||||
|
||||
begin {
|
||||
function Show-Help {
|
||||
@'
|
||||
NAME
|
||||
match - Compare two strings and output visual match status
|
||||
|
||||
SYNOPSIS
|
||||
match <String1> <String2> [-CaseSensitive]
|
||||
match -Help
|
||||
|
||||
DESCRIPTION
|
||||
Compares two string arguments.
|
||||
Outputs "Match OK" in GREEN if they match.
|
||||
Outputs "FAIL: NO MATCH" in RED if they differ.
|
||||
|
||||
PARAMETERS
|
||||
String1
|
||||
The first string to compare.
|
||||
|
||||
String2
|
||||
The second string to compare.
|
||||
|
||||
-CaseSensitive
|
||||
Perform a case-sensitive comparison (default is case-insensitive).
|
||||
|
||||
-h, -Help, h, help (case insensitive)
|
||||
Display this help message.
|
||||
|
||||
EXAMPLES
|
||||
1. Compare two strings (case-insensitive default):
|
||||
match "hello" "Hello"
|
||||
Output: Match OK (in green)
|
||||
|
||||
2. Compare two strings with case sensitivity:
|
||||
match "hello" "Hello" -CaseSensitive
|
||||
Output: FAIL: NO MATCH (in red)
|
||||
|
||||
3. Compare hashes or checksum outputs:
|
||||
match "e3b0c442" "e3b0c442"
|
||||
Output: Match OK (in green)
|
||||
'@
|
||||
}
|
||||
}
|
||||
|
||||
process {
|
||||
# Check if help was explicitly requested via flag or positional parameter
|
||||
$isHelpRequested = $Help.IsPresent -or (
|
||||
$PSBoundParameters.Count -gt 0 -and (
|
||||
($String1 -and $String1 -match '^(?i)[-/]{0,2}(h|help)$') -or
|
||||
($String2 -and $String2 -match '^(?i)[-/]{0,2}(h|help)$')
|
||||
)
|
||||
)
|
||||
|
||||
if ($isHelpRequested) {
|
||||
Show-Help
|
||||
return
|
||||
}
|
||||
|
||||
# Trigger help usage if either string parameter is missing or whitespace
|
||||
if ([string]::IsNullOrWhiteSpace($String1) -or [string]::IsNullOrWhiteSpace($String2)) {
|
||||
Write-Host "Error: Missing required arguments." -ForegroundColor Yellow
|
||||
Write-Host "Usage: match <String1> <String2> [-CaseSensitive]" -ForegroundColor Cyan
|
||||
Write-Host "Run 'match -Help' for detailed documentation.`n"
|
||||
return
|
||||
}
|
||||
|
||||
# Compare strings based on case-sensitivity choice
|
||||
$isMatch = if ($CaseSensitive) {
|
||||
$String1 -ceq $String2
|
||||
} else {
|
||||
$String1 -eq $String2
|
||||
}
|
||||
|
||||
if ($isMatch) {
|
||||
Write-Host "`n`nSUCCESS: MATCH OK!`n`n" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "`n`nFAIL: NO MATCH!`n`n" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
end {}
|
||||
Reference in New Issue
Block a user