Commit 809a975c625ec78e9b565cc270d5203123dc0c78
1 parent
ef220068
Cleanups and simpligied files as a seq.
Showing
1 changed file
with
16 additions
and
12 deletions
lapp.nim
1 | 1 | import strutils |
2 | -from os import paramCount, paramStr | |
2 | +import os | |
3 | 3 | import tables |
4 | 4 | export tables.`[]` |
5 | 5 | |
... | ... | @@ -125,7 +125,7 @@ var |
125 | 125 | progname, usage: string |
126 | 126 | aliases: array[char,string] |
127 | 127 | parm_spec = initTable[string,PSpec]() |
128 | - | |
128 | + | |
129 | 129 | proc fail(msg: string) = |
130 | 130 | stderr.writeln(progname & ": " & msg) |
131 | 131 | quit(usage) |
... | ... | @@ -213,12 +213,11 @@ proc parseSpec(u: string) = |
213 | 213 | proc tail(s: string): string = s[1..(-1)] |
214 | 214 | |
215 | 215 | var |
216 | - files: array[1..MAX_FILES,File] | |
217 | - nfiles = 0 | |
216 | + files = newSeq[File]() | |
218 | 217 | |
219 | 218 | proc closeFiles() {.noconv.} = |
220 | - if nfiles == 0: return | |
221 | - for i in 1..nfiles: files[i].close() | |
219 | + for f in files: | |
220 | + f.close() | |
222 | 221 | |
223 | 222 | proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = |
224 | 223 | var |
... | ... | @@ -308,14 +307,14 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = |
308 | 307 | try: |
309 | 308 | v = value.parseInt |
310 | 309 | except: |
311 | - fail("bad integer") | |
310 | + fail("bad integer for " & flag) | |
312 | 311 | pval = intValue(v) |
313 | 312 | of "float": |
314 | 313 | var v: float |
315 | 314 | try: |
316 | 315 | v = value.parseFloat |
317 | 316 | except: |
318 | - fail("bad float") | |
317 | + fail("bad float for " & flag) | |
319 | 318 | pval = floatValue(v) |
320 | 319 | of "bool": |
321 | 320 | pval = boolValue(value.parseBool) |
... | ... | @@ -325,12 +324,17 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = |
325 | 324 | var f: File |
326 | 325 | try: |
327 | 326 | if info.ptype == "infile": |
328 | - f = if value=="stdin": stdin else: open(value,fmRead) | |
327 | + if value == "stdin": | |
328 | + f = stdin | |
329 | + else: | |
330 | + f = open(value, fmRead) | |
329 | 331 | else: |
330 | - f = if value=="stdout": stdout else: open(value,fmWrite) | |
332 | + if value == "stdout": | |
333 | + f = stdout | |
334 | + else: | |
335 | + f = open(value, fmWrite) | |
331 | 336 | # they will be closed automatically on program exit |
332 | - nfiles += 1 | |
333 | - if nfiles <= MAX_FILES: files[nfiles] = f | |
337 | + files.add(f) | |
334 | 338 | except: |
335 | 339 | fail("cannot open " & value) |
336 | 340 | pval = fileValue(f,value) | ... | ... |