【问题标题】:Yargs help not displaying all the help optionsYargs 帮助不显示所有帮助选项
【发布时间】:2018-10-04 08:54:32
【问题描述】:

我正在使用 yargs 为传递给脚本的参数添加命令行选项。当我发出帮助命令以及脚本名称时,它不会显示 add 参数的帮助。

const yargs=require('yargs');
 const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
    title : {
        describe : 'title of the file',
        demand: 'true'
    }
})
.help()
.argv;




root# node mainFile_node.js --help
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number

     node mainFile_node.js add
YARGS ARGV:-{ _: [ 'add' ], '$0': 'mainFile_node.js' }

【问题讨论】:

  • 任何帮助将不胜感激。

标签: javascript node.js yargs


【解决方案1】:

添加 yargs.parse();到文件末尾,让 yargs 使用其预定义的 args,例如 --help、--version。

【讨论】:

    【解决方案2】:

    试试下面的代码

    const yargs = require("yargs")
    yargs.command({
      command: 'add',
      describe: 'ADD A NOTE TO A FILE',
      handler: function(){
        console.log('Adding to a file')
      }
    })
    

    然后用下面的命令执行

    root# node mainFile_node.js --help
    

    【讨论】:

      【解决方案3】:
      const yargs=require('yargs');
      const argv= yargs.command('add', 'ADD A NOTE TO A FILE', {
          title : {
              describe : 'title of the file',
              demand: 'true'
          }
      }).parse()   
      

      你必须在最后添加 parse() 方法。如果你将来碰巧使用 parse() 也会执行处理函数。

      或下代码

      console.log(yargs.argv) 
      

      如果您没有在文件上编写任何代码但您正在使用命令行,则此方法很有用。比方说

      node app.js 添加--title="somthing" --help

      要查看帮助菜单,您必须在文件中输入console.log(yargs.argv)

      【讨论】:

        【解决方案4】:
        const yargs = require("yargs");
        
        yargs.command({
          command: "add",
          describe: "Add new note",
          builder: {
            title: {
              // describe: 'note title',
              demandOption: true,
              type: "string"
            }
          },
          handler: function(argv) {
            console.log("My title: " + argv.title);
          }
        });
        
        yargs.parse();
        

        然后在命令行中运行:

        node filename.js add --title="title name"
        

        【讨论】:

          【解决方案5】:

          首先检查 node ./main.js --h 是否工作?

          然后,只需在单独的 js 文件上运行 node ./app.js --help ,它就会开始处理前一个文件。

          const chalk = require('chalk');
          const yargs = require('yargs');
          
          //Create add command
          yargs.command({
              command:'add',
              describe:'Add a new note',
              handler:function(){
                  console.log("add a new note");
              }
          })
          
          //Create remove command
          yargs.command({
              command:'remove',
              describe:'Remove the note',
              handler:function(){
                  console.log("Removing a note");
              }
          })
          
          //Create list command
          yargs.command({
              command:'list',
              describe:'List all the notes',
              handler:function(){
                  console.log("List notes");
              }
          })
          
          //Create read command
          yargs.command({
              command:'read',
              describe:'read note',
              handler:function(){
                  console.log("read a note");
              }
          })
          
          
          console.log(yargs.argv)
          

          【讨论】:

            【解决方案6】:
            yargs.command({
            command: 'add',
            
            showInHelp: true, //add this line here!!!
            
            describe: 'add a note',
            builder: {
                title: {
                    describe: 'title of my note',
                    demandOption: true,
                    type: 'string',
                    
                    
                },
                body: {
                    describe: 'body note',
                    demandOption: true,
                    type: 'string',
                    
            
                    
                }
            },
            
            handler: function (argv) {
                console.log(argv.title,'\n', argv.body, '\n creating the note', argv)
            }
            })
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-09-04
              • 1970-01-01
              • 1970-01-01
              • 2014-07-09
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多