Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum 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

Command Line Argument Types

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-20-2006   #1 (permalink)
John Smith
Guest


 

Command Line Argument Types

I am retrieveing command line arguments passed to a PS1 script. I find that:

a. ./myscript 5 $args[0].GetType() gives Int32
b. ./myscript -5 $args[0].GetType() gives String
c. ./myscript (-5) $args[0].GetType() gives Int32

Is this how it is supposed to work?

Thanks

My System SpecsSystem Spec
Old 12-21-2006   #2 (permalink)
Lucvdv
Guest


 

Re: Command Line Argument Types

On Wed, 20 Dec 2006 17:58:01 -0800, John Smith
<JohnSmith@discussions.microsoft.com> wrote:

> I am retrieveing command line arguments passed to a PS1 script. I find that:
>
> a. ./myscript 5 $args[0].GetType() gives Int32
> b. ./myscript -5 $args[0].GetType() gives String
> c. ./myscript (-5) $args[0].GetType() gives Int32
>
> Is this how it is supposed to work?


Interesting, I didn't know that.

It's the same with functions, but I suppose most people would use the
parentheses there out of habit and never notice it:

function testit($arg){ $arg.GetType() }


testit 5

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType


testit -5

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object


testit (-5)

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType

My System SpecsSystem Spec
Old 12-21-2006   #3 (permalink)
Jacques Barathon [MS]
Guest


 

Re: Command Line Argument Types

"Lucvdv" <replace_name@null.net> wrote in message
news:h56lo2h8vqnfe9tfs9nh0jgbv95ns967lk@4ax.com...
<...>
> It's the same with functions, but I suppose most people would use the
> parentheses there out of habit and never notice it:


Not a good habit. Be careful with parentheses when you pass arguments to
functions. Parentheses mean that you are passing everything they enclose as
*one* argument that will be evaluated as an expression:

PS> function testarg {
>> "`$args[0]: $($args[0])"
>> "`$args[1]: $($args[1])"
>> }
>>

PS> testarg (1,5)
$args[0]: 1 5
$args[1]:
PS> testarg (1 5)
Unexpected token '5' in expression or statement.
At line:1 char:13
+ testarg (1 5) <<<<
PS> testarg 1 5
$args[0]: 1
$args[1]: 5

Jacques

My System SpecsSystem Spec
Old 12-22-2006   #4 (permalink)
Keith Hill [MVP]
Guest


 

Re: Command Line Argument Types

"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:eK7YNUUJHHA.3668@TK2MSFTNGP02.phx.gbl...
> "Lucvdv" <replace_name@null.net> wrote in message
> news:h56lo2h8vqnfe9tfs9nh0jgbv95ns967lk@4ax.com...
> <...>
>> It's the same with functions, but I suppose most people would use the
>> parentheses there out of habit and never notice it:

>
> Not a good habit. Be careful with parentheses when you pass arguments to
> functions. Parentheses mean that you are passing everything they enclose
> as *one* argument that will be evaluated as an expression:


I *still* get bit by that one from time to time. It might be easier to
remember not to use the parens on the call if it weren't for the fact that
when calling .NET methods you have to use parens (well at least the call
works as expected if you do).

--
Keith


My System SpecsSystem Spec
Old 12-26-2006   #5 (permalink)
Jacques Barathon [MS]
Guest


 

Re: Command Line Argument Types

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
news:uU750zdJHHA.4760@TK2MSFTNGP03.phx.gbl...
<...>
> I *still* get bit by that one from time to time. It might be easier to
> remember not to use the parens on the call if it weren't for the fact that
> when calling .NET methods you have to use parens (well at least the call
> works as expected if you do).


Calling any method of any object always requires parens:

"hello".replace "h","c" >> won't work

Jacques

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Open notepad with a command-line argument Daz VB Script 3 05-19-2008 05:02 AM
Variable arguments and argument types Chris Warwick PowerShell 8 05-28-2007 03:43 AM
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 05-23-2007 03:16 PM
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 05-23-2007 03:15 PM
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 05-23-2007 03:12 PM


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