05.基于yargs做参数解析

175 阅读3分钟

由于参数option、params的各种不确定性, 导致我们在解析命令行参数的时候还是有一定的工作量的, 值得庆幸的是, 有一个别人造好的轮子来帮我们做这个事情, 这个轮子就是yargs, 这个包在npm上的周下载量居然达到了惊人的7千万+, 可见这个包是非常popular的, 稳定性也可以保障, 那么今天我们就来学习一下yargs的用法

image.png

安装依赖

pnpm add yargs

hideBin获取命令行参数(要调用.argv, 要不然不执行)

#! /usr/bin/env node

const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");

const arg = hideBin(process.argv);

yargs(arg).argv;

strict

args默认支持2条命令 -- help -- version yargs(arg).strict().argv; 我们在yargs后面调用string方法后, 输入--help或者任意不存在的命令都会提示这两条命令, 如下

➜  bin git:(master) ✗ arcjs --help
选项:
  --help     显示帮助信息                                                 [布尔]
  --version  显示版本号                                                   [布尔]

usage提示用法

yargs(arg).usage("Usage: arcjs [command] <options>").strict().argv;

➜  bin git:(master) ✗ arcjs --help
Usage: arcjs [command] <options>

选项:
  --help     显示帮助信息                                                 [布尔]
  --version  显示版本号                                                   [布尔]

demandCommand 期望命令(第一个参数为期望条数, 第二个参数为错误提示)

yargs(arg)
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .strict().argv;

alias 为命令配置别名

yargs(arg)
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .alias("H", "help")
  .alias("V", "version")
  .strict().argv;

命令行输出

➜  bin git:(master) ✗ arcjs -v
Usage: arcjs [command] <options>

选项:
  -H, --help     显示帮助信息                                             [布尔]
  -V, --version  显示版本号                                               [布尔]

A command is required. Pass --help to see all available commands and options
➜  bin git:(master) ✗ arcjs --V
0.0.0

使用wrap方法调整命令行宽度

yargs(arg)
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .alias("H", "help")
  .alias("V", "version")
  .wrap(100)
  .strict().argv;

使用和不使用的区别

image.png

使用terminalWidth让命令行占满全屏

const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");

const arg = hideBin(process.argv);
const cli = yargs(arg);

cli
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .alias("H", "help")
  .alias("V", "version")
  .wrap(cli.terminalWidth())
  .strict().argv;

image.png

footer描述 epilogue

cli
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .alias("H", "help")
  .alias("V", "version")
  .wrap(cli.terminalWidth())
  .epilogue("You owner footer description")
  .strict().argv;

输出结果

image.png

使用option和options定义命令, 使用group对命令进行分组

没啥好说的, 直接看输出结果吧

#! /usr/bin/env node

// const utils = require("arcjs-utils");
const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");

const arg = hideBin(process.argv);
const cli = yargs(arg);

cli
  .usage("Usage: arcjs [command] <options>")
  .demandCommand(
    1,
    "A command is required. Pass --help to see all available commands and options"
  )
  .alias("H", "help")
  .alias("V", "version")
  .wrap(cli.terminalWidth())
  .epilogue("You owner footer description")
  .option("registry", {
    type: "string",
    description: "Define global registry",
    alias: "R",
  })
  .options({
    debug: {
      type: "boolean",
      description: "Bootstrap debug mode",
      alias: "D",
    },
  })
  .group(["debug"], "Develop options")
  .strict().argv;

image.png

核心command方法(推荐第二种)

command的两种用法 用法1: 接收四个参数 command(命令名, 命令描述, builder, handler)

command(
    "init [name]",
    "Init a project",
    (yargs) => {
      yargs.option("name", {
        type: "string",
        description: "Name of a project",
        alias: "I",
      });
    },
    (argv) => {
      console.log("argv ===> ", argv);
    }
  )

用法2: 接收一个对象 command(命令名, 命令描述, builder, handler)

command({
    command: "list"
    alias: ["ls", "ll", "la"],
    describe: "list local package"
    builder:  (yargs) => {}, 
    handler: (argv) => {}
})

recommendCommands会根据输入查找最接近的命令

使用非常简单

image.png