为什么 PowerShell 拆分包含连字符和句点的参数? [英] Why does PowerShell split arguments containing hyphens and periods?

查看:32
本文介绍了为什么 PowerShell 拆分包含连字符和句点的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 窗口中:

In a PowerShell window:

PS C:\> echo -abc.def.ghi
-abc
.def.ghi

出于某种原因,连字符和句点的组合导致 Powershell 将参数分成两行.

For some reason, the combination of a hyphen and period cause Powershell to split the argument into two lines.

没有连字符就不会发生:

It does not occur with without the hyphen:

PS C:\> echo abc.def.ghi
abc.def.ghi

没有句点时也不会发生:

Nor does it occur when there are no periods:

PS C:\> echo -abcdefghi
-abcdefghi

通过实验,我发现我可以通过反引号来避免这种行为:

Through experimentation, I've found I can escape the behavior with a backtick:

PS C:\> echo `-abc.def.ghi
-abc.def.ghi

但是为什么会出现这种情况?我不理解 PowerShell 语法的哪些基本部分?

But why does this occur? What fundamental part of PowerShell syntax am I not understanding?

推荐答案

我已经反汇编了 Microsoft.PowerShell.Utility 来查看 Write-Output 的代码没有什么特别的,它只是迭代InputObject 并将每个传递给由当前 ICommandRuntime 实现的 WriteObject 方法.

I've disassembled Microsoft.PowerShell.Utility to look at the code of Write-Output nothing special there, it just iterates through InputObject and passes each to a WriteObject method implemented by the current ICommandRuntime.

我的猜测是,处理文本的标记器会尝试将任何以 - 开头的内容与声明的参数匹配.如果失败,它会将其作为 -InputObject 中的一个项目通过管道.由于 . 不能成为变量名称的一部分,因此不能成为开关名称的一部分,它可能会在执行 if 检查之前将其分开,当它成为参数时,它不会t 将它与令牌的其余部分一起备份.因此,您可能发现了一个小错误.

My guess is that the tokenizer that process the text tries to match anything starting with a - to a declared parameter. Failing that, it passes it through the pipeline as an item in -InputObject. Since a . cannot be a part of a variable name and therefore can't be part of a switch name it might separate it before doing the if check and when it turns out to be a parameter it doesn't join it back up with the rest of the token. You might therefore have found a minor bug.

当使用反勾号或引号时,它不会犯这个错误,因为它可以标记整个事物.

When using the back tick or quotation marks it doesn't make this mistake however since it can tokenize the entire thing.

这都是猜想,我和你一样希望看到权威的答案.

This is all conjecture though, I'd love to see a authoritative answer as much as you.

编辑

我所说的证据:

PS> echo -NoEnumerate.foo
.foo

这篇关于为什么 PowerShell 拆分包含连字符和句点的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆