温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodejs渐入佳境[11]-yargs获取用户输入高级用法

发布时间:2020-07-05 10:26:24 来源:网络 阅读:266 作者:jonson_jackson 栏目:开发技术

yargs高级用法,用于输出帮助信息,缩写提示等。

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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//打印字符串
const yargs = require('yargs');
const nodes = require('./nodes.js')

const argv = yargs
 .command('add','add a new note',{   //add为命令参数,第二个为说明
   title:{       //--titile
     describe:'Title of note',
     demand:true, //必须要的参数
     alias:'t'  //缩写 //--t
   },
   body:{
     describe:'Body of note',
     demand:true,
     alias:'b'
   }
 })
 .command('list','List all notes')
 .command('read','Read a note',{
   title:{
     describe:'Title of note',
     demand:true,
     alias:'t'
   }
 })
 .command('remove','Remove a note',{
   title:{
     describe:'Title of note',
     demand:true,
     alias:'t'
   }
 })
 .help()
 .argv;
var command = process.argv[2];

if(command==='add'){
  var note = nodes.addNote(argv.title,argv.body);
 if(note){
   console.log('add success');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }
}else if(command === 'list'){
   var allnotes =  nodes.getAll();
   allnotes.forEach((note)=>{  console.log(note)});
}else if(command =='read'){
 var note = nodes.getNote(argv.title);
 if(note){
   console.log('find');
   console.log(`title:${note.title}`);
   console.log(`body:${note.body}`);
 }else{
   console.log('note not found');
 }
}else if(command=='remove'){
 var noteRemoved =  nodes.removeNote(argv.title);
 var message = noteRemoved?'Note was removed':'note not found';
 console.log(message);
}else{
 console.log('command not find');
}

nodes.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
console.log('start nodes.js');
const fs = require('fs');
//从文件中获取节点
var fetchNode = ()=>{
 try{
   var notesString = fs.readFileSync('notes-data.json');
   return JSON.parse(notesString);
 }catch(e){
   return [];
 }
}
//保存节点到文件
var saveNote = (notes)=>{

 fs.writeFileSync('notes-data.json',JSON.stringify(notes));

}

//增加节点,如果新增返回新增节点。
var addNote = (title,body)=>{
 var notes = fetchNode();
 var note = {
     title,
     body
 };

 //筛选出相同的节点
 var duplicateNotes = notes.filter((note)=>note.title===title);
 //没有相同的节点
 if(duplicateNotes.length ===0){
   notes.push(note);
   saveNote(notes);
   return note;
 }
}

var getAll = ()=>{
 var notes = fetchNode();
 return notes;
};

var getNote = (title)=>{

 var notes = fetchNode();
 //筛选出相同的节点
 var duplicateNotes = notes.filter((note)=>note.title===title);
 return duplicateNotes[0];
};

var removeNote = (title)=>{
 var notes = fetchNode();
 //筛选出不同的节点
 var duplicateNotes = notes.filter((note)=>note.title!==title);
   saveNote(duplicateNotes);
   return notes.length !==duplicateNotes.length;
};

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

测试

打开控制台,在当前目录下输入:

1
> node app.js add --help

控制台返回结果:

1
2
3
4
5
6
7
8
9
app.js add

add a new note

选项:
 --version    显示版本号                                                 [布尔]
 --help       显示帮助信息                                               [布尔]
 --title, -t  Title of note                                              [必需]
 --body, -b   Body of note                                               [必需]

输入:

1
> node app.js  --help

控制台返回结果:

1
2
3
4
5
6
7
8
9
10
11
app.js [命令]

命令:
 app.js add     add a new note
 app.js list    List all notes
 app.js read    Read a note
 app.js remove  Remove a note

选项:
 --version  显示版本号                                                   [布尔]
 --help     显示帮助信息                                                 [布尔]
  • 本文链接: https://dreamerjonson.com/2018/11/14/node-11-yargs/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

nodejs渐入佳境[11]-yargs获取用户输入高级用法

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI