AK-21/Graphite-Industrial-Intelligence
0
1#!/usr/bin/env node2 3let { nanoid, customAlphabet } = require('..')4 5function print(msg) {6 process.stdout.write(msg + '\n')7}8 9function error(msg) {10 process.stderr.write(msg + '\n')11 process.exit(1)12}13 14if (process.argv.includes('--help') || process.argv.includes('-h')) {15 print(`16 Usage17 $ nanoid [options]18 19 Options20 -s, --size Generated ID size21 -a, --alphabet Alphabet to use22 -h, --help Show this help23 24 Examples25 $ nanoid --s 1526 S9sBF77U6sDB8Yg27 28 $ nanoid --size 10 --alphabet abc29 bcabababca`)30 process.exit()31}32 33let alphabet, size34for (let i = 2; i < process.argv.length; i++) {35 let arg = process.argv[i]36 if (arg === '--size' || arg === '-s') {37 size = Number(process.argv[i + 1])38 i += 139 if (Number.isNaN(size) || size <= 0) {40 error('Size must be positive integer')41 }42 } else if (arg === '--alphabet' || arg === '-a') {43 alphabet = process.argv[i + 1]44 i += 145 } else {46 error('Unknown argument ' + arg)47 }48}49 50if (alphabet) {51 let customNanoid = customAlphabet(alphabet, size)52 print(customNanoid())53} else {54 print(nanoid(size))55}56 