Industrial Strength Functions in V2 Part 2

Jeffrey Snover just published a great article on using the  capabilities of Advanced Functions. They have changed the name from Script Cmdlets to Advanced Functions and completely gotten rid of the Cmdlet keyword.

This actually makes a lot of sense IMHO. One of the beauties of this is that there is a smooth path from a basic function to an advanced function

   1: PS C:> function foo {"foo $args"}
   2: PS C:> foo bar
   3: foo bar
   4: PS C:> function foo ($a) {"foo $a"}
   5: PS C:> foo -a bar
   6: foo bar
   7: PS C:> function foo {param($a) "foo $a"}
   8: PS C:> foo -a bar
   9: foo bar
  10: PS C:> function foo {param([Parameter(ValueFromPipeline=$true)]$a) "foo $a"}
  11: PS C:> "bar" | foo
  12: foo bar
  13: PS C:> foo bar
  14: foo bar
  15: PS C:> foo -a bar
  16: foo bar
  17: PS C:>

Just by setting some attributes on a parameter, you can get all kinds of great functionality for free.

Jeffrey’s function “Test-LeapYear” is a great example or template to start with. I really like how he used multi-line comments for all the Help documentation. Very slick.

Comments are closed