1
+ import tables
2
+ import parseopt2
3
+
4
+ type
5
+
6
+ ValueKind * = enum
7
+ vkNone,
8
+ vkBool,
9
+ vkStr,
10
+
11
+ Value * = object
12
+ case kind* : ValueKind
13
+ of vkNone:
14
+ nil
15
+ of vkBool:
16
+ bool_v: bool
17
+ of vkStr:
18
+ str_v: string
19
+
20
+
21
+ converter to_bool * (v: Value ): bool =
22
+ case v.kind
23
+ of vkNone:
24
+ false
25
+ of vkBool:
26
+ v.bool_v
27
+ of vkStr:
28
+ v.str_v != nil and v.str_v.len > 0
29
+
30
+
31
+ proc val (): Value = Value (kind: vkNone)
32
+ proc val (v: bool ): Value = Value (kind: vkBool, bool_v: v)
33
+ proc val (v: string ): Value = Value (kind: vkStr, str_v: v)
34
+
35
+
36
+ proc `$` * (v: Value ): string =
37
+ case v.kind
38
+ of vkNone:
39
+ " nil"
40
+ of vkBool:
41
+ $ v.bool_v
42
+ of vkStr:
43
+ v.str_v
44
+
45
+
46
+ proc parse_args * (doc: string ): Table [string , Value ] =
47
+
48
+ result = {
49
+ " fq" : val (),
50
+ " fa" : val (),
51
+ " sam" : val (),
52
+ " <file>" : val (),
53
+ " color-atla" : val (),
54
+ " example-config" : val (),
55
+
56
+ " --phred" : val (),
57
+ " --hist" : val (),
58
+ " --delimiter" : val (),
59
+ " --multiline" : val (),
60
+ " --color" : val (),
61
+ " --type" : val (),
62
+ " --config-file" : val (),
63
+ }.toTable ()
64
+
65
+ var arg_ct: int = 0
66
+
67
+ for kind, key, value in getopt ():
68
+ when not defined (release):
69
+ stderr.write (($ kind) & " " & ($ key) & " " & ($ value) & " \n " )
70
+ stderr.flushFile ()
71
+ case kind
72
+ of cmdArgument:
73
+ case arg_ct:
74
+ of 0 :
75
+ case key:
76
+ of " fq" :
77
+ result [" fq" ] = val (true )
78
+ of " fa" :
79
+ result [" fa" ] = val (true )
80
+ of " sam" :
81
+ result [" sam" ] = val (true )
82
+ of " color-atla" :
83
+ result [" color-atla" ] = val (true )
84
+ return result
85
+ of " example-config" :
86
+ result [" example-config" ] = val (true )
87
+ return result
88
+ of 1 :
89
+ result [" <file>" ] = val (key)
90
+ else :
91
+ echo doc
92
+ quit (1 )
93
+ inc arg_ct
94
+ of cmdShortOption:
95
+ case key
96
+ of " h" :
97
+ echo doc
98
+ quit (0 )
99
+ if key == " " and value == " " :
100
+ result [" <file>" ] = val (" -" )
101
+ inc arg_ct
102
+ of cmdLongOption:
103
+ case key:
104
+ of " h" , " help" :
105
+ echo doc
106
+ quit (0 )
107
+ of " phred" :
108
+ result [" --phred" ] = val (value)
109
+ of " hist" :
110
+ result [" --hist" ] = val (value)
111
+ of " delimiter" :
112
+ result [" --delimiter" ] = val (value)
113
+ of " multiline" :
114
+ if value != " " :
115
+ result [" --multiline" ] = val (value)
116
+ else :
117
+ result [" --multiline" ] = val (" yes" )
118
+ of " color" :
119
+ result [" --color" ] = val (value)
120
+ of " type" :
121
+ result [" --type" ] = val (value)
122
+ of " config-file" :
123
+ result [" --config-file" ] = val (value)
124
+ of cmdEnd:
125
+ discard
126
+
127
+ if (not result [" fq" ]) and (not result [" fa" ]) and (not result [" sam" ]) and
128
+ (not result [" example-config" ]) and (not result [" color-atla" ]):
129
+ echo doc
130
+ quit (1 )
131
+
132
+
133
+ when isMainModule :
134
+ discard
0 commit comments