PowerShell’s Domain Specific Language for describing command-line parameter attributes (see about_Functions_Advanced) offers many features. It’s powerful, versatile, and allows scripts to implement some of the worst features of true cmdlets.
What do I mean (this time)?
Halt-and-Demand-User-Input should not be the default behavior for missing command-line parameters in a scripting language.
I’ll say that again.
Halt-and-Demand-User-Input must not be the default behavior for missing command-line parameters in a scripting language.
This is just asking for a hung script, either blinking away in some PSH window, or, worse, in some scheduled task. Unix core utilities’ default behavior rules are simple:
- When not specified, read input from STDIN
- When not specified, send output to STDOUT
- Errors are sent to STDERR. Always
- When in doubt do something useful.
Inherent in that last axiom are the following:
- ‘Useful’ means ‘not disruptive, not destructive’.
‘ ‘Useful’ means educating the user if a default action is not possible (either by showing abbreviated help text to STDOUT, or sending a “I need more data, <expletive>” error to STDERR.
- ‘Useful’ means to STOP EXECUTING after doing something useful.
I much prefer the following:
param ([string]$Name = $null);
Write-Warning "$($MyInvocation.MyCommand.Name) -Name not specified, required.";
} # if (!$Name)
} # begin
# do stuff here
} # process
} # function Test-It
And no, this is not an April Fool’s post.