1、初始化配置:

  1. {
  2. "name": "hahaya",
  3. "version": "1.0.1",
  4. "description": "",
  5. "main": "index.js",
  6. "bin": {
  7. "hahaya": "bin/index.js"
  8. },
  9. "scripts": {
  10. "test": "echo \"Error: no test specified\" && exit 1"
  11. },
  12. "author": "",
  13. "license": "ISC"
  14. }
  1. #!/usr/bin/env node
  2. const yargs = require('yargs')
  3. //帮助处理参数解析
  4. const { hideBin } = require('yargs/helpers')
  5. const arg = hideBin(process.argv)
  6. console.log(yargs(arg).argv)

先使用npm link,建立软链接
image.png

1、usage 配置 cli 的开始内容

.usage('Usage: hahaya [command] <options>')
image.png

2、.demandCommand

(1, ‘A command is required. Pass —help to see all available commands and options.’) 最少要有一个参数,否则提示
image.png

3、.alias(‘h’, ‘help’) 设置别名

image.png

4、.wrap(100) 设置宽度

image.png
cli.terminalWidth() 与终端的宽一致

5、epilogue 结尾配置 dedent 去掉缩进,位于句首

.epilogue(dedent<br /> When a command fails, all logs are written to lerna-debug.log in the current working directory.<br /> For more information, find our manual at https://github.com/lerna/lerna<br />)
image.png

6、argv 打印到控制台上

7、配置选项

  1. const opts = {
  2. loglevel: {
  3. defaultDescription: 'info',
  4. describe: 'What level of logs to report.',
  5. type: 'string'
  6. },
  7. ...
  8. }
  9. .options(opts)

image.png

8、将选项分组

  1. // 得到这个option的名称
  2. const globalKeys = Object.keys(opts).concat(["help", "version"]);
  3. // 对 options 进行分组
  4. .group(globalKeys, 'Global Options:')
  5. .options(opts)
  6. .options({
  7. debug: {
  8. type: 'boolean',
  9. describe: 'Bootstrap debug mode',
  10. alias: 'd'
  11. }
  12. })
  13. .group(['debug'], 'dev Options:')

image.png

9、添加了一个隐藏的 option,界面不会显示

  1. .option('ci', {
  2. hidden: true,
  3. type: 'boolean'
  4. }).argv

2、完整的bin/index.js代码

  1. #!/usr/bin/env node
  2. const yargs = require('yargs')
  3. const dedent = require('dedent')
  4. const os = require('os')
  5. //帮助处理参数解析
  6. const { hideBin } = require('yargs/helpers')
  7. const arg = hideBin(process.argv)
  8. const cli = yargs(arg)
  9. const opts = {
  10. loglevel: {
  11. defaultDescription: 'info',
  12. describe: 'What level of logs to report.',
  13. type: 'string'
  14. },
  15. concurrency: {
  16. defaultDescription: os.cpus().length,
  17. describe: 'How many processes to use when lerna parallelizes tasks.',
  18. type: 'number',
  19. requiresArg: true,
  20. hidden: true //是否显示在终端
  21. },
  22. 'reject-cycles': {
  23. describe: 'Fail if a cycle is detected among dependencies.',
  24. type: 'boolean'
  25. },
  26. 'no-progress': {
  27. describe: 'Disable progress bars. (Always off in CI)',
  28. type: 'boolean'
  29. },
  30. progress: {
  31. // proxy for --no-progress
  32. hidden: true,
  33. type: 'boolean'
  34. },
  35. 'no-sort': {
  36. describe: 'Do not sort packages topologically (dependencies before dependents).',
  37. type: 'boolean'
  38. },
  39. sort: {
  40. // proxy for --no-sort
  41. hidden: true,
  42. type: 'boolean'
  43. },
  44. 'max-buffer': {
  45. describe: 'Set max-buffer (in bytes) for subcommand execution',
  46. type: 'number',
  47. requiresArg: true
  48. }
  49. }
  50. const globalKeys = Object.keys(opts).concat(['help', 'version'])
  51. //配置宽度
  52. // 配置 cli 结尾的内容
  53. //配置结尾
  54. // 配置 cli 结尾的内容
  55. cli
  56. //cli 配置首行
  57. .usage('Usage: hahaya [command] <options>')
  58. // 配置输入的最小命令,为1,否则弹出提示
  59. .demandCommand(1, 'A command is required. Pass --help to see all available commands and options.')
  60. // 开启严格模式
  61. .strict()
  62. //命令别名
  63. //命令别名
  64. .alias('h', 'help')
  65. .alias('v', 'version')
  66. // 配置cli的宽度和命令行一样
  67. .wrap(cli.terminalWidth())
  68. //epilogue 配置结尾
  69. .epilogue(
  70. dedent`
  71. When a command fails, all logs are written to lerna-debug.log in the current working directory.
  72. For more information, find our manual at https://github.com/lerna/lerna
  73. `
  74. )
  75. // 对 options 进行分组
  76. .group(globalKeys, 'Global Options:')
  77. .options(opts)
  78. .options({
  79. debug: {
  80. type: 'boolean',
  81. describe: 'Bootstrap debug mode',
  82. alias: 'd'
  83. }
  84. })
  85. .group(['debug'], 'dev Options:')
  86. // 添加了一个隐藏的 option
  87. .option('ci', {
  88. hidden: true,
  89. type: 'boolean'
  90. }).argv