Created
January 15, 2023 14:40
-
-
Save wgross/0ab9ec21438f074acc7b81e8fd0b0e7e to your computer and use it in GitHub Desktop.
Tiny PowerShell helpers for CLIs
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
filter zero_is_true { | |
# incoming value of 0 is mapped to $true, mapped to $false ortherwise | |
if(0 -eq $_) { | |
$true | |
} | |
else { | |
$false | |
} | |
} | |
filter zero_is_false { | |
# incoming value of 0 is mapped to $true, mapped to $false ortherwise | |
if(0 -eq $_) { | |
$false | |
} | |
else { | |
$true | |
} | |
} | |
filter throw_on_false($msg) { | |
# incomiminf value of $false raises an excepition with the given message | |
if($false -eq $_) { | |
throw $msg | |
} | |
} | |
filter optional_value($value) { | |
# incoming value of $true is mapped to $value, $false produces no value | |
if(($null -ne $_) -and ($true -eq $_)) { | |
$value | |
} | |
} | |
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
param($name) | |
# Create a warpper function for CLI command which dumps the call with itsn parameter to debug stream for easier problem search | |
$upperName = $name.ToUpper() | |
"function $($name)_ {" | |
" `$global:$($name.ToUpper())_LASTEXITCODE = `$null" | |
" `"$($name) `$args`" | Write-Debug" | |
" $($name) `$args" | |
" `$global:$($name.ToUpper())_LASTEXITCODE = `$LASTEXITCODE" | |
" `"$name exitcode: `${global:$($upperName)_LASTEXITCODE}`" | Write-Debug" | |
"}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment