做灯具网站推广哪家好,网站架设的结构,wordpress与论坛,wordpress弹窗注册代码脚手架框架之yargs的基础核心特性与应用 1 #xff09;概述
yargs 是脚手架当中使用量非常大的一个框架进入它的npm官网: https://www.npmjs.com/package/yargs 目前版本: 17.7.2Weekly Downloads: 71,574,188 (动态数据)最近更新#xff1a;last month (github)说明这是一个…脚手架框架之yargs的基础核心特性与应用 1 概述
yargs 是脚手架当中使用量非常大的一个框架进入它的npm官网: https://www.npmjs.com/package/yargs 目前版本: 17.7.2Weekly Downloads: 71,574,188 (动态数据)最近更新last month (github)说明这是一个比较优质的库
2 对 yargs 的应用
准备一个脚手架工程比如 xyzcli进行 npm 初始化 $ npm init -y修改package.json中的关键配置 添加 bin 属性其值设置为: “bin/index.js” 在脚手架工程目录中执行安装开发依赖 $ npm i yargs -S
2.1 初步实践了解其基本API
编写 bin/index.js#!/usr/bin/env nodeconst yargs require(yargs/yargs);
const { hideBin } require(yargs/helpers);
console.log(hideBin(process.argv));
yargs();现在执行 $ npm link 方便调试执行 $ xyzcli --help, 可以看到输出 [ --help ]
2.2 继续优化窥探其基本用法
好接着修改 bin/index.js#!/usr/bin/env nodeconst yargs require(yargs/yargs);
const { hideBin } require(yargs/helpers); // 用于参数解析
const arg hideBin(process.argv);yargs(arg).argv;现在执行 $ xyzcli --help 可以看到输出选项--help 显示帮助信息 [布尔]--version 显示版本号 [布尔]执行 $ xyzcli --version1.0.0这时候说明最简易版脚手架已经开发好了
2.3 探究 strict 模式
现在发现差异如果执行 $ xyzcli --help 有输出但是执行 $ xyzcli -h 就没有输出了可以加入 strict 模式可以对无用参数输入进行提示如下添加yargs(arg).strict().argv;继续执行 $ xyzcli -h 看到输出了报错提示选项--help 显示帮助信息 [布尔]--version 显示版本号 [布尔]无法识别的选项h注意这个 strict 在 lerna 源码中有用到
2.4 对 usage 这个API的使用
继续测试 usageyargs(arg).usage(Usage: xyzcli [command] options).strict().argv;执行 $ xyzcli --help查看输出Usage: xyzcli [command] options选项--help 显示帮助信息 [布尔]--version 显示版本号 [布尔]看到最顶部多了 Usage: xyzcli [command] options
2.5 对 demandCommand 这个API的使用 这个API表示最少输入多少命令 yargs(arg).usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().argv;只执行 $ xyzcli, 可查看输出 Usage: xyzcli [command] options选项--help 显示帮助信息 [布尔]--version 显示版本号 [布尔]A command is required. Pass --help to see all available commands and options.可以发现最后多了一行 A command is required. Pass --help to see all available commands and options. 这个API就可以对命令参数过少时进行提示
2.6 对 alias 这个API的使用 对现有命令或参数起别名 yargs(arg).usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version).argv;对 -h 进行测试执行 $ xyzcli -h输出 Usage: xyzcli [command] options选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]对 -v 进行测试执行 $ xyzcli -v输出 1.0.02.7 对 wrap 这个API的使用
对终端cli窗口的宽度的设定yargs(arg).usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version).wrap(50) // 这里设定为 50.argv;执行 $ xyzcli -h查看宽度Usage: xyzcli [command] options选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]修改宽度为 80.wrap(80) // 这里设定为 80再次执行 $ xyzcli -h查看宽度Usage: xyzcli [command] options选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]可以看到两者不同宽度的区别如果想要占满终端屏幕, 可以使用 terminalWidth()const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version).wrap(cli.terminalWidth()) // 这里.argv;terminalWidth() 会返回终端宽度这里没有必要做演示了会铺满终端
2.8 对 epilogue 这个API的使用 它可以在结尾加上我们想要说的话 const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version).wrap(cli.terminalWidth()).epilogue(We hope you guys work happily every day!) // 注意这里.argv;执行 $ xyzcli -h, 查看输出 Usage: xyzcli [command] options选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]We hope you guys work happily every day!可以看到最后一行输出 We hope you guys work happily every day! 结合 dedent 库来使用首先进行安装$ npm i dedent -S 基于 dedent 库来设定 epilogue, 达成去除缩进(包含首位空格)的目的 #!/usr/bin/env nodeconst yargs require(yargs/yargs);
const { hideBin } require(yargs/helpers);
const dedent require(dedent); // 注意这里const arg hideBin(process.argv);
const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version).wrap(cli.terminalWidth()).epilogue(dedentWelcome to use xyzcli command line.We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli).argv;执行 $ xyzcli -h查看输出 Usage: xyzcli [command] options选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]Welcome to use xyzcli command line.
We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli2.9 对 options 这个API的使用
全局选项对所有 command 都有效const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version)// .alias(d, debug) // 这个设置别名同下面二者取其一即可.wrap(cli.terminalWidth()).epilogue(dedentWelcome to use xyzcli command line.We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli).options({debug: {type: boolean,describe: Bootstrap debug mode,alias: d // 这里添加了上面就可以忽略了推荐写在这里}}).argv;这里设置了 options内部传入一个对象进行配置这里别名的两种设置方式推荐下面这种在options内部设定执行 $ xyzcli -h, 查看输出结果Usage: xyzcli [command] options选项-d, --debug Bootstrap debug mode [布尔]-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]Welcome to use xyzcli command line.
We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli可见这里添加了 -d 和 --debug 的选项
2.10 对 option 这个API的使用
这个 option 和 options 这两个API用途差不多, 但使用方式不一样const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version)// .alias(d, debug).wrap(cli.terminalWidth()).epilogue(dedentWelcome to use xyzcli command line.We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli).options({debug: {type: boolean,describe: Bootstrap debug mode,alias: d // 这里添加了上面就可以忽略了推荐写在这里}}).option(registry, {type: string,describe: Define global registry,alias: r,// hidden: true, // 这个就不会出现在提示中但是可以作为开发调试使用 --registry 或 -r}).argv;option 这个API可以配置 hidden 选项这个配置之后不会出现在终端提示中但是可作为开发调试使用$ xyzcli -r 需要后期结合这个参数做对应的处理程序这块先略过同时需注意options 中是没有 hidden 配置选项的 执行 $ xyzcli -h查看输出结果Usage: xyzcli [command] options选项-d, --debug Bootstrap debug mode [布尔]-r, --registry Define global registry [字符串]-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]Welcome to use xyzcli command line.
We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcliA command is required. Pass --help to see all available commands and options.2.11 对 group 这个API的使用
对命令进行分组管理, 这次全量代码都在这里#!/usr/bin/env nodeconst yargs require(yargs/yargs);
const { hideBin } require(yargs/helpers);
const dedent require(dedent);const arg hideBin(process.argv);
const cli yargs(arg)cli.usage(Usage: xyzcli [command] options).demandCommand(1, A command is required. Pass --help to see all available commands and options.).strict().alias(h, help).alias(v, version)// .alias(d, debug).wrap(cli.terminalWidth()).epilogue(dedentWelcome to use xyzcli command line.We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli).options({debug: {type: boolean,describe: Bootstrap debug mode,alias: d // 这里添加了上面就可以忽略了推荐写在这里}}).option(registry, {type: string,describe: Define global registry,alias: r,// hidden: true, // 这个就不会出现在提示中但是可以作为开发调试使用 --ci}).group([debug], 开发选项).group([registry], 其他选项).argv;执行 $ xyzcli -h查看输出结果Usage: xyzcli [command] options开发选项-d, --debug Bootstrap debug mode [布尔]其他选项-r, --registry Define global registry [字符串]选项-h, --help 显示帮助信息 [布尔]-v, --version 显示版本号 [布尔]Welcome to use xyzcli command line.
We hope you guys work happily every day!For more information, please visit: https://xxx.com/xxx/xyzcli可以看到 Dev Options 这一分组把 debug 包含进去了剩余其他的都在 选项中
3 参考 lerna 的实现细节
https://github.com/lerna/lerna/blob/main/libs/core/src/lib/cli.ts