npm install --save yargs原始文件app.js:12345678const yargs = require('yargs');con..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nodejs漸入佳境[7]-yargs處理命令行參數(shù)

發(fā)布時(shí)間:2020-07-23 15:07:37 來源:網(wǎng)絡(luò) 閱讀:347 作者:jonson_jackson 欄目:開發(fā)技術(shù)

yargs用于方便的處理命令行參數(shù)。導(dǎo)入yargs包,在項(xiàng)目路徑下:

1
> npm install --save yargs

原始文件

app.js:

1
2
3
4
5
6
7
8
const yargs = require('yargs');
console.log('Start app.');

console.log(process.argv);

console.log('yargs',yargs.argv);

var command = process.argv[2];

獲取用戶輸入

打開控制臺(tái),在當(dāng)前目錄下輸入:

1
> node app.js remove --title="buy book"

輸出字符串

1
2
3
4
5
6
7
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'remove',
 '--title=buy book' ]
yargs { _: [ 'remove' ], title: 'buy book', '$0': 'app.js' }
Remove node

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//打印字符串
const yargs = require('yargs');
const nodes = require('./nodes.js')
console.log('Start app.');

console.log(process.argv);

console.log('yargs',yargs.argv);
const argv = yargs.argv;
var command = process.argv[2];

if(command==='add'){
 nodes.addNote(argv.title,argv.body);
}else if(command === 'list'){
 nodes.getAll();

}else if(command =='read'){
 nodes.getNote(argv.title);
}else if(command=='remove'){
 nodes.removeNote(argv.title);
}else{
 console.log('command not find');
}

notes.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
console.log('start nodes.js');

var addNote = (title,body)=>{
console.log('Adding note',title,body);
}

var getAll = ()=>{
console.log('Get All notes');
};

var getNote = (title)=>{

 console.log('getting note',title);
};

var removeNote = (title)=>{
 console.log('Removing note',title);
};

module.exports = {
   addNote,
   getAll,
   getNote,
   removeNote
};

測(cè)試

打開控制臺(tái),在當(dāng)前目錄下輸入:

1
> node app.js remove --title="buy book"

輸出字符串:

1
2
3
4
5
6
7
8
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'remove',
 '--title=buy book' ]
yargs { _: [ 'remove' ], title: 'buy book', '$0': 'app.js' }
Removing note buy book

打開控制臺(tái),在當(dāng)前目錄下輸入:

1
> node app.js add --title="buy book" --body="jonson"

輸出字符串:

1
2
3
4
5
6
7
8
9
10
11
12
start nodes.js
Start app.
[ '/Users/jackson/.nvm/versions/node/v10.13.0/bin/node',
 '/Users/jackson/Desktop/compaign/app.js',
 'add',
 '--title=buy book',
 '--body=jonson' ]
yargs { _: [ 'add' ],
 title: 'buy book',
 body: 'jonson',
 '$0': 'app.js' }
Adding note buy book jonson
  • 本文鏈接: https://dreamerjonson.com/2018/11/13/node-7-yargs/

  • 版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉(zhuǎn)載請(qǐng)注明出處!

nodejs漸入佳境[7]-yargs處理命令行參數(shù)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI