[英]Getting the value from command line input, command-line-args NPM module, node.js/javascript
我使用此代碼添加-find標志,但我找不到如何在可用變量中獲取值。
const commandLineArgs = require('command-line-args')
const quicksearch = [
{name: 'find', alias: 'f',type: String}
]
我想在終端中實現這一點,-find = github,然后在一個可用的變量中使用find標志的值,所以我可以將它發送到服務器,我已經閱讀了文檔但是沒有關於它。
運行命令-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'
我喜歡yargs庫 。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.