process.argv
Process is a module in Node. By accessing process.argv, we can easily receive arguments passed in when executing node programs by command.
[info] argv command line argument array
What does that mean? We’ve all used the NPM command to download packages from the command line window, and we can also pass parameters such as the name of the package to download, whether it is a development dependency or a production dependency.
These arguments + the absolute path of node.exe + the array of the absolute path of the file executed by Node = process.argv. It contains not only the passed parameters but also two other things as members of the array, so it is called an argument array.
Example 1
// console.log(process.argv) in p1.js; >>> Enter node p1.js - on the command line-a-b c <<< output E:\w>node p1.js --a -b c
[ 'C:\\Program Files\\nodejs\\node.exe'.'E:\\w\\p1.js'.'--a'.'-b'.'c' ]
Copy the code
Batch file
Batch files, as the name suggests, can execute multiple Node commands by executing only one file. It greatly simplifies typing on the command line. You only need to type the name of the batch file, and you don’t even need to type the prefix node. Batch files are different under different operating systems. They are divided into:
- Window batch file
- Class Linux batch file
Batch files in Windows
In Windows, the extension of a batch file is.bat.
//hello.bat file node p.js-aHello -b world >>> hello // xx. Bat <<<'C:\\Program Files\\nodejs\\node.exe'.'E:\\w\\p1.js'.'-a'.'hello'.'-b'.'world' ]
Copy the code
A placeholder
There is a problem in the above file. The parameter we pass is fixed. To make it not fixed, we need to replace the fixed parameter with a placeholder in the.bat file and then map it by passing the parameter when executing the batch file.
//hello.bat file node p.js %1 %2 %3 %4 >>> Enter Hello in the command line-aHello -b world // remove the suffix for xx.bat file <<< output ['C:\\Program Files\\nodejs\\node.exe'.'E:\\w\\p1.js'.'-a'.'hello'.'-b'.'world' ]
Copy the code
[warning] note:
- Now the number after % represents
%x
This placeholder represents the number of arguments passed in - %x starts at %1 instead of %0
The value of the passed parameter is no longer fixed, but the number is still fixed, so we recommend the following linux-like batch file.
Batch files in class LINx
At this point, we no longer need the.bat suffix, our batch file name is simply XXX (no suffix needed).
Add executable permissions to files
If the file we want to execute is called Hello, to execute the batch file, we need to give it executable permission first
$ chmod +x hello
Copy the code
Tell the command line who should execute the contents of the file
Next, add a sentence to the file that tells Node to execute the command
#! /usr/bin/env node
Copy the code
‘./’ execute the file
Finally, how do you execute this file by simply accessing it
>>> Enter $./hello --a-b c <<< output ['C:\\Program Files\\nodejs\\node.exe'.'E:\\w\\hello'.'--a'.'-b'.'c' ]
Copy the code
[danger] Note: at this time./ is required
npm link
If we want to type a command without typing the preceding ‘./’, we need to use the NPM link. First, add this paragraph under package.json
"bin": {"hello":"hello"// The former is the command we entered, the latter is the path of the file to execute (including the file name)}Copy the code
Next, enter it on the command line in the package.json directory
npm link
Copy the code
That way we don’t have to prefix the command with a./.
[important] note:
-
This is because the command line after the NPM link points to the NPM directory bat file (npm-link is automatically generated in the NPM directory, which is used to attach the executable file configured in package.json to the.bat file). The bat file in turn points to the Hello file (path) of the current package.json directory.
-
NPM link must work with #! /usr/bin/env node. Otherwise, Windows reports script execution errors
yargs
Yargs helps us wrap the parameters accepted in the file being executed. Argv is the wrapped object of process.argv. In addition, we can further customize the wrapped object through yargs.options.
[important] note: Argv differs from process.argv in that it returns an object instead of an array. Arguments starting with a conjunction line – or – are used as a key in the object, followed by non-conjunction line arguments as the key, and return true if there are no non-conjunction line arguments
/ / hello file#! /usr/bin/env node
let yargs = require('yargs');
letargv = yargs.argv; Argv is mandatory console.log(argv); >>> Enter $./hello - on the command line-a1-b 2 c <<< output {_: ['c'].help: false,
version: false,
a: 1,
b: 2,
'$0': 'hello',} console.log(argv.a); < < < 1 outputCopy the code
Underline attribute
We can get arguments at the beginning of a non-conjunction line by calling argv._
>>> Enter $./hello d - on the command line-a1 -b 2 c <<<'d'.'c' ]
Copy the code
Command line parameter configuration
Multiple parameters are separated by. Option
#! /usr/bin/env node
let yargs = require('yargs');
// let argv = yargs.argv;
let argv = yargs.options('a', {alias:'ant'
,demand:true
,default:'super'
,describe:'A very large ant'
,boolean:false
,type:'string'
}).options('b', {alias:'BB'
,describe:'woshi bb'
,boolean:false}).argv; console.log(argv._); console.log(argv); >>> Enter $./p1.js on the command line-aValueA -b valueB-c <<< Output [] {_: [],help: false,
version: false,
a: 'valueA',
ant: 'valueA',
b: 'valueB',
BB: 'valueB',
c: true.'$0': 'p1.js' }
Copy the code
Description of configuration items:
- Alias: an alias that also generates Ant when a is passed in
- Demand: Indicates whether this parameter is required
- Default: indicates the default value
- Describe: Parameter description
- Boolean: When set to true, the parameter is set to false if it is not passed in
- Type: Limits the type of the passed argument
Configuring Help Information
First of all, Yargs has provided us with –help by default to display help information. We can also configure a separate name for this –help parameter to simplify things
letArgv = yargs.'h'Argv >>> Enter $./p1.js --h <<< output options: --version Displays the version number [Boolean]-a--ant A very large ant [string] [required] [Default:"super"] -b, --BB woshi BB [Boolean] -h display help message [Boolean]Copy the code
Configure other help prompts
- Usage: Usage format
- Example: a detailed use of chestnut
- Epilog: Display at the end, often used to show the version line of a command tool
// Hello file... .help('h')
.usage('hello -[option] value')
.example('I, chestnut, make you understand! ')
.epilog('copyright 2018-') .argv; >>> $./hello -h <<< hello -[option] value Option: --version Display version number [Boolean]-a--ant A very large ant [string] [required] [Default:"super"] -b, --BB woshi BB [Boolean] -h Display help message [Boolean] Example: I, chestnut, let you understand! copyright 2018-Copy the code
Yargs implementation idea
let args = process.argv;
let argv = {};
for(leti=2; i<args.length; ++i){let cur = args[i];
if(/^(--)/.test(cur)){ argv[cur.slice(2)] = args[++i]; }}Copy the code