Skip to content

Instantly share code, notes, and snippets.

@wgross
Created January 15, 2023 14:40
Show Gist options
  • Save wgross/0ab9ec21438f074acc7b81e8fd0b0e7e to your computer and use it in GitHub Desktop.
Save wgross/0ab9ec21438f074acc7b81e8fd0b0e7e to your computer and use it in GitHub Desktop.
Tiny PowerShell helpers for CLIs
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
}
}
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