Adding a -Quiet parameter to NuPack cmdlets

Oct 7, 2010 at 6:34 AM

I'm considering adding a -Quiet parameter to all cmdlets. This parameter, when specified, will suppress all messages from being printed. Here is the background:

There is a bug in the console which occurs when you type "Install-Package<tab>". At this time, the tab expansion internally calls Get-Package to get the list of all packages in order to feed to the suggestion list. If, for any reason, Get-Package fails, it will print an ugly error message to the end of the command line. Adding a -Quiet parameter to Get-Package will work around that.

Even though the main intent is to fix the above bug, I figure that adding the -Quiet parameter will give benefits to the mainstream usage too. Agreed?

Oct 7, 2010 at 6:36 AM

What's the PowerShell convention here? I don't think it's this ... Normally it's either "the usual" or "verbose", but I'd defer to a PS expert before jumping in.

Oct 7, 2010 at 8:12 AM

The option seems reasonable, following PS conventions of course. We had a PS MVP in here recently. Hopefully that person chimes in. J

Oct 7, 2010 at 8:56 AM

If it's an error condition, this is controlled by a few things. First up, in PowerShell there is the notion of terminating and non-terminating errors. The former will cause the script to immediately terminate and is typically incurred with a throw, or CmdletBinding style $PSCmdlet.ThrowTerminatingError(...). The latter depends on the value of the global, $ErrorActionPreference. When using v2-style functions with [CmdletBinding()], this allows the caller to override on a per-cmdlet basis with -ErrorAction. This is an enum, which can have four values: SilentlyContinue, Stop, Continue or Inquire. Non-terminating errors should be emitted with the Write-Error cmdlet, or by using the CmdletBinding hook $PSCmdlet.WriteError(...). All errors should be wrapped in ErrorRecord instances if you want to comply with guidelines.

If you're complying with standard error handling guidelines, you should be suppressing errors by calling Get-Package -ErrorAction SilentlyContinue - this presumes you are using try/catch to guard against throws from any of your interop (DTE, COM whatever) and are wrapping these .NET exceptions in ErrorRecords and writing them out to the error stream with Write-Error.


function Get-Package {
     param( ... )

 try {
     $packages = [foo]::bar() # may throw FooException
 } catch {
     # convert to non-terminating
     write-error $_

I've got to run now, but take a closer look at Write-Error, ErrorActionPreference and the help topic: about_preference_variables. The above is very simplified.