-
Notifications
You must be signed in to change notification settings - Fork 0
/
Getopts.jl
82 lines (73 loc) · 2.24 KB
/
Getopts.jl
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
module Getopts
export getopts
done(instr, idx) = iterate(instr, idx)===nothing
"""
```
function skipcwhile(instr, startidx=1, mchars=" \\t")
```
Consume and skip characters **WHILE** they match characters specified in
mchars. Return index of first character following consumed characters
in string.
"""
function skipcwhile(instr, startidx=1, mchars=" \t")
idx = startidx
while !done(instr, idx) && instr[idx] in mchars
idx = iterate(instr, idx)[2]
end
idx
end
"""
```
function getcuntil(instr, startidx=1, mchars=" \\t")
```
Consume and return characters in input string **UNTIL** a character
specified in mchars (match characters) occurs. Also return index
of first character following consumed characters in string.
"""
function getcuntil(instr, startidx=1, mchars=" \t")
idx = startidx
outstr = ""
while !done(instr, idx) && !(instr[idx] in mchars)
outstr = string(outstr, instr[idx])
idx = iterate(instr, idx)[2]
end
outstr, idx
end
"""
```
function getcuntilunless(instr, startidx=1, mchars=" \\t", uchars="-")
```
Consume and return characters in input string **UNTIL** a character
specified in mchars (match characters) occurs, **UNLESS** the input string
starts with a character specified in uchars. Also return index
of first character following consumed characters in string.
"""
function getcuntilunless(instr, startidx=1, mchars=" \t", uchars="-")
idx = skipcwhile(instr, startidx, mchars)
outstr, idx = done(instr, idx) || (instr[idx] in uchars) ? ("", idx) : getcuntil(instr, idx, " \t")
end
getopt = getcuntil
getarg = getcuntilunless
function getopts(instr::AbstractString)
opts = Dict{AbstractString,Array{AbstractString,1}}()
argv = Array{AbstractString,1}()
idx = 1
while !done(instr, idx)
while true
arg, idx = getarg(instr, idx, " \t", "-")
arg != "" && push!(argv, arg)
(arg == "" || done(instr, idx)) && break
end
if !done(instr, idx)
key, idx = getopt(instr, idx, " \t")
!(key in keys(opts)) && (opts[key] = Array{AbstractString,1}())
val, idx = getarg(instr, idx, " \t", "-")
push!(opts[key], val)
end
end
opts, argv
end
function getopts(inarray::AbstractArray{S,1}) where S<:AbstractString
getopts(foldl(string,map(x->string(" ",x),inarray)))
end
end