/
repl.ts
46 lines (42 loc) · 1.07 KB
/
repl.ts
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
import { command, program } from '../src'
let url: string | null = null
const app = program()
.add(
command('connect')
.description('Connect to a server')
.argument('host', {
default: 'example.com',
prompt: true,
})
.argument('port', {
default: 443,
prompt: true,
})
.argument('tls', {
default: true,
prompt: true,
})
.option('protocol', {
default: 'http',
choices: ['http', 'ftp', 'imap', 'ldap', 'pop3'] as const,
})
.option('timeout', {
default: 60,
})
.action(async ({ host, port, tls, protocol, timeout }) => {
url = `${protocol}${tls && 's'}://${host}:${port}`
console.log(`Connecting to ${url} (timeout set to ${timeout}s)...`)
})
)
.add(
command('disconnect')
.description('Disconnect from a server')
.action(async () => {
if (!url) {
throw new Error('Not connected')
}
console.log(`Disconnecting from ${url}...`)
url = null
})
)
app.runOrRepl()