我使用此代码添加-find标志,但我找不到如何在可用变量中获取值。
const commandLineArgs = require('command-line-args')
const quicksearch = [
{name: 'find', alias: 'f',type: String}
]
我想在终端中实现这一点,-find = github,然后在一个可用的变量中使用find标志的值,所以我可以将它发送到服务器,我已经阅读了文档但是theres没什么关系的。
答案 0 :(得分:0)
运行命令-comt / bash,如:
node test.js -f github
或
node test.js --find github
或
node test.js --find=github
期望输出:
{ find: 'github' }
服务器/ Javascript文件( test.js ):
var commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'find', alias: 'f', type: String }
]
const options = commandLineArgs(optionDefinitions)
console.log(options);
//options.find will be equal to 'github'
答案 1 :(得分:0)
我喜欢yargs library。
npm i yargs
//in your script.js
var yargs = require('yargs');
var flag = yargs.argv.nameOfFlag;
//var flag === 'value'
//equivalent to typing this in terminal:
--nameOfFlag value