温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

前端技术之:命令模块及其执行方法

发布时间:2020-03-11 09:35:17 来源:网络 阅读:248 作者:popgis 栏目:web开发

一、创建一个命令模块
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、将命令模块通过npm link进行全局注册后,即可在命令行窗口直接使用该命令
2、在其它模块中的package.json中引用命令模块,并增加scripts

"scripts": { "artisan": "artisan" }, "dependencies": { ...... "@uad/nat-cli": "^0.0.2", ...... }

增加对命令模块的依赖后,执行npm install后,会在node_modules/.bin目录下生成命令的快捷方式,在scripts中即可使用。
命令执行方法如下:

npm run artisan -- -h

npx artisan -h Usage: hello [options]

命令:

 artisan morning good morning

选项:
--version 显示版本号 [布尔]
-n, --name your name [字符串] [必需] [默认值: "tom"]
-h, --help 显示帮助信息 [布尔]

示例:

hello -n tom say hello to Tom Copyright 2019.
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI