zoukankan      html  css  js  c++  java
  • 如何用nodejs 开发一个命令行交互工具

    参考地址1

    参考地址2

    一、npm package.json bin

    1、package.json

    {
      "name": "test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "bin": {
        "gen": "bin/gen.js"
      },
      "dependencies": {
        "commander": "^2.15.1"
      }
    }

    2、bin/gen.js

    #!/usr/bin/env node
    var argv = process.argv;
    var filePath = __dirname;
    var currentPath = process.cwd();
    
    console.log(argv)
    console.log(filePath)
    console.log(currentPath)

    二、Commnader + inquirer + minimist + download-git-repo + ejs(Nunjucks、handlebars ) +  execa (child_process)

    Commander 示例

    #!/usr/bin/env node
    var program = require('commander');
    
     program
       .version('0.0.1')
       .option('-C, --chdir <path>', 'change the working directory')
       .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
       .option('-T, --no-tests', 'ignore test hook')
       .option('-p, --peppers', 'Add peppers')
       .option('-P, --pineapple', 'Add pineapple')
       .option('-b, --bbq-sauce', 'Add bbq sauce')
       .option('--p, --fuck-you', 'fuckyou')
       .option('build --env <fuckyou>', 'fuckyou2')
       .parse(process.argv);
    
    if (program.peppers) console.log('  - peppers');
    if (program.pineapple) console.log('  - pineapple');
    if (program.bbqSauce) console.log('  - bbq');
    if (program.fuckYou) console.log('fuckyou');
    if (program.env && program.args.length != 0) {
        console.log(program.args);
    }
    
     program
       .command('init')
       .description('run remote setup commands')
       .action(function() {
         console.log('setup');
       });
    
     program
       .command('exec <cmd>')
       .description('run the given remote command')
       .action(function(cmd) {
         console.log('exec "%s"', cmd);
       });
    
     program
       .command('teardown <dir> [otherDirs...]')
       .description('run teardown commands')
       .action(function(dir, otherDirs) {
         console.log('dir "%s"', dir);
         if (otherDirs) {
           otherDirs.forEach(function (oDir) {
             console.log('dir "%s"', oDir);
           });
         }
       });
  • 相关阅读:
    2019-2020nowcoder牛客寒假基础2
    2019-2020nowcoder牛客寒假基础1
    CF1291
    Daily Codeforces
    2019ICPC 上海现场赛
    Codeforces Round #686 (Div. 3)
    Codeforces Round #685 (Div. 2)
    Educational Codeforces Round 98 (Rated for Div. 2)
    Codeforces Round #654 (Div. 2)
    Codeforces Round #683 (Div. 2, by Meet IT)
  • 原文地址:https://www.cnblogs.com/CyLee/p/9189849.html
Copyright © 2011-2022 走看看