Commit 33f6041b408cc735c09753043fc7645ca1c434de
1 parent
06b0bbf2
make blimp compile with Nim version 0.11.2
Showing
2 changed files
with
65 additions
and
65 deletions
blimp.nim
@@ -12,7 +12,7 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp, subexes, t | @@ -12,7 +12,7 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp, subexes, t | ||
12 | # | 12 | # |
13 | # Just run blimp --help for detailed help. | 13 | # Just run blimp --help for detailed help. |
14 | 14 | ||
15 | -const | 15 | +const |
16 | versionMajor* = 0 | 16 | versionMajor* = 0 |
17 | versionMinor* = 4 | 17 | versionMinor* = 4 |
18 | versionPatch* = 0 | 18 | versionPatch* = 0 |
@@ -88,7 +88,7 @@ proc cmd(cmd: string): string = | @@ -88,7 +88,7 @@ proc cmd(cmd: string): string = | ||
88 | when defined(windows): | 88 | when defined(windows): |
89 | let tup = execCmdEx("cmd /c \"" & cmd & "\"") | 89 | let tup = execCmdEx("cmd /c \"" & cmd & "\"") |
90 | else: | 90 | else: |
91 | - let tup = execCmdEx(cmd) | 91 | + let tup = execCmdEx(cmd) |
92 | #echo "cmd: " & $cmd & "err:" & $tup[1] | 92 | #echo "cmd: " & $cmd & "err:" & $tup[1] |
93 | if tup[1] == 0: | 93 | if tup[1] == 0: |
94 | result = strip(tup[0]) | 94 | result = strip(tup[0]) |
@@ -124,7 +124,7 @@ proc expandDirs(templ: string): string = | @@ -124,7 +124,7 @@ proc expandDirs(templ: string): string = | ||
124 | result = templ.replace("%home%", homeDir) | 124 | result = templ.replace("%home%", homeDir) |
125 | result = result.replace("%cwd%", currentDir) | 125 | result = result.replace("%cwd%", currentDir) |
126 | if result.contains("%gitroot%"): | 126 | if result.contains("%gitroot%"): |
127 | - if gitRootDir.isNil: quit("Not in a git clone, can not expand %gitroot% in '" & templ & "'") | 127 | + if gitRootDir.isNil: quit("Not in a git clone, can not expand %gitroot% in '" & templ & "'") |
128 | result = result.replace("%gitroot%", gitRootDir) | 128 | result = result.replace("%gitroot%", gitRootDir) |
129 | 129 | ||
130 | # Copy of readAllBuffer from sysio.nim - we need to call it | 130 | # Copy of readAllBuffer from sysio.nim - we need to call it |
@@ -147,14 +147,14 @@ proc parseConfFile(filename: string) = | @@ -147,14 +147,14 @@ proc parseConfFile(filename: string) = | ||
147 | var f = newFileStream(filename, fmRead) | 147 | var f = newFileStream(filename, fmRead) |
148 | if f != nil: | 148 | if f != nil: |
149 | if verbose: echo "Reading config: " & filename | 149 | if verbose: echo "Reading config: " & filename |
150 | - var p: CfgParser | 150 | + var p: CfgParser |
151 | open(p, f, filename) | 151 | open(p, f, filename) |
152 | var section: string | 152 | var section: string |
153 | var area: RemoteArea | 153 | var area: RemoteArea |
154 | while true: | 154 | while true: |
155 | var e = next(p) | 155 | var e = next(p) |
156 | case e.kind | 156 | case e.kind |
157 | - of cfgEof: | 157 | + of cfgEof: |
158 | break | 158 | break |
159 | of cfgSectionStart: | 159 | of cfgSectionStart: |
160 | section = e.section | 160 | section = e.section |
@@ -163,7 +163,7 @@ proc parseConfFile(filename: string) = | @@ -163,7 +163,7 @@ proc parseConfFile(filename: string) = | ||
163 | of "blimp": | 163 | of "blimp": |
164 | case e.key | 164 | case e.key |
165 | of "blimpstore": | 165 | of "blimpstore": |
166 | - if blimpStore.isNil: blimpStore = expandDirs(e.value) | 166 | + if blimpStore.isNil: blimpStore = expandDirs(e.value) |
167 | of "version": | 167 | of "version": |
168 | if blimpVersion.isNil: blimpVersion = e.value | 168 | if blimpVersion.isNil: blimpVersion = e.value |
169 | else: | 169 | else: |
@@ -215,7 +215,7 @@ proc uploadFile(filename, fromDir: string) = | @@ -215,7 +215,7 @@ proc uploadFile(filename, fromDir: string) = | ||
215 | let errorCode = run(format(remoteArea.upload, filename, remoteArea.url, toUnixyPath(fromDir))) | 215 | let errorCode = run(format(remoteArea.upload, filename, remoteArea.url, toUnixyPath(fromDir))) |
216 | if errorCode != 0: | 216 | if errorCode != 0: |
217 | quit("Something went wrong uploading " & filename & " to " & remoteArea.url, 2) | 217 | quit("Something went wrong uploading " & filename & " to " & remoteArea.url, 2) |
218 | - | 218 | + |
219 | # Download a file to the remote area | 219 | # Download a file to the remote area |
220 | proc downloadFile(filename, toDir: string) = | 220 | proc downloadFile(filename, toDir: string) = |
221 | if remoteArea.isNil: | 221 | if remoteArea.isNil: |
@@ -243,7 +243,7 @@ proc copyToBlimpStore(filename, blimpFilename: string) = | @@ -243,7 +243,7 @@ proc copyToBlimpStore(filename, blimpFilename: string) = | ||
243 | else: | 243 | else: |
244 | copyFile(filename, blimpStore / blimpFilename) | 244 | copyFile(filename, blimpStore / blimpFilename) |
245 | uploadFile(blimpFilename, blimpStore) | 245 | uploadFile(blimpFilename, blimpStore) |
246 | - | 246 | + |
247 | 247 | ||
248 | # Copy content from blimpStore, and downloading first if needed | 248 | # Copy content from blimpStore, and downloading first if needed |
249 | proc copyFromBlimpStore(blimpFilename, filename: string) = | 249 | proc copyFromBlimpStore(blimpFilename, filename: string) = |
@@ -279,7 +279,7 @@ proc blimpFileName(filename: string): string = | @@ -279,7 +279,7 @@ proc blimpFileName(filename: string): string = | ||
279 | var hashfile: File | 279 | var hashfile: File |
280 | if not open(hashfile, filename): | 280 | if not open(hashfile, filename): |
281 | quit("Failed opening file: " & filename, 4) | 281 | quit("Failed opening file: " & filename, 4) |
282 | - blimpFileNameFromString(string(readLine(hashfile))) | 282 | + blimpFileNameFromString(string(readLine(hashfile))) |
283 | 283 | ||
284 | # Get hash and compute blimpFilename | 284 | # Get hash and compute blimpFilename |
285 | proc computeBlimpFilename(filename: string): string = | 285 | proc computeBlimpFilename(filename: string): string = |
@@ -290,7 +290,7 @@ proc computeBlimpFilename(filename: string): string = | @@ -290,7 +290,7 @@ proc computeBlimpFilename(filename: string): string = | ||
290 | quit("Failed opening file: " & filename, 1) | 290 | quit("Failed opening file: " & filename, 1) |
291 | let hash = getMD5(content) | 291 | let hash = getMD5(content) |
292 | result = extractFilename(filename) & "-" & hash | 292 | result = extractFilename(filename) & "-" & hash |
293 | - | 293 | + |
294 | # Copy original file to blimpStore and replace with hash stub in git. | 294 | # Copy original file to blimpStore and replace with hash stub in git. |
295 | proc deflate(filename: string) = | 295 | proc deflate(filename: string) = |
296 | if verbose: echo "Deflating " & filename | 296 | if verbose: echo "Deflating " & filename |
@@ -312,7 +312,7 @@ iterator allDeflated() = | @@ -312,7 +312,7 @@ iterator allDeflated() = | ||
312 | for fn in filenames: | 312 | for fn in filenames: |
313 | if not blimpFilename(fn).isNil: | 313 | if not blimpFilename(fn).isNil: |
314 | yield fn | 314 | yield fn |
315 | - | 315 | + |
316 | # Iterator over all files matching the blimp filter in the git clone | 316 | # Iterator over all files matching the blimp filter in the git clone |
317 | iterator allFiltered() = | 317 | iterator allFiltered() = |
318 | let lines = cmd("git ls-files | git check-attr --stdin filter").split({'\l', '\c'}) | 318 | let lines = cmd("git ls-files | git check-attr --stdin filter").split({'\l', '\c'}) |
@@ -360,7 +360,7 @@ proc upload(filename: string) = | @@ -360,7 +360,7 @@ proc upload(filename: string) = | ||
360 | if verbose: echo "Uploading " & filename | 360 | if verbose: echo "Uploading " & filename |
361 | uploadFile(filename, currentDir) | 361 | uploadFile(filename, currentDir) |
362 | if verbose: echo("\t" & filename & " uploaded.") | 362 | if verbose: echo("\t" & filename & " uploaded.") |
363 | - | 363 | + |
364 | # Download a file from a remote area. | 364 | # Download a file from a remote area. |
365 | proc download(filename: string) = | 365 | proc download(filename: string) = |
366 | if verbose: echo "Downloading " & filename | 366 | if verbose: echo "Downloading " & filename |
@@ -421,17 +421,17 @@ let help = """ | @@ -421,17 +421,17 @@ let help = """ | ||
421 | Use "blimp i mybigfile" to inflate it back to original content. | 421 | Use "blimp i mybigfile" to inflate it back to original content. |
422 | 422 | ||
423 | When deflated the file only has this content: | 423 | When deflated the file only has this content: |
424 | - | 424 | + |
425 | "blimphash:" <filename> "-" <md5sum> | 425 | "blimphash:" <filename> "-" <md5sum> |
426 | 426 | ||
427 | Deflate also copies the real content to your local blimpstore: | 427 | Deflate also copies the real content to your local blimpstore: |
428 | - | 428 | + |
429 | <blimpstore>/<filename>-<md5sum> | 429 | <blimpstore>/<filename>-<md5sum> |
430 | 430 | ||
431 | ...and if configured also uploads it to "remote", using rsync. | 431 | ...and if configured also uploads it to "remote", using rsync. |
432 | 432 | ||
433 | Configuration is in these locations in order: | 433 | Configuration is in these locations in order: |
434 | - | 434 | + |
435 | ./.blimp.conf | 435 | ./.blimp.conf |
436 | <gitroot>/.blimp.conf | 436 | <gitroot>/.blimp.conf |
437 | ~/<blimpstore>/.blimp.conf | 437 | ~/<blimpstore>/.blimp.conf |
@@ -458,19 +458,19 @@ let help = """ | @@ -458,19 +458,19 @@ let help = """ | ||
458 | both from the local blimpstore and from the remote. This only removes | 458 | both from the local blimpstore and from the remote. This only removes |
459 | the current content version, not older versions. The file itself is first | 459 | the current content version, not older versions. The file itself is first |
460 | inflated, if needed, and not deleted. This only "unblimps" the file. | 460 | inflated, if needed, and not deleted. This only "unblimps" the file. |
461 | - | 461 | + |
462 | The push command (no single character shortcut) will force upload the given file | 462 | The push command (no single character shortcut) will force upload the given file |
463 | from the local blimpstore to the remote. This is normally done automatically, | 463 | from the local blimpstore to the remote. This is normally done automatically, |
464 | but this way you can make sure they are synced onto the remote. | 464 | but this way you can make sure they are synced onto the remote. |
465 | - | 465 | + |
466 | The upload and download commands are used to distribute artifacts typically in a | 466 | The upload and download commands are used to distribute artifacts typically in a |
467 | build script. If no --area is given, they use the standard "remote" area. | 467 | build script. If no --area is given, they use the standard "remote" area. |
468 | - | 468 | + |
469 | In order to have blimp work automatically you can: | 469 | In order to have blimp work automatically you can: |
470 | - | 470 | + |
471 | * Create a .gitattributes file with lines like: | 471 | * Create a .gitattributes file with lines like: |
472 | *.png filter=blimp binary | 472 | *.png filter=blimp binary |
473 | - | 473 | + |
474 | * Configure blimp as a filter by running: | 474 | * Configure blimp as a filter by running: |
475 | git config filter.blimp.clean "blimp -s d %f" | 475 | git config filter.blimp.clean "blimp -s d %f" |
476 | git config filter.blimp.smudge "blimp -s i %f" | 476 | git config filter.blimp.smudge "blimp -s i %f" |
@@ -479,13 +479,13 @@ let help = """ | @@ -479,13 +479,13 @@ let help = """ | ||
479 | 479 | ||
480 | When the above is done (per clone) git will automatically run blimp deflate | 480 | When the above is done (per clone) git will automatically run blimp deflate |
481 | just before committing and blimp inflate when operations are done. | 481 | just before committing and blimp inflate when operations are done. |
482 | - | 482 | + |
483 | This means that if you clone a git repository that already has a .gitattributes | 483 | This means that if you clone a git repository that already has a .gitattributes |
484 | file in it that uses the blimp filter, then you should do: | 484 | file in it that uses the blimp filter, then you should do: |
485 | - | 485 | + |
486 | blimp init | 486 | blimp init |
487 | blimp inflate --filter | 487 | blimp inflate --filter |
488 | - | 488 | + |
489 | This will configure the blimp filter and then find and inflate all deflated | 489 | This will configure the blimp filter and then find and inflate all deflated |
490 | files throughout the clone. | 490 | files throughout the clone. |
491 | """ | 491 | """ |
@@ -493,7 +493,7 @@ let help = """ | @@ -493,7 +493,7 @@ let help = """ | ||
493 | ################################ main ##################################### | 493 | ################################ main ##################################### |
494 | # Set some dirs | 494 | # Set some dirs |
495 | homeDir = getHomeDir() | 495 | homeDir = getHomeDir() |
496 | -homeDir = homeDir[0.. -2] # Not sure why it keeps a trailing "/" on Linux | 496 | +homeDir = homeDir[0..^2] # Not sure why it keeps a trailing "/" on Linux |
497 | currentDir = getCurrentDir() | 497 | currentDir = getCurrentDir() |
498 | gitRootDir = gitRoot() | 498 | gitRootDir = gitRoot() |
499 | 499 | ||
@@ -545,7 +545,7 @@ if not blimpVersion.isNil and blimpVersion != versionAsString: | @@ -545,7 +545,7 @@ if not blimpVersion.isNil and blimpVersion != versionAsString: | ||
545 | quit("Wrong version of blimp, configuration wants: " & blimpVersion) | 545 | quit("Wrong version of blimp, configuration wants: " & blimpVersion) |
546 | 546 | ||
547 | 547 | ||
548 | -# Ok, let's see | 548 | +# Ok, let's see |
549 | var filenames = initSet[string]() | 549 | var filenames = initSet[string]() |
550 | 550 | ||
551 | # Add upp all files to operate on in a Set | 551 | # Add upp all files to operate on in a Set |
lapp.nim
@@ -25,7 +25,7 @@ proc next(L: PLexer) = L.idx += 1 | @@ -25,7 +25,7 @@ proc next(L: PLexer) = L.idx += 1 | ||
25 | 25 | ||
26 | proc skipws(L: PLexer) = | 26 | proc skipws(L: PLexer) = |
27 | while thisChar(L) in Whitespace: next(L) | 27 | while thisChar(L) in Whitespace: next(L) |
28 | - | 28 | + |
29 | proc get(L: PLexer; t: var TLexType): string = | 29 | proc get(L: PLexer; t: var TLexType): string = |
30 | skipws(L) | 30 | skipws(L) |
31 | let c = thisChar(L) | 31 | let c = thisChar(L) |
@@ -33,7 +33,7 @@ proc get(L: PLexer; t: var TLexType): string = | @@ -33,7 +33,7 @@ proc get(L: PLexer; t: var TLexType): string = | ||
33 | if c == '\0': return nil | 33 | if c == '\0': return nil |
34 | result = "" | 34 | result = "" |
35 | result.add(c) | 35 | result.add(c) |
36 | - next(L) | 36 | + next(L) |
37 | t = tchar | 37 | t = tchar |
38 | case c | 38 | case c |
39 | of '-': # '-", "--" | 39 | of '-': # '-", "--" |
@@ -43,7 +43,7 @@ proc get(L: PLexer; t: var TLexType): string = | @@ -43,7 +43,7 @@ proc get(L: PLexer; t: var TLexType): string = | ||
43 | next(L) | 43 | next(L) |
44 | if thisChar(L) == '-': # "---..." | 44 | if thisChar(L) == '-': # "---..." |
45 | t = tdivider | 45 | t = tdivider |
46 | - result.add('-') | 46 | + result.add('-') |
47 | while thisChar(L) == '-': | 47 | while thisChar(L) == '-': |
48 | next(L) | 48 | next(L) |
49 | of Letters: # word | 49 | of Letters: # word |
@@ -73,11 +73,11 @@ proc get(L: PLexer; t: var TLexType): string = | @@ -73,11 +73,11 @@ proc get(L: PLexer; t: var TLexType): string = | ||
73 | result.add('.') | 73 | result.add('.') |
74 | next(L) | 74 | next(L) |
75 | else: discard | 75 | else: discard |
76 | - | ||
77 | -proc get(L: PLexer): string = | 76 | + |
77 | +proc get(L: PLexer): string = | ||
78 | var t: TLexType | 78 | var t: TLexType |
79 | get(L,t) | 79 | get(L,t) |
80 | - | 80 | + |
81 | proc reset(L: PLexer, s: string) = | 81 | proc reset(L: PLexer, s: string) = |
82 | L.str = s | 82 | L.str = s |
83 | L.idx = 0 | 83 | L.idx = 0 |
@@ -85,9 +85,9 @@ proc reset(L: PLexer, s: string) = | @@ -85,9 +85,9 @@ proc reset(L: PLexer, s: string) = | ||
85 | proc newLexer(s: string): PLexer = | 85 | proc newLexer(s: string): PLexer = |
86 | new(result) | 86 | new(result) |
87 | result.reset(s) | 87 | result.reset(s) |
88 | - | 88 | + |
89 | ### a container for values ### | 89 | ### a container for values ### |
90 | - | 90 | + |
91 | type | 91 | type |
92 | TValueKind = enum | 92 | TValueKind = enum |
93 | vInt, | 93 | vInt, |
@@ -110,17 +110,17 @@ type | @@ -110,17 +110,17 @@ type | ||
110 | of vSeq: asSeq*: seq[PValue] | 110 | of vSeq: asSeq*: seq[PValue] |
111 | 111 | ||
112 | proc boolValue(c: bool): PValue = PValue(kind: vBool, asBool: c) | 112 | proc boolValue(c: bool): PValue = PValue(kind: vBool, asBool: c) |
113 | - | 113 | + |
114 | proc fileValue(f: File, name: string): PValue = PValue(kind: vFile, asFile: f, fileName: name) | 114 | proc fileValue(f: File, name: string): PValue = PValue(kind: vFile, asFile: f, fileName: name) |
115 | - | ||
116 | -proc strValue(s: string): PValue = PValue(kind: vString, asString: s) | ||
117 | - | 115 | + |
116 | +proc strValue(s: string): PValue = PValue(kind: vString, asString: s) | ||
117 | + | ||
118 | proc intValue(v: int): PValue = PValue(kind: vInt, asInt: v) | 118 | proc intValue(v: int): PValue = PValue(kind: vInt, asInt: v) |
119 | - | 119 | + |
120 | proc floatValue(v: float): PValue = PValue(kind: vFloat, asFloat: v) | 120 | proc floatValue(v: float): PValue = PValue(kind: vFloat, asFloat: v) |
121 | - | 121 | + |
122 | proc seqValue(v: seq[PValue]): PValue = PValue(kind: vSeq, asSeq: v) | 122 | proc seqValue(v: seq[PValue]): PValue = PValue(kind: vSeq, asSeq: v) |
123 | - | 123 | + |
124 | type | 124 | type |
125 | PSpec = ref TSpec | 125 | PSpec = ref TSpec |
126 | TSpec = object | 126 | TSpec = object |
@@ -137,18 +137,18 @@ proc fail(msg: string) = | @@ -137,18 +137,18 @@ proc fail(msg: string) = | ||
137 | stderr.writeln(progname & ": " & msg) | 137 | stderr.writeln(progname & ": " & msg) |
138 | quit(usage) | 138 | quit(usage) |
139 | 139 | ||
140 | -proc parseSpec(u: string) = | 140 | +proc parseSpec(u: string) = |
141 | var | 141 | var |
142 | L: PLexer | 142 | L: PLexer |
143 | tok: string | 143 | tok: string |
144 | groupCounter: int | 144 | groupCounter: int |
145 | k = 1 | 145 | k = 1 |
146 | - | 146 | + |
147 | let lines = u.splitLines | 147 | let lines = u.splitLines |
148 | L = newLexer(lines[0]) | 148 | L = newLexer(lines[0]) |
149 | progname = L.get | 149 | progname = L.get |
150 | usage = u | 150 | usage = u |
151 | - for line in lines[1..(-1)]: | 151 | + for line in lines[1 .. ^1]: |
152 | var | 152 | var |
153 | isarg = false | 153 | isarg = false |
154 | multiple = false | 154 | multiple = false |
@@ -188,7 +188,7 @@ proc parseSpec(u: string) = | @@ -188,7 +188,7 @@ proc parseSpec(u: string) = | ||
188 | tok = L.get | 188 | tok = L.get |
189 | if tok == nil: continue | 189 | if tok == nil: continue |
190 | # default types for flags and arguments | 190 | # default types for flags and arguments |
191 | - var | 191 | + var |
192 | ftype = if isarg: "string" else: "bool" | 192 | ftype = if isarg: "string" else: "bool" |
193 | defValue = "" | 193 | defValue = "" |
194 | if tok == "(": # typed flag/argument | 194 | if tok == "(": # typed flag/argument |
@@ -212,16 +212,16 @@ proc parseSpec(u: string) = | @@ -212,16 +212,16 @@ proc parseSpec(u: string) = | ||
212 | multiple = t == telipsis | 212 | multiple = t == telipsis |
213 | elif ftype == "bool": # no type or default | 213 | elif ftype == "bool": # no type or default |
214 | defValue = "false" | 214 | defValue = "false" |
215 | - | 215 | + |
216 | if name != nil: | 216 | if name != nil: |
217 | #echo("Param: " & name & " type: " & $ftype & " group: " & $groupCounter & " needsvalue: " & $(ftype != "bool") & " default: " & $defValue & " multiple: " & $multiple) | 217 | #echo("Param: " & name & " type: " & $ftype & " group: " & $groupCounter & " needsvalue: " & $(ftype != "bool") & " default: " & $defValue & " multiple: " & $multiple) |
218 | let spec = PSpec(defVal:defValue, ptype: ftype, group: groupCounter, needsValue: ftype != "bool",multiple:multiple) | 218 | let spec = PSpec(defVal:defValue, ptype: ftype, group: groupCounter, needsValue: ftype != "bool",multiple:multiple) |
219 | - aliases[alias] = name | 219 | + aliases[alias] = name |
220 | parm_spec[name] = spec | 220 | parm_spec[name] = spec |
221 | 221 | ||
222 | -proc tail(s: string): string = s[1..(-1)] | 222 | +proc tail(s: string): string = s[1..^1] |
223 | 223 | ||
224 | -var | 224 | +var |
225 | files = newSeq[File]() | 225 | files = newSeq[File]() |
226 | 226 | ||
227 | proc closeFiles() {.noconv.} = | 227 | proc closeFiles() {.noconv.} = |
@@ -234,16 +234,16 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -234,16 +234,16 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
234 | n = len(args) - 1 | 234 | n = len(args) - 1 |
235 | i = 1 | 235 | i = 1 |
236 | k = 1 | 236 | k = 1 |
237 | - flag,value, arg: string | 237 | + flag,value, arg: string |
238 | info: PSpec | 238 | info: PSpec |
239 | short: bool | 239 | short: bool |
240 | flagvalues: seq[seq[string]] | 240 | flagvalues: seq[seq[string]] |
241 | - | 241 | + |
242 | proc next(): string = | 242 | proc next(): string = |
243 | if i > n: fail("an option required a value!") | 243 | if i > n: fail("an option required a value!") |
244 | result = args[i] | 244 | result = args[i] |
245 | i += 1 | 245 | i += 1 |
246 | - | 246 | + |
247 | proc get_alias(c: char): string = | 247 | proc get_alias(c: char): string = |
248 | result = aliases[c] | 248 | result = aliases[c] |
249 | if result == nil: | 249 | if result == nil: |
@@ -252,7 +252,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -252,7 +252,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
252 | fail("no such argument: " & $n) | 252 | fail("no such argument: " & $n) |
253 | else: | 253 | else: |
254 | fail("no such option: " & c) | 254 | fail("no such option: " & c) |
255 | - | 255 | + |
256 | proc get_spec(name: string): PSpec = | 256 | proc get_spec(name: string): PSpec = |
257 | result = parm_spec[name] | 257 | result = parm_spec[name] |
258 | if result == nil: | 258 | if result == nil: |
@@ -264,7 +264,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -264,7 +264,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
264 | 264 | ||
265 | # Collect failures | 265 | # Collect failures |
266 | var failures = newSeq[string]() | 266 | var failures = newSeq[string]() |
267 | - | 267 | + |
268 | # parse the flags and arguments | 268 | # parse the flags and arguments |
269 | while i <= n: | 269 | while i <= n: |
270 | arg = next() | 270 | arg = next() |
@@ -299,26 +299,26 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -299,26 +299,26 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
299 | if not info.multiple: k += 1 | 299 | if not info.multiple: k += 1 |
300 | flagvalues.add(@[flag,value]) | 300 | flagvalues.add(@[flag,value]) |
301 | info.used = true | 301 | info.used = true |
302 | - | 302 | + |
303 | # Some options disables checking | 303 | # Some options disables checking |
304 | var enableChecks = true | 304 | var enableChecks = true |
305 | for flag,info in parm_spec: | 305 | for flag,info in parm_spec: |
306 | if info.used: | 306 | if info.used: |
307 | if flag == "help" or flag == "version": | 307 | if flag == "help" or flag == "version": |
308 | enableChecks = false | 308 | enableChecks = false |
309 | - | 309 | + |
310 | # Check maximum group used | 310 | # Check maximum group used |
311 | var maxGroup = 0 | 311 | var maxGroup = 0 |
312 | for item in flagvalues: | 312 | for item in flagvalues: |
313 | info = get_spec(item[0]) | 313 | info = get_spec(item[0]) |
314 | if maxGroup < info.group: | 314 | if maxGroup < info.group: |
315 | maxGroup = info.group | 315 | maxGroup = info.group |
316 | - | 316 | + |
317 | # any flags not mentioned? | 317 | # any flags not mentioned? |
318 | for flag,info in parm_spec: | 318 | for flag,info in parm_spec: |
319 | if not info.used: | 319 | if not info.used: |
320 | # Is there no default and we have used options in this group? | 320 | # Is there no default and we have used options in this group? |
321 | - if info.defVal == "" and info.group <= maxGroup: | 321 | + if info.defVal == "" and info.group <= maxGroup: |
322 | failures.add("required option or argument missing: " & flag) | 322 | failures.add("required option or argument missing: " & flag) |
323 | else: | 323 | else: |
324 | flagvalues.add(@[flag,info.defVal]) | 324 | flagvalues.add(@[flag,info.defVal]) |
@@ -373,7 +373,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -373,7 +373,7 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
373 | fail("cannot open " & value) | 373 | fail("cannot open " & value) |
374 | pval = fileValue(f,value) | 374 | pval = fileValue(f,value) |
375 | else: discard | 375 | else: discard |
376 | - | 376 | + |
377 | var oval = vars[flag] | 377 | var oval = vars[flag] |
378 | if info.multiple: # multiple flags are sequence values | 378 | if info.multiple: # multiple flags are sequence values |
379 | if oval == nil: # first value! | 379 | if oval == nil: # first value! |
@@ -388,13 +388,13 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | @@ -388,13 +388,13 @@ proc parseArguments*(usage: string, args: seq[string]): Table[string,PValue] = | ||
388 | return vars | 388 | return vars |
389 | 389 | ||
390 | proc parse*(usage: string): Table[string,PValue] = | 390 | proc parse*(usage: string): Table[string,PValue] = |
391 | - var | 391 | + var |
392 | args: seq[string] | 392 | args: seq[string] |
393 | n = paramCount() | 393 | n = paramCount() |
394 | newSeq(args,n+1) | 394 | newSeq(args,n+1) |
395 | for i in 0..n: | 395 | for i in 0..n: |
396 | args[i] = paramStr(i) | 396 | args[i] = paramStr(i) |
397 | - return parseArguments(usage,args) | 397 | + return parseArguments(usage,args) |
398 | 398 | ||
399 | # Helper proc for verbosity level. | 399 | # Helper proc for verbosity level. |
400 | proc verbosityLevel*(args: Table[string,PValue]): int = | 400 | proc verbosityLevel*(args: Table[string,PValue]): int = |
@@ -432,19 +432,19 @@ when isMainModule: | @@ -432,19 +432,19 @@ when isMainModule: | ||
432 | # These two are special, they short out | 432 | # These two are special, they short out |
433 | if args.showHelp: quit(help) | 433 | if args.showHelp: quit(help) |
434 | if args.showVersion: quit("Version: 1.99") | 434 | if args.showVersion: quit("Version: 1.99") |
435 | - | 435 | + |
436 | # Ok, so what did we get... | 436 | # Ok, so what did we get... |
437 | let n = args["n"].asInt | 437 | let n = args["n"].asInt |
438 | - | 438 | + |
439 | # This one is a helper | 439 | # This one is a helper |
440 | let v = verbosityLevel(args) | 440 | let v = verbosityLevel(args) |
441 | - | 441 | + |
442 | echo "Lines to show: " & $n | 442 | echo "Lines to show: " & $n |
443 | echo "Verbosity level: " & $verbosityLevel(args) | 443 | echo "Verbosity level: " & $verbosityLevel(args) |
444 | - | 444 | + |
445 | let myfiles = args["files"].asSeq | 445 | let myfiles = args["files"].asSeq |
446 | var outFile = args["out"].asFile | 446 | var outFile = args["out"].asFile |
447 | - | 447 | + |
448 | for f in myfiles: | 448 | for f in myfiles: |
449 | for i in 1..n: | 449 | for i in 1..n: |
450 | writeln(outFile, string(f.asFile.readLine())) | 450 | writeln(outFile, string(f.asFile.readLine())) |