使用带等号和句点的单连字符参数执行外部命令 [英] Execute external command with single-hypen parameter with equals and period

查看:36
本文介绍了使用带等号和句点的单连字符参数执行外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具可以接受这样的参数(例如在 test.ps1 中):

I have a tool that accepts parameters like this (e.g. in test.ps1):

foo.exe -name="john"

因此,每个参数都用一个连字符 -、名称、等于 = 和参数的值指定.

So, each parameter is specified with a single hyphen -, the name, an equals =, and then the value of the parameter.

当我从 PowerShell 调用这个确切的表达式时,它执行时没有任何问题.但是,当其中一个值包含句点 . 时,如下所示:

When I invoke this exact expression from PowerShell, it executes without any problems. However, when one of the values contains a period . like this:

foo.exe -name="john.doe"

运行它会导致语法错误:

Running it causes a syntax error:

$ ./test.ps1 字符串开始:At测试.ps1:1字符:24+ foo.exe -name="john.doe <<<< " 缺少终止符:".在 test.ps1:1字符:25
+ foo.exe -name="john.doe" <<<<
+ CategoryInfo : ParserError: (:String) [], ParseException
+ FullQualifiedErrorId : TerminatorExpectedAtEndOfString

$ ./test.ps1 The string starting: At test.ps1:1 char:24 + foo.exe -name="john.doe <<<< " is missing the terminator: ". At test.ps1:1 char:25
+ foo.exe -name="john.doe" <<<<
+ CategoryInfo : ParserError: (:String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我可以阻止 PowerShell 解释这一点的一些方法是:

Some ways that I can prevent PowerShell from interpreting this are:

  • foo.exe "-name="john.doe`""
  • foo.exe '-name="john.doe"'
  • PowerShell V3+:foo.exe --% -name="john.doe"
  • $nameArg = "john.doe";foo.exe -name="$nameArg"

但是,其中一些选项会阻止变量的插值.还有其他方法可以阻止 PowerShell 导致语法问题吗?在此特定实例中(添加句点),为什么 PowerShell 会在解释此问题时出现问题?

However, some of these options prevent interpolation of variables. Are there other ways to stop PowerShell from causing syntax issues? In this specific instance (adding the period), why is PowerShell having issues interpreting this?

推荐答案

你看到的是 PSv2 中的一个bug;简而言之:一个未加引号-开头的参数,如果它也包含一个. ,则解析会中断. "...".

What you're seeing is a bug in PSv2; in short: an argument that starts with unquoted - breaks parsing if it also contains a . within "...".

该错误的轻微变体仍然存在于 PowerShell v5.1/PowerShell Core v6.0.1;它已在 GitHub 上报告.现在,"..." 中的 . 工作正常,但 未加引号 . 实际上会中断两个中的参数.下面的解决方法对上述变体也有效 - 请参阅此答案 我的.

A milder variation of the bug still exists in PowerShell v5.1 / PowerShell Core v6.0.1; it has been reported on GitHub. Now, a . inside "..." works fine, but an unquoted . actually breaks the argument in two. The workaround below is also effective for said variation - see this answer of mine.

解决方法引用整个参数:

The workaround is to quote the entire argument:

# Use '...' instead if you don't need interpolation
foo.exe "-name=john.doe"

请注意,通常没有需要单独引用 value 部分 - 即对 目标程序
-name="john.doe 1" 通常与 "-name=john.doe 1"

Note that there is typically no need to quote the value part individually - that is, to the target program,
-name="john.doe 1" is usually the same as "-name=john.doe 1"

这篇关于使用带等号和句点的单连字符参数执行外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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