Commit 809a975c625ec78e9b565cc270d5203123dc0c78

Authored by Göran Krampe
1 parent ef220068

Cleanups and simpligied files as a seq.

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