分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

node.js ?设置脚本命令

发布时间:2023-09-06 01:49责任编辑:赖小花关键词:js

yargs模块 https://www.npmjs.com/package/yargs https://github.com/yargs/yargs/blob/HEAD/docs/api.md

脚本文件

// /bin/main.js#!/usr/bin/env nodeconsole.log( ?123);

package.json

{ ?"name": "sh", ?"version": "1.0.0", ?"description": "", ?"main": "main.js", ?"scripts": { ???"test": "echo \"Error: no test specified\" && exit 1" ?}, ?"author": "", ?"license": "ISC", ?"bin": { ???"r32": "./bin/main.js" ?}}

在脚本根目录执行 cnpm i -g ./
r32 执行命令就会出现 123

这是一个返回 随机数的命令

#!/usr/bin/env nodeconst { ?argv} = require('yargs') ?.config({ ???s: 'c0' ?}) ?.option('all', { ???describe: '查看返回字符串类型' ?}) ?.alias('v', 'version') ?.help('help')if (argv.version) { ?console.log('1.0.0'); ?return;}if (argv.all) { ?console.log(` ???--s=c0 ?默认返回大小写字母和数字 [c1 全大写] [c2 全小写] [c3 全小写] ?`); ?return;}let len = argv._[0] && typeof + argv._[0] === "number" ? ?+argv._[0] : ?32const c0 = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 大小写字母和数字const c1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 大写const c2 = 'abcdefghijklmnopqrstuvwxyz'; // 小写const c3 = '123456789'; // 小写let c = argv.s; // 默认大小写数字switch (c) { ?case 'c1': ???c = c1; ???break; ?case 'c2': ???c = c2; ???break; ?case 'c3': ???c = c3; ???break; ?default: ???c = c0; ???break;}const random = (a, b) => Math.floor(Math.random() * (b - a + 1) + a);function randomString32(len, c) { ?// 返回随机字符串 ?const c_len = c.length; ?return new Array(len) ???.fill() ???.reduce((acc) => acc += c[random(0, c_len - 1)], '')}const str = randomString32(len, c);const exec = require('child_process').exec ;exec(`echo ${str} | clip`, (error, stdout, stderr)=>{ ?if(error) console.log('复制到粘贴板失败!');})console.log(str);
>node .\bin\main.js 6 --s=c3842773

node.js ?设置脚本命令

原文地址:https://www.cnblogs.com/ajanuw/p/8818873.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved