var minimist = require("minimist")
const a = minimist(`executable --param "a b"`.split(' '))
console.log(a)https://runkit.com/embed/57837xcuv5v0
实际产出:
Object {_: ["executable", "b\""], param: "\"a"}
预期产出:
Object {_: ["executable"], param: "a b"}
在使用yargs和commander时,我也看到了同样的结果。
这很奇怪,因为jest正在使用yargs和jest,请接受以下命令:jest -t "test name with spaces"
发布于 2020-03-13 04:59:30
根据示例代码,问题在于您准备了字符串数组,该数组在解析器看到字符串之前已经将字符串与空格分开:
$ node -e 'console.log(`executable --param "a b"`.split(" "))'
[ 'executable', '--param', '"a', 'b"' ]手动设置参数时,一个简单的修复方法是自己构造参数数组,而不是使用字符串和split,如下所示:
$ node -e 'console.log(["executable", "--param", "a b"])'
[ 'executable', '--param', 'a b' ]或
const a = minimist(['executable', '--param', 'a b'])如果您需要做的是像shell一样将单个字符串分解为参数,那么指挥官、Yargs或minimist就不会这样做。
您可以查看https://www.npmjs.com/package/shell-quote,它有一个解析命令。
https://stackoverflow.com/questions/60657823
复制相似问题