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

const { argv } = require('yargs');
console.log(argv); >cui 123 ajanuw -p=./
{ _: [ 123, 'ajanuw' ],
help: false,
version: false,
p: './',
'$0': 'AppData\\Roaming\\npm\\node_modules\\sh\\bin\\create-user-info.js' }

脚本文件 /bin/main.js

#!/usr/bin/env node

console.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"
}
}

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

#!/usr/bin/env node

const {
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] :
32 const 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=c3
842773
05-11 13:41