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

Argument that starts with '-' and contains ':' gets separated.

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-11-2007   #1 (permalink)
Dan
Guest


 

Argument that starts with '-' and contains ':' gets separated.

I discovered that when I execute a program from the PS command line
and I try to pass an argument that starts with a hyphen (-) and
contains a colon (, the argument will be split into 2 arguments at
the colon.

To demonstrate, I wrote a simple python script then merely echoes out
all the arguments in argv:

PS C:\3rdParty> .\testPSArgs.py c:\goodbye
arg: C:\3rdParty\testPSArgs.py
arg: c:\goodbye
PS C:\3rdParty> .\testPSArgs.py =c:\goodbye
arg: C:\3rdParty\testPSArgs.py
arg: =c:\goodbye
PS C:\3rdParty> .\testPSArgs.py hello=c:\goodbye
arg: C:\3rdParty\testPSArgs.py
arg: hello=c:\goodbye
PS C:\3rdParty> .\testPSArgs.py -hello=c:\goodbye
arg: C:\3rdParty\testPSArgs.py
arg: -hello=c:
arg: \goodbye

Now, of course, the workaround is fairly simple, merely enclose the
argument in quotes:
PS C:\3rdParty> .\testPSArgs.py "-hello=c:\goodbye"
arg: C:\3rdParty\testPSArgs.py
arg: -hello=c:\goodbye

I was just wondering, why does it parse the command line in this way?

thanks,
Dan


My System SpecsSystem Spec
Old 05-11-2007   #2 (permalink)
Don Jones [MVP]
Guest


 

Re: Argument that starts with '-' and contains ':' gets separated.

Just because .

Actually, it's probably the space (or lack thereof) having more of an
effect. PowerShell treats spaces as delimiters; it's not so much a "-" that
it looks for to begin an argument name, but rather " -" (that is, a space
and a dash). It looks for the next space to seperate the argument name and
value. The colon is obviously another character it "looks" for - I don't
think it wants to see them in argument names, and in your last example it
would "think" that the colon was part of the argument name (no spaces) so
"broke" it there.

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Dan" <dz.open@gmail.com> wrote in message
news:1178921284.945251.87840@u30g2000hsc.googlegroups.com...
>I discovered that when I execute a program from the PS command line
> and I try to pass an argument that starts with a hyphen (-) and
> contains a colon (, the argument will be split into 2 arguments at
> the colon.
>
> To demonstrate, I wrote a simple python script then merely echoes out
> all the arguments in argv:
>
> PS C:\3rdParty> .\testPSArgs.py c:\goodbye
> arg: C:\3rdParty\testPSArgs.py
> arg: c:\goodbye
> PS C:\3rdParty> .\testPSArgs.py =c:\goodbye
> arg: C:\3rdParty\testPSArgs.py
> arg: =c:\goodbye
> PS C:\3rdParty> .\testPSArgs.py hello=c:\goodbye
> arg: C:\3rdParty\testPSArgs.py
> arg: hello=c:\goodbye
> PS C:\3rdParty> .\testPSArgs.py -hello=c:\goodbye
> arg: C:\3rdParty\testPSArgs.py
> arg: -hello=c:
> arg: \goodbye
>
> Now, of course, the workaround is fairly simple, merely enclose the
> argument in quotes:
> PS C:\3rdParty> .\testPSArgs.py "-hello=c:\goodbye"
> arg: C:\3rdParty\testPSArgs.py
> arg: -hello=c:\goodbye
>
> I was just wondering, why does it parse the command line in this way?
>
> thanks,
> Dan
>


My System SpecsSystem Spec
Old 05-13-2007   #3 (permalink)
William Stacey [C# MVP]
Guest


 

Re: Argument that starts with '-' and contains ':' gets separated.

In psh, you can use a space or a colon to seperate the parm name from the
value of the parm:

# Use a space or a colon
mycmd -namene -name2:$var1 -name3 $var2

So in your last example your parm name is everything upto the colon
(i.e. -hello=c and the value is everything after the colon upto the first
space. So in terms of a psh script, this is expected behavior.
If you have a Param that matches the parm name on the cmd line, that param
will be set with the value.

# parms.ps1 test script
#
param(${hello=c}) # We have the use the ${} syntax to hide the "="
'$hello=c value: ' + ${hello=c}
"Args value: $args" # everything left over not pluged into a param

Test
---------
PS C:\> .\parms.ps1 one two three
$hello=c value: one
Args value: two three

PS C:\> .\parms.ps1 two -hello=cne
$hello=c value: one
Args value: two

Note in both cmd line example the var $hello=c is set to one. The first
case is because "one" is Position 1. In the second example, the var is set
to one because it *matches the Param name of "hello=c".

--
William Stacey [C# MVP]


"Dan" <dz.open@gmail.com> wrote in message
news:1178921284.945251.87840@u30g2000hsc.googlegroups.com...
|I discovered that when I execute a program from the PS command line
| and I try to pass an argument that starts with a hyphen (-) and
| contains a colon (, the argument will be split into 2 arguments at
| the colon.
|
| To demonstrate, I wrote a simple python script then merely echoes out
| all the arguments in argv:
|
| PS C:\3rdParty> .\testPSArgs.py c:\goodbye
| arg: C:\3rdParty\testPSArgs.py
| arg: c:\goodbye
| PS C:\3rdParty> .\testPSArgs.py =c:\goodbye
| arg: C:\3rdParty\testPSArgs.py
| arg: =c:\goodbye
| PS C:\3rdParty> .\testPSArgs.py hello=c:\goodbye
| arg: C:\3rdParty\testPSArgs.py
| arg: hello=c:\goodbye
| PS C:\3rdParty> .\testPSArgs.py -hello=c:\goodbye
| arg: C:\3rdParty\testPSArgs.py
| arg: -hello=c:
| arg: \goodbye
|
| Now, of course, the workaround is fairly simple, merely enclose the
| argument in quotes:
| PS C:\3rdParty> .\testPSArgs.py "-hello=c:\goodbye"
| arg: C:\3rdParty\testPSArgs.py
| arg: -hello=c:\goodbye
|
| I was just wondering, why does it parse the command line in this way?
|
| thanks,
| Dan
|


My System SpecsSystem Spec
Old 05-14-2007   #4 (permalink)
DZ
Guest


 

Re: Argument that starts with '-' and contains ':' gets separated.


Thanks, I understand now.

Though, I must say that I find this behavior a little annoying if you
must invoke non-powershell scripts or executables, especially if you
are used to other command line interfaces. I spent quite a bit of time
trying to figure out why my command wasn't working properly.

It would be nice if PS could somehow parse the arguments differently
depending on what the target of the arguments are. For example, if you
are not invoking a cmdlet or PS script, the parse the command line in
the "conventional" way.

Dan


On May 13, 6:55 pm, "William Stacey [C# MVP]"
<william.sta...@gmail.com> wrote:
> In psh, you can use a space or a colon to seperate the parm name from the
> value of the parm:
>
> # Use a space or a colon
> mycmd -namene -name2:$var1 -name3 $var2
>
> So in your last example your parm name is everything upto the colon
> (i.e. -hello=c and the value is everything after the colon upto the first
> space. So in terms of a psh script, this is expected behavior.
> If you have a Param that matches the parm name on the cmd line, that param
> will be set with the value.
>
> # parms.ps1 test script
> #
> param(${hello=c}) # We have the use the ${} syntax to hide the "="
> '$hello=c value: ' + ${hello=c}
> "Args value: $args" # everything left over not pluged into a param
>
> Test
> ---------
> PS C:\> .\parms.ps1 one two three
> $hello=c value: one
> Args value: two three
>
> PS C:\> .\parms.ps1 two -hello=cne
> $hello=c value: one
> Args value: two
>
> Note in both cmd line example the var $hello=c is set to one. The first
> case is because "one" is Position 1. In the second example, the var is set
> to one because it *matches the Param name of "hello=c".
>
> --
> William Stacey [C# MVP]
>
> "Dan" <dz.o...@gmail.com> wrote in message
>
> news:1178921284.945251.87840@u30g2000hsc.googlegroups.com...
> |I discovered that when I execute a program from the PS command line
> | and I try to pass an argument that starts with a hyphen (-) and
> | contains a colon (, the argument will be split into 2 arguments at
> | the colon.
> |
> | To demonstrate, I wrote a simple python script then merely echoes out
> | all the arguments in argv:
> |
> | PS C:\3rdParty> .\testPSArgs.py c:\goodbye
> | arg: C:\3rdParty\testPSArgs.py
> | arg: c:\goodbye
> | PS C:\3rdParty> .\testPSArgs.py =c:\goodbye
> | arg: C:\3rdParty\testPSArgs.py
> | arg: =c:\goodbye
> | PS C:\3rdParty> .\testPSArgs.py hello=c:\goodbye
> | arg: C:\3rdParty\testPSArgs.py
> | arg: hello=c:\goodbye
> | PS C:\3rdParty> .\testPSArgs.py -hello=c:\goodbye
> | arg: C:\3rdParty\testPSArgs.py
> | arg: -hello=c:
> | arg: \goodbye
> |
> | Now, of course, the workaround is fairly simple, merely enclose the
> | argument in quotes:
> | PS C:\3rdParty> .\testPSArgs.py "-hello=c:\goodbye"
> | arg: C:\3rdParty\testPSArgs.py
> | arg: -hello=c:\goodbye
> |
> | I was just wondering, why does it parse the command line in this way?
> |
> | thanks,
> | Dan
> |



My System SpecsSystem Spec
Old 05-15-2007   #5 (permalink)
William Stacey [C# MVP]
Guest


 

Re: Argument that starts with '-' and contains ':' gets separated.

How would it know what your intent is? As far as psh is concerned, your in
the psh business. What you start after that is your business. They could
probably add some other switch, to not do any parsing or not parse if there
is no matching parm name (which would seem reasonable here), but that starts
getting into one offs - and one-offs get very complex fast and start
weighing down the whole product. Like you said, the workaround is pretty
simple and explicit.

--
William Stacey [C# MVP]


"DZ" <noah.boy@gmail.com> wrote in message
news:1179171654.654985.150950@l77g2000hsb.googlegroups.com...
|
| Thanks, I understand now.
|
| Though, I must say that I find this behavior a little annoying if you
| must invoke non-powershell scripts or executables, especially if you
| are used to other command line interfaces. I spent quite a bit of time
| trying to figure out why my command wasn't working properly.
|
| It would be nice if PS could somehow parse the arguments differently
| depending on what the target of the arguments are. For example, if you
| are not invoking a cmdlet or PS script, the parse the command line in
| the "conventional" way.
|
| Dan
|
|
| On May 13, 6:55 pm, "William Stacey [C# MVP]"
| <william.sta...@gmail.com> wrote:
| > In psh, you can use a space or a colon to seperate the parm name from
the
| > value of the parm:
| >
| > # Use a space or a colon
| > mycmd -namene -name2:$var1 -name3 $var2
| >
| > So in your last example your parm name is everything upto the colon
| > (i.e. -hello=c and the value is everything after the colon upto the
first
| > space. So in terms of a psh script, this is expected behavior.
| > If you have a Param that matches the parm name on the cmd line, that
param
| > will be set with the value.
| >
| > # parms.ps1 test script
| > #
| > param(${hello=c}) # We have the use the ${} syntax to hide the "="
| > '$hello=c value: ' + ${hello=c}
| > "Args value: $args" # everything left over not pluged into a param
| >
| > Test
| > ---------
| > PS C:\> .\parms.ps1 one two three
| > $hello=c value: one
| > Args value: two three
| >
| > PS C:\> .\parms.ps1 two -hello=cne
| > $hello=c value: one
| > Args value: two
| >
| > Note in both cmd line example the var $hello=c is set to one. The first
| > case is because "one" is Position 1. In the second example, the var is
set
| > to one because it *matches the Param name of "hello=c".
| >
| > --
| > William Stacey [C# MVP]
| >
| > "Dan" <dz.o...@gmail.com> wrote in message
| >
| > news:1178921284.945251.87840@u30g2000hsc.googlegroups.com...
| > |I discovered that when I execute a program from the PS command line
| > | and I try to pass an argument that starts with a hyphen (-) and
| > | contains a colon (, the argument will be split into 2 arguments at
| > | the colon.
| > |
| > | To demonstrate, I wrote a simple python script then merely echoes out
| > | all the arguments in argv:
| > |
| > | PS C:\3rdParty> .\testPSArgs.py c:\goodbye
| > | arg: C:\3rdParty\testPSArgs.py
| > | arg: c:\goodbye
| > | PS C:\3rdParty> .\testPSArgs.py =c:\goodbye
| > | arg: C:\3rdParty\testPSArgs.py
| > | arg: =c:\goodbye
| > | PS C:\3rdParty> .\testPSArgs.py hello=c:\goodbye
| > | arg: C:\3rdParty\testPSArgs.py
| > | arg: hello=c:\goodbye
| > | PS C:\3rdParty> .\testPSArgs.py -hello=c:\goodbye
| > | arg: C:\3rdParty\testPSArgs.py
| > | arg: -hello=c:
| > | arg: \goodbye
| > |
| > | Now, of course, the workaround is fairly simple, merely enclose the
| > | argument in quotes:
| > | PS C:\3rdParty> .\testPSArgs.py "-hello=c:\goodbye"
| > | arg: C:\3rdParty\testPSArgs.py
| > | arg: -hello=c:\goodbye
| > |
| > | I was just wondering, why does it parse the command line in this way?
| > |
| > | thanks,
| > | Dan
| > |
|
|


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
P4P invalid argument encountered nikro Vista General 3 07-27-2008 02:16 AM
Convert CSV semi-colone to comma separated Gregor PowerShell 3 04-10-2008 09:25 AM
Using @ as argument Anders PowerShell 8 01-21-2008 03:23 PM
do something for each token in a line separated by comma MaxMad PowerShell 5 01-09-2008 03:00 PM
Notepad starts with "[.ShellClassInfo] computer starts in Vista otho69 Vista General 7 03-10-2007 10:18 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