-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartcli.nim
More file actions
50 lines (40 loc) · 1.3 KB
/
Copy pathsmartcli.nim
File metadata and controls
50 lines (40 loc) · 1.3 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
import std / [parseopt, strutils, syncio]
export parseopt
proc writeSpec(spec: string) =
stdout.write spec
if not spec.endsWith('\n'):
stdout.write '\n'
stdout.flushFile()
proc cliExitHelp*(spec: string) {.noreturn.} =
writeSpec(spec)
quit 0
proc cliExitVersion*(spec: string) {.noreturn.} =
var title = ""
var i = 0
while i < spec.len and spec[i] notin {'\n', '\r'}:
title.add spec[i]
inc i
title = title.strip()
if title.len == 0:
title = spec.strip()
stdout.write title
stdout.write '\n'
stdout.flushFile()
quit 0
proc cliFail*(spec, message: string) {.noreturn.} =
stdout.write "[Error] "
stdout.write message
stdout.write '\n'
writeSpec(spec)
quit 1
proc cliUnknownLongOption*(spec, key: string) {.noreturn.} =
cliFail(spec, "unknown option: --" & key)
proc cliUnknownShortOption*(spec, key: string) {.noreturn.} =
cliFail(spec, "unknown option: -" & key)
proc cliUnexpectedArgument*(spec, arg: string) {.noreturn.} =
cliFail(spec, "unexpected argument: " & arg)
proc cliMissingArguments*(spec: string) {.noreturn.} =
cliFail(spec, "missing arguments")
proc cliInvalidValue*(spec, optionName, value: string) {.noreturn.} =
cliFail(spec, "invalid value for " & optionName & ": " & value)
template cliapp*(spec: string): untyped {.plugin: "smartcliplugin".}