The front-end technology: how to create a command-line interactive project NodeJs

Method 1: NodeJs API native , as follows:
 
#!/usr/bin/env node
# test.js
var argv = process.argv;
console.log(argv)
 
Execute the following command:
node test.js param1 --param2 -param3
 
Output results as follows:
[ '/usr/local/Cellar/node/10.10.0/bin/node',
  'test.js',
  "Param1",
  '--Param2'
  '-Param3']
 
Visible, argv first parameter for the application path node, the second parameter is the js program file to be executed, to execute the remaining parameters.
 
Method Two: Get command line parameters yargs , as follows:
 
First, we need to introduce the module in the project:
 
npm install --save args
 
Then, create JS executable program, as follows:
#!/usr/bin/env node
 
 
var args = require('yargs');
 
 
 
const argv = args.option('n', {
 
alias : 'name',
 
demand: true,
 
default: 'tom',
 
describe: 'your name',
 
type: 'string'
 
})
.usage('Usage: hello [options]')
.example('hello -n bob', 'say hello to Bob')
 
.help('h')
.alias('h', 'help')
 
.argv;
 
 
console.log('the args:', argv)
 
 
Execute the following command:
node test.js -h
 
The results show the following:
Usage: hello [options]
 
 
Options:
  --version Show the version number [Istanbul]
  -n, --name your name [String] [required] [Default: "tom"]
  -h, --help display help information [Istanbul]
 
 
Example:
  hello -n bob  say hello to Bob
 
Execute the following command:
node test.js -n Bobbbb 'we are friends'
 
The results are shown below:
the args: { _: [ 'we are friends' ],
  n 'Bobbbb'
  name: 'Bobbbb',
  '$0': 'test.js' }
 
Visible through NPM yargs open bag, it is easy to define the command line format, and easy access to the various forms of command line arguments.
 
While it is easy to define and obtain command-line parameters yargs, but not a good solution to interact with the command line, and the data type of the parameter is also more subject to limitations. So, we look at another open source project.
 
Method three: interact command inquirer open source projects
 
Creating test.js file:
#!/usr/bin/env node
 
 
var inquirer = require("inquirer");
inquirer
  .prompt([
    {
      type: "input",
      name: "name",
      message: "controller name please",
      validate: function(value) {
        if (/.+/.test(value)) {
          return true;
        }
        return "name is required";
      }
    },
    {
      type: "list",
      name: "type",
      message: "which type of conroller do you want to create?",
      choices: [
        { name: "Normal Controller", value: "", checked: true },
        { name: "Restful Controller", value: "rest" },
        { name: "View Controller", value: "view" }
      ]
    }
  ])
  .then(answers => {
    console.log(answers);
  });
 
execute program:
node test.js
 
Output:
? controller name please test
? which type of conroller do you want to create? Normal Controller
{ name: 'test', type: '' }
 
 
References:

Guess you like

Origin www.cnblogs.com/popgis/p/11809819.html