Skip to content

Instantly share code, notes, and snippets.

@stej
Created January 22, 2011 22:43
Show Gist options
  • Save stej/791578 to your computer and use it in GitHub Desktop.
Save stej/791578 to your computer and use it in GitHub Desktop.
function switchtest {
param([switch]$myswitch)
if ($myswitch) { 'switch is on' }
if (!$myswitch) { 'switch if off' }
#same as above
#if ($myswitch) { 'switch is on' } else { 'switch if off' }
write-host switch value is ([bool]$myswitch) and $myswitch
}
## test
PS> switchtest -myswitch
switch is on
switch value is True and True
PS> switchtest
switch if off
switch value is False and False
@stej
Copy link
Author

stej commented Jan 23, 2011

A to jak obráceně? Switch je od toho, aby tam buď byl, nebo nebyl :) False je prostě defaultní hodnota. V tvým případě, pokud si na tom budeš trvat, tak je to spíš bool a ne switch.

Asi ti slo o obracenou logiku, co?

function Do-It {
    param([switch] $delete = $true)   
    if ($delete -eq $true) { Remove-Item $tmp -force -recurse }
}
Do-It # smaze, protoze default je true

function Do-It {
    param([switch] $keep)   
    if (!$keep) { Remove-Item $tmp -force -recurse }
}
Do-It # smaze, protoze keep je false

@rarous
Copy link

rarous commented Jan 24, 2011

Přijímám patche ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment