Powershell命令行参数和' - ' [英] Powershell command line parameters and '--'
问题描述
例如,我提供命令行:
。 \abc.exe foo.txt - bar -
当我调用这个时,C#程序main只获取命令行args:
foo.txt bar -
而不是
foo.txt - bar - -
。
任何人都知道为什么会发生这种情况。
BTW,如果我把它称为:
code> .\abc.exe foo.txt' - 'bar' - '
<
<$ c $ c>& .\abc.exe foo.txt - bar -
似乎没有帮助。
我认为这是一个powershell奇怪的原因是,如果我从CMD.EXE运行相同的命令行一切正常工作。
双连字符指示PowerShell将后面的所有内容视为文字参数而不是选项,以便可以传递实例 foo
到您的脚本/ application / cmdlet。
示例:
PS C:\>回声-bar| select-string -bar
Select-String:找不到与参数名称bar匹配的参数。
在行:1个字符:28
+-bar| select-string -bar<<<<
+ CategoryInfo:InvalidArgument:(:) [Select-String],ParameterBindingException
+ FullyQualifiedErrorId:NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand
vs。
PS C:\>回声-bar| select-string - -bar
-bar
您必须引用( -
,' - '
)或escape( ` -
)双连字符。
For whatever reason, when I try to call a C# program I'm writing, and I try to pass two arguments with '--' in the command line, Powershell doesn't call the program with my command line.
For instance, I'm providing the command line:
.\abc.exe foo.txt -- bar --
When I call this, the C# program's main only gets the command line args:
foo.txt bar --
instead of
foo.txt -- bar --
as would be expected.
Anybody know why this would be happening?
BTW, if I call it as:
.\abc.exe foo.txt '--' bar '--'
it works as expected.
Also, calling it as:
& .\abc.exe foo.txt -- bar --
Doesn't seem to help.
My reason for thinking this is a powershell weirdness is that if I run the same command line from CMD.EXE everything works as expected.
A double hyphen instructs PowerShell to treat everything coming after as literal arguments rather than options, so that you can pass for instance a literal -foo
to your script/application/cmdlet.
Example:
PS C:\> echo "-bar" | select-string -bar
Select-String : A parameter cannot be found that matches parameter name 'bar'.
At line:1 char:28
+ "-bar" | select-string -bar <<<<
+ CategoryInfo : InvalidArgument: (:) [Select-String], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand
vs.
PS C:\> echo "-bar" | select-string -- -bar
-bar
To avoid this behavior you must either quote ("--"
, '--'
) or escape (`--
) the double hyphen.
这篇关于Powershell命令行参数和' - '的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!