import parseopt2 const usageString Usage sr OPTIONS search-query proc

 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
import
parseopt2
const usageString =
"""Usage: sr [OPTIONS] <search-query"""
proc doSearch(search: string) =
echo("Searching for ", search)
proc parseOpts(): string =
var
search: string
posArgCount = 0
for kind, key, val in getopt():
case kind
of cmdArgument:
if posArgCount == 0:
search = key
posArgCount += 1
else:
raise newException(SystemError,
"Only one position argument is allowed")
of cmdLongOption, cmdShortOption:
case key
of "help", "h":
raise newException(SystemError, usageString)
else:
raise newException(SystemError,
"Unknown option: " & key)
of cmdEnd: discard
if posArgCount == 0 or search == "":
raise newException(SystemError,
"Search query is not defined")
return search
proc main() =
let search = parseOpts()
doSearch(search)
when isMainModule:
main()