当前位置: 首页 > news >正文

网站建设的好处先域名 还是先做网站

网站建设的好处,先域名 还是先做网站,分析建设网站的可行性分析,wordpress图设置脚手架框架之yargs的基础核心特性与应用 1 )概述 yargs 是脚手架当中使用量非常大的一个框架进入它的npm官网: https://www.npmjs.com/package/yargs 目前版本: 17.7.2Weekly Downloads: 71,574,188 (动态数据)最近更新:last month (github)说明这是一个…

脚手架框架之yargs的基础核心特性与应用


1 )概述

  • yargs 是脚手架当中使用量非常大的一个框架
  • 进入它的npm官网: https://www.npmjs.com/package/yargs
    • 目前版本: 17.7.2
    • Weekly 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 --version
    1.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的使用

  • 继续测试 usage
    yargs(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.0
    

2.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(dedent`Welcome 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/xyzcli
    

2.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(dedent`Welcome 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(dedent`Welcome 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(dedent`Welcome 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
http://www.yayakq.cn/news/604422/

相关文章:

  • 郑州网站建设知乎微信 绑定网站
  • 做oa系统的网站楚雄做网站建设的公司
  • 做外贸怎么网站找客户信息中铁建设集团有限公司什么级别
  • 小型的企业网站服务 信誉好的网站制作
  • j动态加载网站开发包河网站建设
  • 建设一个企业网站到底要多少钱什么网站的页面做的比较好看
  • 搭建网站需要多少钱网站开发需要的知识
  • 重庆智能网站建设多少钱wordpress添加磁力下载
  • 网站备案号如何获得做招聘网站还有法盈利吗
  • 网站建设实践课程报告营销网站的宣传、推广与运作
  • 网站建设上机考试题目静态网站建设课程设计
  • 韩雪冬个人网站个人博客怎么做
  • 网站热点关键词广州新塘网站建设推广公司
  • 深圳网站建设方维网站建设免费的服务器
  • 黑龙江省住房与城乡建设厅网站无锡模板网站建设找哪个好
  • 常见网站漏洞共享备案网站
  • 新手建站工具建设部网站在哪里看受理
  • 单页面网站源码接私活 做网站
  • 网站建设好怎么才有生意旅行社的网站建设
  • asp资源下载网站网站制作的步骤不包括
  • 西安网站建设制作需要哪些费用为什么网站显示正在建设
  • 毕业设计做网站要求百度识图软件
  • 北京科技网站建设公司图书大厦网站建设报告
  • 笔记本做网站外网访问优化型网站建设的基本要求
  • 苏宁易购电子商务网站建设目标汕头网站制作找哪家
  • 淘宝网站建设类目需要什么资质做哪个网站招出国打工的多
  • 做网站的软件帝国常用网站建设工具
  • 南阳做网站优化公司网站运营核心
  • 怎么学网站建设做网站好还是阿里巴巴
  • 广西代理网站建设公司试析媒体网站品牌建设