用节点指挥官解析引号
parsing quotes with node commander
我正在使用 node commander 编写命令行界面,但一直在解析参数中的单引号。我搜索了文档和 Whosebug,但没有找到任何结论。
有没有办法传入包含单引号的参数?
抽象掉我所知道的工作,我剩下的是一个 CLI,其中 运行:
node test.js command 'value''s values'
使用以下程序(信用 here,用于一个简单的功能示例):
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('command <req> [optional]')
.action(function(req,optional){
console.log('User passed %s', req);
});
program.parse(process.argv);
结果是 User passed values values
,它缺少参数第一个单词的单引号。期望的输出是 User passed value's values
.
您需要转义撇号。这将起作用:
node test.js command 'value'\''s values'
这之所以有效,是因为 "strong quoting"。您可以阅读更多相关信息 here。
强引号意味着这也有效:
node test.js command "value's values"
我正在使用 node commander 编写命令行界面,但一直在解析参数中的单引号。我搜索了文档和 Whosebug,但没有找到任何结论。
有没有办法传入包含单引号的参数?
抽象掉我所知道的工作,我剩下的是一个 CLI,其中 运行:
node test.js command 'value''s values'
使用以下程序(信用 here,用于一个简单的功能示例):
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('command <req> [optional]')
.action(function(req,optional){
console.log('User passed %s', req);
});
program.parse(process.argv);
结果是 User passed values values
,它缺少参数第一个单词的单引号。期望的输出是 User passed value's values
.
您需要转义撇号。这将起作用:
node test.js command 'value'\''s values'
这之所以有效,是因为 "strong quoting"。您可以阅读更多相关信息 here。
强引号意味着这也有效:
node test.js command "value's values"