Node.js 带有可选+可变参数的指挥官
Node.js commander with optional+variadic arguments
我正在努力让节点的 commander
模块以我想要的方式解析参数。
我想将文件列表上传到指定的数据库。有一个默认数据库名称,因此用户不应该需要包含数据库参数。
我希望此命令按如下方式工作:
>>> ./upload.js --db ReallyCoolDB /files/uploadMe1.txt /files/uploadMe2.txt
(uploads "uploadMe1.txt" and "uploadMe2.txt" to database "ReallyCoolDB")
>>> ./upload.js /files/uploadMe1.txt /files/uploadMe2.txt
(uploads "uploadMe1.txt" and "uploadMe2.txt" to the default database)
>>> ./upload.js --db ReallyCoolDB
(returns an error; no files provided)
如何使用 commander
实现此功能?我已经尝试了很多事情,目前我被这个不起作用的代码所困:
// upload.js:
#!/usr/bin/env node
var program = require('commander');
program
.version('0.1.0')
.description('Upload files to a database')
.command('<path1> [morePaths...]')
.option('-d, --db [dbName]', 'Optional name of db', null)
.action(function(path1, morePaths) {
// At this point I simply want:
// 1) a String "dbName" var
// 2) an Array "paths" containing all the paths the user provided
var dbName = program.db || getDefaultDBName();
var paths = [ path1 ].concat(morePaths || []);
console.log(dbName, paths);
// ... do the upload ...
})
.parse(process.argv);
当我尝试 运行 ./upload.js
时,我没有输出!
如何使用 commander 接受单个可选参数和非空字符串列表??
编辑:感谢 Rob Raisch 的回答,我已经解决了我的问题!解决方案是使用 usage
而不是 action
,在 program
命令之后完成所有工作(而不是在 action
函数内),使用 program.db
和program.args
,并手动确保program.args
非空:
var program = require('commander');
program
.version('0.1.0')
.description('Upload files to a database')
.usage('[options] <path1> [morePaths ...]') // This improves "--help" output
.option('-d, --db [dbName]', 'Optional name of db', null)
.parse(process.argv);
var dbName = program.db || getDefaultDBName();
var paths = program.args;
if (!paths.length) {
console.log('Need to provide at least one path.');
process.exit(1);
}
// Do the upload!
commander
命令行处理模块的 README.md 文件在其第二段中回答了您的用例:
"Options with commander are defined with the .option() method, also serving as documentation for the options. The example below parses args and options from process.argv, leaving remaining args as the program.args array which were not consumed by options."
我正在努力让节点的 commander
模块以我想要的方式解析参数。
我想将文件列表上传到指定的数据库。有一个默认数据库名称,因此用户不应该需要包含数据库参数。
我希望此命令按如下方式工作:
>>> ./upload.js --db ReallyCoolDB /files/uploadMe1.txt /files/uploadMe2.txt
(uploads "uploadMe1.txt" and "uploadMe2.txt" to database "ReallyCoolDB")
>>> ./upload.js /files/uploadMe1.txt /files/uploadMe2.txt
(uploads "uploadMe1.txt" and "uploadMe2.txt" to the default database)
>>> ./upload.js --db ReallyCoolDB
(returns an error; no files provided)
如何使用 commander
实现此功能?我已经尝试了很多事情,目前我被这个不起作用的代码所困:
// upload.js:
#!/usr/bin/env node
var program = require('commander');
program
.version('0.1.0')
.description('Upload files to a database')
.command('<path1> [morePaths...]')
.option('-d, --db [dbName]', 'Optional name of db', null)
.action(function(path1, morePaths) {
// At this point I simply want:
// 1) a String "dbName" var
// 2) an Array "paths" containing all the paths the user provided
var dbName = program.db || getDefaultDBName();
var paths = [ path1 ].concat(morePaths || []);
console.log(dbName, paths);
// ... do the upload ...
})
.parse(process.argv);
当我尝试 运行 ./upload.js
时,我没有输出!
如何使用 commander 接受单个可选参数和非空字符串列表??
编辑:感谢 Rob Raisch 的回答,我已经解决了我的问题!解决方案是使用 usage
而不是 action
,在 program
命令之后完成所有工作(而不是在 action
函数内),使用 program.db
和program.args
,并手动确保program.args
非空:
var program = require('commander');
program
.version('0.1.0')
.description('Upload files to a database')
.usage('[options] <path1> [morePaths ...]') // This improves "--help" output
.option('-d, --db [dbName]', 'Optional name of db', null)
.parse(process.argv);
var dbName = program.db || getDefaultDBName();
var paths = program.args;
if (!paths.length) {
console.log('Need to provide at least one path.');
process.exit(1);
}
// Do the upload!
commander
命令行处理模块的 README.md 文件在其第二段中回答了您的用例:
"Options with commander are defined with the .option() method, also serving as documentation for the options. The example below parses args and options from process.argv, leaving remaining args as the program.args array which were not consumed by options."