从 Windows PowerShell 运行 node.js 脚本时,参数转义未正确解释

Cas*_*sey 2 windows powershell node.js yargs

给出以下脚本:

const yargs = require('yargs');

const argv =
    yargs
        .usage('Usage: $0 [--whatIf]')
        .alias('d', 'directory')
        .alias('wi', 'whatIf')
        .nargs('d', 1)
        .describe('d', 'alphabetize this directory')
        .describe('whatIf', 'show what would happen if run')
        .demandOption(['d'])
        .argv;

console.log(argv.directory);
Run Code Online (Sandbox Code Playgroud)

如果我像这样从 Windows PowerShell 调用脚本: 我会得到我期望的node .\alphabetizer.js -d 'l:\my folder\Files - Some Files In Here\' --whatIf输出。对于不需要转义的文件夹名称,它可以正常工作,但似乎会因转义而感到困惑。l:\my folder\Files - Some Files In Here\" --whatIfl:\my folder\Files - Some Files In Here\

如果我检查process.argv,我可以看到相同的转义问题。

我注意到,如果删除尾部斜杠,它就会起作用。然而,这仍然表明节点脚本没有正确处理输入,因为对于用单引号引起来的字符串来说这不是必需的。

有办法让这项工作发挥作用吗?

mkl*_*nt0 5

Windows PowerShell ( powershell.exe) 和 PowerShell (Core) ( pwsh) 直至 v7.2.x在正确引用外部程序的参数方面都从根本上被破坏-有关背景信息,请参阅此答案。

\n

一般来说,Windows 上的 PowerShell 必须在幕后执行重新引用,以确保仅使用 - 引用,因为外部程序在解析其命令行时"..."不能假定也理解- 引用(在 Windows 上每个\'...\'程序必须自己完成)。

\n

对于以 结尾\\ 嵌入空格的参数, Windows PowerShell 的问题更加严重,无法正确地重新引用它们;例如:

\n
PS> foo.exe \'c:\\foo \\\' bar\n
Run Code Online (Sandbox Code Playgroud)\n

在幕后被翻译成以下命令行:

\n
foo.exe "c:\\ foo \\" bar\n
Run Code Online (Sandbox Code Playgroud)\n

这是错误的,因为大多数应用程序 - 包括 PowerShell自己的 CLI - 明智地假设\\"转义 "字符。逐字记录,认为争论仍在继续\xc2\xa0bar,然后隐含地结束,尽管没有正式的结束语"

\n

PowerShell (Core) v6+ 更明智地将上面的内容翻译为foo.exe "c:\\foo \\\\" bar,其中\\\\被解释为转义的\\,下面的内容"再次具有语法功能。

\n
\n

如果您无法使用Windows PowerShell,您唯一的选择是:

\n
    \n
  • 要么:如果可能,省略尾随\\
  • \n
  • 否则:手动将其加倍( \\\\),但仅当参数也包含空格时才这样做(否则,将按\\\\原样保留,尽管文件系统路径通常是良性的)。
  • \n
\n