-
Notifications
You must be signed in to change notification settings - Fork 162
/
cli.js
executable file
·83 lines (76 loc) · 2.18 KB
/
cli.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
const yargs = require('yargs')
.usage(`
Usage: $0 [-e eye_string] [-f cowfile] [-h] [-l] [-n] [-T tongue_string] [-W column] [-bdgpstwy] text
If any command-line arguments are left over after all switches have been processed, they become the cow's message.
If the program is invoked as cowthink then the cow will think its message instead of saying it.
`)
.options({
e: {
default: 'oo',
},
T: {
default: ' ',
},
W: {
default: 40,
type: 'number',
},
f: {
default: 'default',
},
think: {
type: 'boolean',
},
})
.describe({
b: 'Mode: Borg',
d: 'Mode: Dead',
g: 'Mode: Greedy',
p: 'Mode: Paranoia',
s: 'Mode: Stoned',
t: 'Mode: Tired',
w: 'Mode: Wired',
y: 'Mode: Youthful',
e: "Select the appearance of the cow's eyes.",
T:
'The tongue is configurable similarly to the eyes through -T and tongue_string.',
h: 'Display this help message',
n: 'If it is specified, the given message will not be word-wrapped.',
W:
'Specifies roughly where the message should be wrapped. The default is equivalent to -W 40 i.e. wrap words at or before the 40th column.',
f:
"Specifies a cow picture file (''cowfile'') to use. It can be either a path to a cow file or the name of one of cows included in the package.",
r: 'Select a random cow',
l: 'List all cowfiles included in this package.',
think: 'Think the message instead of saying it aloud.',
})
.boolean(['b', 'd', 'g', 'p', 's', 't', 'w', 'y', 'n', 'h', 'r', 'l'])
.help()
.alias('h', 'help');
const argv = yargs.argv;
if (argv.l) {
listCows();
} else if (argv._.length) {
say();
} else {
require('get-stdin')().then((data) => {
if (data) {
argv._ = [require('strip-final-newline')(data)];
say();
} else {
yargs.showHelp();
}
});
}
function say() {
const module = require('./index');
const think = /think$/.test(argv['$0']) || argv.think;
console.log(think ? module.think(argv) : module.say(argv));
}
function listCows() {
require('./index').list((err, list) => {
if (err) throw new Error(err);
console.log(list.join(' '));
});
}