-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (43 loc) · 1.28 KB
/
app.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
const fileName = "app"
const defaultValue = "Default"
const flags = ["-h", "-u"]
const checkFlag = (flag) => {
if(process.argv.indexOf(flag) > -1) return true;
}
const getValueFlags = (flag) => {
const customIndex = process.argv.indexOf(flag);
let customValue;
if (customIndex) {
customValue = process.argv[customIndex + 1];
}
const customFlag = (customValue || defaultValue);
return customFlag;
}
const convertUrl = (url) => {
fetch(`https://api.shrtco.de/v2/shorten?url=${url}`).then((response) => {
return response.json();
}).then(result => console.log(result))
.catch((err) => {
console.log(`${fileName}: ` + err)
});
}
const help = () => {
const getFlags = flags.map(i => `[${i}]`).join(" ");
console.log(`usage: ${fileName} -u "https://example.com"
Options
-h help command
-u <url> set target url`
);
}
const uFlagHandler = (flag) => {
const flagValue = getValueFlags(flag);
try{
if(flagValue == defaultValue) throw new Error("[-u] flag require arguments!");
convertUrl(flagValue);
} catch(err) {
console.log(`${fileName}: ${err}`)
}
}
if(checkFlag(flags[0])) return help();
if(checkFlag(flags[1])) return uFlagHandler(flags[1]);
help();