首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么终端不显示其他yargs命令?

为什么终端不显示其他yargs命令?
EN

Stack Overflow用户
提问于 2020-04-19 03:48:44
回答 1查看 216关注 0票数 0

当我运行node app.js --help命令时,终端只显示add命令,同时我定义了三个命令,它们都可以工作。当我使用parse()方法而不是argv时,为什么我会打印两次“删除注释”?

代码语言:javascript
运行
复制
const yargs = require('yargs');


yargs.command({
    command:'add',
    describe:'Adds new note',
    handler:function(){
    console.log('Adding a note')
    }
}).argv;

yargs.command({
    command:'remove',
    describe:'Removes a note',
    handler:function(){
    console.log('Removing a note')
    }
 }).argv;

yargs.command({
    command:'list',
    describe:'Fetches a list of notes',
    handler:function(){
    console.log('Fetching a list')
    }
 }).argv;

Commands:
  app.js add  Adds new note

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
 PS C:\Users\Sanket\Documents\node js\notes-app> 

PS C:\Users\Sanket\Documents\node js\notes-app> node app.js add   

Adding a note
PS C:\Users\Sanket\Documents\node js\notes-app> node app.js remove
Removing a note
Removing a note
PS C:\Users\Sanket\Documents\node js\notes-app> node app.js list  
Fetching a list
PS C:\Users\Sanket\Documents\node js\notes-app> 
EN

回答 1

Stack Overflow用户

发布于 2020-04-19 05:02:20

您调用的是.argv,它在调用help之前先停止进程,然后才能定义其他进程。

从您创建的最后一个命令定义中删除.argv是解决此问题的最简单的方法。

代码语言:javascript
运行
复制
const yargs = require('yargs');


yargs.command({
    command:'add',
    describe:'Adds new note',
    handler:function(){
    console.log('Adding a note')
    }
});

yargs.command({
    command:'remove',
    describe:'Removes a note',
    handler:function(){
    console.log('Removing a note')
    }
 });

yargs.command({
    command:'list',
    describe:'Fetches a list of notes',
    handler:function(){
    console.log('Fetching a list')
    }
 }).argv;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61295304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档