powershell - 如何禁用/覆盖 PowerShell 点符号

标签 powershell

PowerShell 中的命令几乎与 Bash 类似,但点符号扩展为我带来了很多工作。目前我必须用引号括起很多命令参数:

.\mvnw.cmd -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/-DrepositoryId=nexus -DgroupId=com .zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar

成为

.\mvnw.cmd -D"maven.repo.local"=".m2/repository"deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/-DrepositoryId= nexus -DgroupId="com.zippo"-DartifactId=test -Dversion="1.0"-Dpackaging=jar -Dfile="test-1.0.jar"

如何禁用点符号,或覆盖点运算符,将其替换为其他内容等?

最佳答案

How can I disable dot notation?

  • 问题不在于点符号,它是 PowerShell 如何解析要传递给外部程序的参数的一个错误:一个 token ,例如 -foo.bar 分为两个 参数,-foo.bar - 参见GitHub issue #6291 (意外重复:GitHub issue #15541);这只是其中之一 several related bugs , 至少支持 PowerShell 7.2.6。

  • 确实需要引用来解决这个问题,但是您可以通过将每个受影响的参数作为一个整体包含在中来简化它'...'(或 "...",如果您需要字符串插值):

    .\mvnw.cmd '-Dmaven.repo.local=.m2/repository' 'deploy:deploy-file' -'Durl=http://zippo:8081/repository/grinch/' '-DrepositoryId=nexus' -'DgroupId=com.zippo' '-DartifactId=test' '-Dversion=1.0' '-Dpackaging=jar' '-Dfile=test-1.0.jar'
    

备选方案:

  • 您可以使用--%stop-parsing token ,这避免了引用的需要,但请注意它的许多限制,特别是无法(直接)将 PowerShell 变量和表达式合并到命令中 - 参见 this answer了解详情。

    # --% passes the remainder of the command line as-is to the target program
    .\mvnw.cmd --% -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar
    
  • 您可以通过 cmd/c 调用,在这种情况下您可以引用您的整个命令行,从而避免解析漏洞;请注意,由 cmd.exe 中继的任何输出都会根据存储在 [Console]::OutputEncoding 中的字符编码进行解码,因此除非 mvnw 将此编码用于其输出,您可能必须(暂时)先将 [Console]::OutputEncoding 设置为实际编码。

    cmd /c '.\mvnw.cmd --% -Dmaven.repo.local=.m2/repository deploy:deploy-file -Durl=http://zippo:8081/repository/grinch/ -DrepositoryId=nexus -DgroupId=com.zippo -DartifactId=test -Dversion=1.0 -Dpackaging=jar -Dfile=test-1.0.jar'
    

关于powershell - 如何禁用/覆盖 PowerShell 点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66794981/

相关文章:

powershell - 将数组匹配到多个正则表达式(来自数组)

powershell - 如何确保在停止信号时对高级函数的局部变量调用 Dispose()?

powershell - 如何在 PowerShell 中重命名文件?

powershell - 比较文件哈希并输出文件

powershell - 将小数点数字转换为整数 - PowerShell

powershell - 如何在 PowerShell 中创建私有(private)类成员?

powershell - Nuget.exe 安装 — Microsoft.CSharp 已经具有 System.Dynamic.Runtime 的依赖项

java - Cygwin 运行的 java 版本与 windows powershell 不同

powershell - 如何使用 azure CLI 和 power shell 脚本实现自动登录,无需用户交互从 VSTS 进入 azure 门户

linux - 尝试使用 PowerShell 从我的本地计算机复制文本文件内容并将其添加到 Linux 服务器中的另一个文件