PowerShell V2 & Cmdlet Keyword

Link. October 18, 2008. Comments [3]. Posted in: PowerShell

In the latest entry in the official PowerShell Blog, Jeffrey Snover leaves a little tidbit about a change coming on the next CTP of PowerShell V2:

The cmdlet keyword is going away and we'll just have function. Notice that now you can specify the [Parameter()] attribute on parameters.  When you do that, we treat the function like a cmdlet.

I don’t mind getting rid of the cmdlet keyword; honestly, but is the new syntax that much of an improvement? So now instead of an explicit keyword telling you whether something is a function of a cmdlet, you have an attribute spilled around the function parameters? Not sure if that’s good or not, but sounds confusing to me, at first glance.

Maybe what rubs me the wrong way, however, is the [Parameter] attribute. I’m OK with using an attribute to specify the metadata associated with the parameters, honestly. But the whole Param + [Parameter] thing just reads redundant and ugly:

function Emit-XML {
Param ([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$object)
...
}

Can anyone more familiar with this chime in? Am I the only one slightly bothered by this?



Saturday, October 18, 2008 1:22:00 PM (SA Pacific Standard Time, UTC-05:00)
Thomas,

For a number of basic functions, this notation will not be necessary, but what it does allow is greater flexibility in working with pipelined objects and saving you from writing some argument parsing.

In the first case, by allowing a value from the pipeline, you are allowing the PowerShell parser to take a look at objects coming in to the function and fill out the parameters based what properties are available.

In the second scenario, by setting the parameter attribute, you can set up groups of parameters, set up optional parameters, and not have to write code in your function to parse through the arguements and determine what has been passed in.
Saturday, October 18, 2008 1:35:57 PM (SA Pacific Standard Time, UTC-05:00)
Thanks for the response. FWIW, I have no problems with having both functions and cmdlets. In fact, I do think that script cmdlets are a great feature in V2. My question was purely about the syntax used to define the script cmdlets and more precisely about the syntax change coming in CTP3 (whether is a better syntax or not or how it could be improved).
Saturday, October 18, 2008 3:04:47 PM (SA Pacific Standard Time, UTC-05:00)
Yeah, you have to just tell yourself that you technically never had a "cmdlet" keyword in scripts. Rembemer that even in CTP2, when you write "cmdlet" (or "filter" for that matter) ... what you get is actually a Function that shows up in the Function: PSDrive.

If you think about it in terms of the change from v1 (and pretend not to be an alpha technology user for a moment), what they are doing is enhancing the existing v1 function feature with the ability to have attributes so we can specify constraints and order, and take values from the pipeline or from properties of the pipeline objects.

I'd agree that the Param([Parameter]...) syntax is a little redundant, but I can't really think of a better pair of words to use, and they were going to have that redundancy even if they have the "cmdlet" keyword, so basically ... no difference, right?
Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1051
This Year: 2
This Month: 2
This Week: 1
Comments: 827

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In