-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·50 lines (43 loc) · 1.45 KB
/
cli.js
File metadata and controls
executable file
·50 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
const shortener = require('./');
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h') || args.length === 0) {
console.log('Usage: theshortener [options] <string>');
console.log('\nGenerate a short code from any string using SHA-256 + Base62');
console.log('\nOptions:');
console.log(' -l, --length <n> Output length (1-22, default: 8)');
console.log(' -v, --version Show version');
console.log(' -h, --help Show help');
console.log('\nExamples:');
console.log(' theshortener https://google.com');
console.log(' theshortener --length 12 https://google.com');
process.exit(args.length === 0 ? 1 : 0);
}
if (args.includes('--version') || args.includes('-v')) {
console.log(require('./package.json').version);
process.exit(0);
}
let length;
let input;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--length' || args[i] === '-l') {
length = parseInt(args[++i], 10);
if (isNaN(length)) {
console.error('Error: --length requires a numeric value');
process.exit(1);
}
} else {
input = args[i];
}
}
if (!input) {
console.error('Error: no input string provided');
process.exit(1);
}
try {
const result = length != null ? shortener.gen(input, length) : shortener.gen(input);
console.log(result);
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
}