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

发布时间:2020-03-11 09:35:17 作者:popgis
来源:网络 阅读:210

一、创建一个命令模块
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.
推荐阅读:
  1. 前端技术之:如何创建一个NodeJs命令行交互项目
  2. 前端技术之:通过plop生成Controller的方法与步骤

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

command line 命令行 工具

上一篇:26 python语言编写判断奇数偶数 动态输出菱形 eva

下一篇:web前端入门到实战:css中display属性

相关阅读

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

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