美文网首页
npm重要模块--commander

npm重要模块--commander

作者: janezhang | 来源:发表于2021-06-24 18:29 被阅读0次

    https://github.com/tj/commander.js/blob/master/Readme_zh-CN.md#%E5%A3%B0%E6%98%8E-program-%E5%8F%98%E9%87%8F

    #!/usr/bin/env node
    
    var program = require('commander');
     
    program
        .version(require('../package').version)
        .parse(process.argv);
    
    program
        .command('buildRouter')
        .description('refresh routers..')
        .action(require('../lib/buildRouter'))
    
        console.log('hello cl22ooo2i');
    
    
    program
      .option('-d, --debug', 'output extra debugging')
      .option('-s, --small', 'small pizza size')
      .option('-p, --pizza-type <type>', 'flavour of pizza');
    
    program.parse(process.argv);
    
    const options = program.opts();
    if (options.debug) console.log(options);
    console.log('pizza details:');
    if (options.small) console.log('- small pizza size');
    if (options.pizzaType) console.log(`- ${options.pizzaType}`);
    
    
    // 选填:
    
    program
      .option('-c, --cheese [type]', 'Add cheese with optional type');
    
    program.parse(process.argv);
    
    const options = program.opts();
    if (options.cheese === undefined) console.log('no cheese');
    else if (options.cheese === true) console.log('add cheese');
    else console.log(`add cheese type ${options.cheese}`);
    
    // 必填
    program
      .requiredOption('-c, --cheese <type>', 'pizza must have cheese');
    
    program.parse(process.argv);
    
    
    // 命令
    program
      .command('clone <source> [destination]')
      .description('clone a repository into a newly created directory')
      .action((source, destination) => {
        console.log('clone command called');
      });
    
    
    
    program
      .command('start <service>', 'start named service')
      .command('stop [service]', 'stop named service, or all if no name supplied');
    
    
    
    // 设置命令参数
    program
      .version('0.1.0')
      .arguments('<username> [password]')
      .description('test command', {
        username: 'user to login',
        password: 'password for user, if required'
      })
      .action((username, password) => {
        console.log('username:', username);
        console.log('environment:', password || 'no password given');
      });
    
    
    
    // 可变参数
      program
      .version('0.1.0')
      .command('rmdir <dirs...>')
      .action(function (dirs) {
        dirs.forEach((dir) => {
          console.log('rmdir %s', dir);
        });
      });
    // 这个非常重要,不能丢
    program.parse(process.argv)
    

    相关文章

      网友评论

          本文标题:npm重要模块--commander

          本文链接:https://www.haomeiwen.com/subject/wkcdsltx.html