Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

supplying help option and throwing on empty option

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 06-08-2007   #1 (permalink)
Neil Chambers
Guest


 

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
Guest


 

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]
Guest


 

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
Guest


 

Re: supplying help option and throwing on empty option


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

n

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
The DPI option Catmandu Vista performance & maintenance 9 08-14-2008 01:45 PM
Option not available... AzTeacher Vista mail 3 05-12-2008 04:59 PM
No option for mail notification in sounds option BR Vista mail 4 08-01-2007 04:31 PM
RUN option Jack Vista General 4 07-25-2007 05:30 PM
run option Daniel Vista General 7 07-14-2007 07:48 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51