准备工作
- 安装nodejs
安装commander.js,执行
npm install commander --saveversion方法
参数说明:
版本号<必须>
-
使用:
(1)只传入版本信息
// index.jsconst program = require('commander');program.version('1.0.0').parse(process.argv);复制代码
执行
node index.js -V或者node index.js --version得到版本号。(2)自定义flag
// index.jsconst program = require('commander');program.version('1.0.0', '-v, --version').parse(process.argv);复制代码
当自定义flag时,
--version不能被省略执行
node index.js -v或者node index.js --version得到版本号。option方法
参数说明:
自定义flag<必须>
- 一长一短的flag,中间可以逗号、竖线或空格隔开
- flag后面可以跟参数,
<>定义必需参数,[]定义可选参数
- 选项的描述<可省略>:在使用
-h或者--help时会显示 -
使用
(1)定义多个选项
// index.jsconst program = require('commander');program.version('1.0.0').option('-a, --add', 'add Something').option('-u, --update', 'update Something').option('-r, --remove', 'remove Something').parse(process.argv);console.log('You choose: ');if (program.add) console.log(' add Something');if (program.update) console.log(' update Something');if (program.remove) console.log(' remove Something');复制代码
短flag使用
-,长flag使用--program.XXX可以得到输入的选项执行
node index.js -a或者node index.js --add会打印You choose: add Something(2)多单词形式
// index.jsconst program = require('commander');program.version('1.0.0').option('--add-file', 'add a file').parse(process.argv);if (program.addFile) console.log('add a file')复制代码
当选项为多单词形式时,使用驼峰形式得到输入项
执行
node index.js --add-file会打印add a file(3)以
--no形式开头的选项,代表后面紧跟单词的相反面// index.jsconst program = require('commander');program.version('1.0.0').option('--no-add', 'not add a file').parse(process.argv);if (program.add) console.log('add a file')else console.log('not add a file')复制代码
执行
node index.js --no-add会打印not add a file(4)选项后面使用<>或[]
// index.jsconst program = require('commander');program.version('1.0.0').option('-a, --add <fileName>', 'add a file').parse(process.argv);console.log('add a file named: ' + program.add)复制代码
执行
node index.js -a demo.js会打印add a file named: demo.jscommand方法
参数说明:
自定义命令名称
- 名称<必须>
- 命令参数<可选>:
<>和[]定义参数- 命令的最后一个参数可以是可变的,需要在数组后面加入 … 标志;在命令后面传入的参数会
- 命令描述<可省略>
配置选项<可省略>:可配置noHelp、isDefault等
使用
const program = require('commander');program.version('1.0.0').command('my-cli <path>').option('-a, --add <fileName>', 'add a file').option('-u, --update <fileName>', 'update a file').option('-r, --remove <fileName>', 'remove a file').action(function(path, cmd) {console.log(path)console.log(cmd.add)})program.parse(process.argv);复制代码
使用
action后会开启输入的选项校验,若输入了未定义的选项,则抛出错误执行
node index.js my-cli C -a demo.js会打印C demo.jsdescription方法
参数说明
-
使用
// index.jsconst program = require('commander');program.version('1.0.0').description('It is my cli').parse(process.argv);
action方法
参数说明:
-
使用
const initAction = () => {inquirer.prompt([{type: "input",message: "Project name",name: 'name'},{type: "list",message: "Project type",name: 'type',choices: ['vue', 'react', 'dva']}, {type: "list",message: "choose your install type",name: "install",choices: ['npm', 'cnpm', 'yarn', 'by my self']}]).then(answers => {const targetPath = path.join(process.cwd(), answers.name);const sourcePath = path.join(__dirname, "..", `lib/${answers.type}-cli-master`)copy(targetPath, sourcePath)// execSync(`cd ${answers.name}`,(error,stdout,stderr)=>{// if(error)return console.error(error)// console.log(process.cwd())// })exec(`${answers.install} install`, { cwd: `${targetPath}` }, (error, stdout, stderr) => {if (error) return console.error(error)console.log(stdout)})})}program.version(require("../package.json").version)program.command("init").description("创建项目").action(initAction)program.parse(process.argv)
parse方法
作用:用于解析process.argv,设置options以及触发commands
参数说明:
- process.argv
