Yargs introduction

Yargs be a node.js library fer hearties tryin’ ter parse optstrings

Yargs framework uses Node.js to build a fully functional command line application, which can easily configure commands, resolve multiple parameters, set shortcuts, etc., and automatically generate help menus. Lerna source code uses YARgs.

Command to introduce

When we want to create a VUE project through VUE-CLI. We’re going to run the Vue Create app. So how did vue become a global command? Type which vue on the console. We will get a path, go to the path, execute ls -la to see that the vue command is a soft connection. Points to the local VUE installation path.

$ which vue $ cd /usr/local/bin $ ls -la lrwxr-xr-x 1 admin admin 62 9 2 19:44 vue -> .. /.. /.. /Users/admin/.config/yarn/global/node_modules/.bin/vueCopy the code

After we enter the command on the console, the system will find the execution file through the soft link.

$ cd .. /.. /.. /Users/admin/.config/yarn/global/node_modules/.bin/ cat vueCopy the code

Using the preceding commands, you can find the execution file of the vue command. You can see this line of code at the beginning of the file. #! /usr/bin/env node is used to specify that the script file is to be executed using node.

#! /usr/bin/env nodeCopy the code

commands

Programmers are all too familiar with commands. Our daily development. Often you need to use commands to do something in the middle. Such as Git, etc. There are a few different types of commands we type on a daily basis. So how do commands come together.

git add
git -v
git rebase -i
Copy the code

command [-options] [parameter]

  • Command: indicates the command name, for example, ls and PWD
  • [-options] : options. There can be zero, one, or multiple options. Multiple options can be combined, such as -r. 【 eG: LS-L-a 】
  • [parameter] : indicates a parameter. It can have zero, one, or multiple parameters, such as touch file name, mkdir directory name, and CD target directory (path). These parameters are all parameters. 【touch 1.txt 2. TXT 】
  • [] : indicates optional

Node

Before introducing yargs, let’s take a look at nodeJs-related apis to help us better use and understand Yargs. Those familiar with ARGV can skip this section.

Argv is introduced

Before using yargs, let’s take a look at Node process.argv. Node has a global object, Process, through which argv can be retrieved.

  • The Process object provides information about and controls the current Node.js process.
  • Process. argv returns an array containing the command-line arguments passed in when the Node.js process is started.

Argv is intuitively known by a small chestnut. First we create a new index.js file locally. The following

console.log(process.argv)
Copy the code

The file is then executed through nodeJS. On the console, type node index.js test

We’re going to get an array in the console

['/Users/admin /. NVM/versions/node/v12.22.3 / bin/node ', '/ Users/admin/Documents/test/index, js',' test ']Copy the code

Argv [1] is the absolute path to the current file process.argv[2] is followed by the parameters we enter in the command

Yargs use

basis

  • Install dependencies
$ npm i yargs
Copy the code
  • The introduction of
const yargs = requier('yargs/yargs')

// or

import yargs from 'yargs' // From V16, ESM import is supported
Copy the code

Initializes a project locally

$ mkdir demo
$ cd demo
$ npm init -y
Copy the code

Add bin to package


{
  "name": "demo",..."bin": {"cli-test":"bin/index.js"}}Copy the code

Create an index.js file and add #! To the beginning of the js file. /usr/bin/env node


#!/usr/bin/env node


const yargs = require('yargs/yargs')
const {hideBin} = require('yargs/helpers')
const arg = hideBin(process.argv)
The hideBin function is equivalent to process.argv.slice(2). But it is compatible with changes in the environment. Such as Electron

const cli = yargs(arg)
console.log(cli)




Copy the code

Run NPM link in the root directory to add a soft connection to cli-test. You can directly enter CLI-test on the console


$ cli-test

cli Jt {
  customScriptName: false.parsed: false.'$0': 'cli-text'.argv: [cli-test]
}

$ cli-text  --version
$ cli-text  --help

Copy the code

Yargs comes with version and help.

Yargs function

usage

cli.usage('Usage: test [command] <options>') $ cli-test --help Usage: Test [command] <options> Options: --help display help information [Boolean] --version Display version number [Boolean]Copy the code

option

cli.option('optionTest', {type:"boolean".describe:'this is a option test'.alias:'ot'}).argv

$ cli-test -h
Copy the code

Run the cli-test -h command to see the console print out the option option

--optionTest, -- OT This is an option test [Boolean]Copy the code

Command Adds a command


cli.command('test'.'this is a test command'.(yargs) = >{},(argv) = >{}).argv

$ cli-test test
Copy the code

Alias Sets an alias.


cli.alias('v'.'version')
cli.alias('h'.'help')

$ cli-test -v
$ cli-test -h
Copy the code

Wrap Set width

cli.wrap(100).argv
Copy the code

The grounp Option group is displayed

cli.option('debugTest',{type:"boolean",describe:'this is a option test',alias:'ot'})
.group(['debugTest'],'dev option')
.argv
Copy the code
$ cli-test -h Usage: Test [command] <options> Cli -test test this is a test command dev option --debugTest, --ot this is an option test [Boolean] --optionTest, --ot this is an option test [Boolean] -v, --version Display version number [Boolean] -h, --help display help information [Boolean]Copy the code

Epilogue The script displayed at the bottom of the control panel

cli.. epilogue('bottom text').argv $ cli-test -hCopy the code
$cli-test-h -v, --version Displays the version number [Boolean] -h, --help displays the help information [Boolean] bottom textCopy the code

Shameshamecommands error correction hint

cli
.recommendCommands()
.command('list','starg',(yargs)=>{
    return yargs
},()=>{})
.argv

Copy the code
$cli-test li cli-test [command] command: cli-test list starg Options: -v, --version Displays the version number [Boolean] -h, --help displays the help information [Boolean] indicates list?Copy the code