您好,登錄后才能下訂單哦!
一、創(chuàng)建一個(gè)命令模塊
1、package.json
{
"name": "@uad/nat-cli",
"version": "0.0.2",
"description": "Demo",
"main": "index.js",
"bin": {
"artisan": "./src/artisan.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git"
},
"keywords": [
"CLI"
],
"author": "chunrong.liu",
"license": "ISC",
"dependencies": {
"shelljs": "^0.8.3",
"yargs": "^13.2.4"
}
}
2、src/artisan.js
#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
.option('n', {
alias : 'name',
demand: true,
default: 'tom',
describe: 'your name',
type: 'string'
})
.usage('Usage: hello [options]')
.example('hello -n tom', 'say hello to Tom')
.help('h')
.alias('h', 'help')
.epilog('Copyright 2019')
.command("morning", "good morning", function (yargs) {
echo("Good Morning");
var argv = yargs.reset()
.option("m", {
alias: "message",
description: "provide any sentence"
})
.help("h")
.alias("h", "help")
.argv;
echo(argv.m);
})
.argv;
console.log('hello ', argv.n);
console.log(argv._);
二、使用方法
1、將命令模塊通過(guò)npm link進(jìn)行全局注冊(cè)后,即可在命令行窗口直接使用該命令
2、在其它模塊中的package.json中引用命令模塊,并增加scripts
"scripts": {
"artisan": "artisan"
},
"dependencies": {
......
"@uad/nat-cli": "^0.0.2",
......
}
增加對(duì)命令模塊的依賴(lài)后,執(zhí)行npm install后,會(huì)在node_modules/.bin目錄下生成命令的快捷方式,在scripts中即可使用。
命令執(zhí)行方法如下:
npm run artisan -- -h
或
npx artisan -h
Usage: hello [options]
命令:
artisan morning good morning
選項(xiàng):
--version 顯示版本號(hào) [布爾]
-n, --name your name [字符串] [必需] [默認(rèn)值: "tom"]
-h, --help 顯示幫助信息 [布爾]
示例:
hello -n tom say hello to Tom
Copyright 2019.
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。