-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepThought
More file actions
executable file
·91 lines (74 loc) · 2.55 KB
/
deepThought
File metadata and controls
executable file
·91 lines (74 loc) · 2.55 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
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
84
85
86
87
88
89
90
91
#!/usr/bin/env node
const {parser} = require('shargs/core')
const {subcommand, flag, number, command, string, stringPos} = require('shargs/opts')
const {cast, flagsAsBools, requireOpts, restrictToOnly, reverseFlags, setDefaultValues, splitShortOpts, verifyOpts} = require('shargs/parser')
const {desc, optsDef, optsLists, space, synopses, synopsis, usage} = require('shargs/usage')
const description = (
'Deep Thought was created to come up with the Answer to ' +
'The Ultimate Question of Life, the Universe, and Everything.'
)
const askOpts = [
string('format', ['--format'], {only: ['json', 'xml'], defaultValues: ['json'], desc: 'Respond either with json or xml.'}),
flag('html', ['--no-html'], {reverse: true, desc: 'Removes HTML tags.'}),
flag('help', ['-h', '--help'], {desc: 'Print this help message and exit.'}),
stringPos('question', {required: true, desc: 'State your question.'})
]
const opts = [
subcommand(askOpts)('ask', ['ask'], {required: true, desc: 'Ask a question.'}),
number('answer', ['-a', '--answer'], {defaultValues: [42], desc: 'The answer.'}),
flag('help', ['-h', '--help'], {desc: 'Print this help message and exit.'})
]
const ask = command('deepThought ask', askOpts, {desc: description})
const deepThought = command('deepThought', opts, {desc: description})
const rules = opts => (
!opts.some(_ => _.key === 'html' && typeof _.values !== 'undefined') ||
opts.some(_ => _.key === 'format' && (typeof _.values === 'undefined' || _.values[0] === 'json'))
)
const stages = {
argv: [splitShortOpts],
opts: [setDefaultValues, requireOpts, reverseFlags, restrictToOnly, cast],
args: [flagsAsBools]
}
const substages = {
ask: [verifyOpts(rules), ...stages.opts]
}
const docs = usage([
synopses,
space,
optsLists,
space,
desc
])
const askDocs = usage([
synopsis,
space,
optsDef,
space,
desc
])
const style = {
line: [{width: 80}],
desc: [{padStart: 4, width: 76}],
cols: [{width: 25}, {width: 55}]
}
const argv = process.argv.slice(2)
const deepThoughtParser = parser(stages, substages)(deepThought)
deepThoughtParser(argv).then(({args, errs}) => {
if (args.help) {
const help = docs(deepThought)(style)
console.log(help)
process.exit(0)
}
if (args.ask && args.ask.help) {
const askHelp = askDocs(ask)(style)
console.log(askHelp)
process.exit(0)
}
if (errs.length > 0) {
errs.forEach(({code, msg}) => console.log(`${code}: ${msg}`))
process.exit(1)
}
console.log(`The answer is: ${args.answer}`)
console.log(JSON.stringify(args, null, 2))
process.exit(0)
})