-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·154 lines (129 loc) · 3.95 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
var chalk = require('chalk');
var VocabFetcher = require("vocab-fetcher")
var vocabFetcher = new VocabFetcher()
var ManipulateSubstring = require("manipulate-substring")
var SpellChecker = require("spellchecker")
var parseArgv = require("parse-argv")
var args = process.argv.slice(2);
var argv = parseArgv(args)
// Defaults
var showSentences = false;
var showFamily = false;
var showDefinitions = true;
var showLongDescription = false;
var showShortDescription = true;
if (args.indexOf("-h") > -1) {
console.log("Usage:\n definition <word> [options]")
console.log("Options:")
console.log(" -h # Show all options")
console.log(" -s # Show sentences")
console.log(" -f # Show related words")
console.log(" -sd # Show short description")
console.log(" -ld # Show long description")
console.log(" -ld # Show long description")
console.log("Example:\n definition abate -a")
process.exit()
}
if (!args[0]) {
throw new Error("Must provide a word as the first agument")
}
if (SpellChecker.isMisspelled(args[0])) {
var spellingSuggestions = SpellChecker.getCorrectionsForMisspelling(args[0]);
console.log("It looks like the word might be misspelled. Here are some suggestions:")
for(var i = 0; i < spellingSuggestions.length; i++){
console.log(i + 1 + ") " + chalk.underline.blue(spellingSuggestions[i]))
}
return
}
if (argv["s"]) {
showSentences = true;
}
if (argv["f"]) {
showFamily= true;
}
if (argv["d"]) {
switch(argv["d"]) {
case "short":
showShortDescription = true;
showLongDescription = false;
break
case "long":
showLongDescription = true;
showShortDescription = false;
break
case true:
showShortDescription = true;
showLongDescription = true;
default:
break;
}
}
if (argv["a"]) {
showFamily= true;
showSentences = true;
showDefinitions = true;
showLongDescription = true;
showShortDescription = true;
}
vocabFetcher.getWord(args[0]).then(function(word) {
console.log("");
console.log("WORD: " + chalk.green(args[0].toUpperCase()));
console.log("");
if (showShortDescription == true) {
printShortDescription(word);
}
if (showLongDescription == true) {
printLongDescription(word);
}
if (showDefinitions == true) {
printDefinitions(word);
}
if (showSentences == true) {
printSentences(word);
}
if (showFamily== true) {
printFamily(word);
}
})
function printShortDescription(wordObj) {
if (wordObj.shortDescription != "") {
console.log(chalk.underline.blue("Short Description"));
console.log(chalk.green(wordObj.name.capitalizeFirstLetter()) + ": " +wordObj.shortDescription);
console.log("");
}
}
function printLongDescription(wordObj) {
if (wordObj.longDescription != "") {
console.log(chalk.underline.blue("Long Description"));
console.log(chalk.green(wordObj.name.capitalizeFirstLetter()) + ": " + wordObj.longDescription);
console.log("");
}
}
function printDefinitions(wordObj) {
console.log(chalk.underline.blue("Definitions"));
for(var i = 0; i < wordObj.definitions.length; i++) {
var defObj = wordObj.definitions[i];
console.log(chalk.yellow(defObj.partOfSpeech) + ") " + defObj.definition);
}
console.log("");
}
function printSentences(wordObj) {
console.log(chalk.underline.blue("Sentences"));
for(var i = 0; i < wordObj.sentences.length; i++) {
var sentenceObj = wordObj.sentences[i]
console.log(chalk.yellow(i+1) +") " + ManipulateSubstring.colorizeBetweenCharacterIndexes("green", sentenceObj.offsets[0], sentenceObj.offsets[1], sentenceObj.sentence));
}
console.log("");
}
function printFamily(wordObj) {
console.log(chalk.underline.blue("Family"));
for(var i = 0; i < wordObj.family.length; i++) {
familyObj = wordObj.family[i];
console.log(chalk.yellow(i+1) +") " + familyObj.word);
}
console.log("");
}
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}