仍然是unknow; 视频中也是 直接输入 --debug命令,依然可以名称option:debug但是我这就是调不出来 , 依稀为完整代码
#! /usr/bin/env node
// commander 创建命令
// npm i commander --registry=https://registry.npmmirror.com
const commander = require('commander')
const pkg = require('../package.json')
// 用法2 创建新的实例
const program = new commander.Command()
// 使用command注册命令
// 第一种方式通过program.command
const clone = program.command('clone <source> [description]')
clone
.description('clone a repository') // 对命令的描述
.option('-f, --force', '是否强制克隆') // 对clone添加option
.action((source, description, cmdObj) => {
// 输入命令tleon clone aa -f desc -> do clone aa desc true
console.log('do clone', source, description, cmdObj.force)
})
// 高级定制2: 实现debug模式,此种当时轻松实现打印级别verbose,可以弃用iuleon/core/cli/lib.js中的checkInputArgs方法
program.on('option:debug', function () {
console.log('1111')
if (program.debug) {
console.log('2222')
process.env.LOG_LEVEL = 'verbose'
}
console.log(process.env.LOG_LEVEL)
})
// 高级定制3: 对未知命令的监听
program.on('command:*', function (obj) {
console.log(obj) // tleon aa bb -> [aa, bb]
console.error('未知的命令:' + obj[0])
// console.log(program.commands) // 获取所有的命令
const availableCommands = program.commands.map(cmd => cmd.name())
console.log(availableCommands)
console.log('可用命令:'+availableCommands.join(',')) // 可用命令:clone,service
})
program.parse(process.argv)
{
"name": "my-test-cli1",
"version": "1.0.3",
"description": "",
"main": "index.js",
"bin": {
"tleon": "bin/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"dev": "webpack -w"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^6.2.1",
"dedent": "^1.5.1",
"my-test-cli1-lib": "file:../my-test-cli1-lib",
"path-exists": "^4.0.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"babel-loader": "^8.2.2",
"webpack": "^5.10.3",
"webpack-cli": "^4.2.0"
}
}
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星