Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - supplying help option and throwing on empty option

Reply
 
Old 06-08-2007   #1 (permalink)
Neil Chambers


 
 

supplying help option and throwing on empty option

Just wanted to poll the collective brain on the best way to go about this . . .

I have a script which takes a number of options at run-time (of which
defaults are stored in 'param'). One of the options is -help which
gives the user a nice help description. However, one of the options
should throw an error if left blank. But if the user issues -help and
the required option is blank then, of course, an exception is thrown.

Is there anything funky I can do within the param collection to get the
desired functionality?

Cheers,
n


My System SpecsSystem Spec
Old 06-08-2007   #2 (permalink)
Kiron


 
 

Re: supplying help option and throwing on empty option

You could make -help a [switch] parameter and check its state to determine
whether or not to throw the exception if -xOption is $null:

param ([switch]$help, $xOption, ...)
if ($xOption -eq $null) {
if ($help) {
# change $help's state to avoid showing it twice
$help = (!$help)
"show help"
}
else {"throw exception"}
}

# show help if its state is $true and $xOption is not $null
if ($help) {"show help"}

--
Kiron

My System SpecsSystem Spec
Old 06-10-2007   #3 (permalink)
Jacques Barathon [MS]


 
 

Re: supplying help option and throwing on empty option

"Neil Chambers" <n3llyb0y@aol.com> wrote in message
news:2007060815331116807-n3llyb0y@aolcom...
> Just wanted to poll the collective brain on the best way to go about this
> . . .
>
> I have a script which takes a number of options at run-time (of which
> defaults are stored in 'param'). One of the options is -help which gives
> the user a nice help description. However, one of the options should throw
> an error if left blank. But if the user issues -help and the required
> option is blank then, of course, an exception is thrown.
>
> Is there anything funky I can do within the param collection to get the
> desired functionality?


You should test the content of your option outside of the param statement.
To avoid running the test before showing the help when -help is specified,
you can write the help section first and finish it with an exit statement so
any further code is skipped. That would look like this:

param ($option1, [switch]$help)
if ($help) {"Help!";exit}
if (! $option1) {throw "Option1 cannot be blank!"}
....

Hope that helps,
Jaqcues

My System SpecsSystem Spec
Old 06-12-2007   #4 (permalink)
Neil Chambers


 
 

Re: supplying help option and throwing on empty option


Thank you, Kiron and Jacques :-) Both excellent options.

n

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
CD Drive "Eject" menu option ... but no "Close" option ... Vista General
Solved How do I create a right-click option for Empty Recycle Bin? General Discussion
The DPI option Vista performance & maintenance
No option for mail notification in sounds option Vista mail
run option Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46