main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100755
|
File size:
1.1 KB
|
Line | |
---|
1 | #!/usr/bin/env node
|
---|
2 |
|
---|
3 | let { nanoid, customAlphabet } = require('..')
|
---|
4 |
|
---|
5 | function print(msg) {
|
---|
6 | process.stdout.write(msg + '\n')
|
---|
7 | }
|
---|
8 |
|
---|
9 | function error(msg) {
|
---|
10 | process.stderr.write(msg + '\n')
|
---|
11 | process.exit(1)
|
---|
12 | }
|
---|
13 |
|
---|
14 | if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
---|
15 | print(`
|
---|
16 | Usage
|
---|
17 | $ nanoid [options]
|
---|
18 |
|
---|
19 | Options
|
---|
20 | -s, --size Generated ID size
|
---|
21 | -a, --alphabet Alphabet to use
|
---|
22 | -h, --help Show this help
|
---|
23 |
|
---|
24 | Examples
|
---|
25 | $ nanoid --s 15
|
---|
26 | S9sBF77U6sDB8Yg
|
---|
27 |
|
---|
28 | $ nanoid --size 10 --alphabet abc
|
---|
29 | bcabababca`)
|
---|
30 | process.exit()
|
---|
31 | }
|
---|
32 |
|
---|
33 | let alphabet, size
|
---|
34 | for (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 += 1
|
---|
39 | 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 += 1
|
---|
45 | } else {
|
---|
46 | error('Unknown argument ' + arg)
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (alphabet) {
|
---|
51 | let customNanoid = customAlphabet(alphabet, size)
|
---|
52 | print(customNanoid())
|
---|
53 | } else {
|
---|
54 | print(nanoid(size))
|
---|
55 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.