summary refs log tree commit diff stats
path: root/lib/pure/parseopt2.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-07-13 04:48:22 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-07-13 04:48:22 +0200
commit2b862b74e0b0b7b4a18f4262356289fb921eaf0c (patch)
tree8f41b7355f6d791d6485e8225d6a5cb2f80ca7d6 /lib/pure/parseopt2.nim
parenta5695c13afabac6e67ff677d564b6d1a6aeb1af4 (diff)
parent0c271f54208c7ba0bac6ad2da87f60e7c6d8e37c (diff)
downloadNim-2b862b74e0b0b7b4a18f4262356289fb921eaf0c.tar.gz
Merge branch 'devel' into araq
Diffstat (limited to 'lib/pure/parseopt2.nim')
-rw-r--r--lib/pure/parseopt2.nim45
1 files changed, 30 insertions, 15 deletions
diff --git a/lib/pure/parseopt2.nim b/lib/pure/parseopt2.nim
index 7fd9c60fe..2e8dbe140 100644
--- a/lib/pure/parseopt2.nim
+++ b/lib/pure/parseopt2.nim
@@ -123,26 +123,41 @@ type
 
 {.deprecated: [TGetoptResult: GetoptResult].}
 
+iterator getopt*(p: var OptParser): GetoptResult =
+  ## This is an convenience iterator for iterating over the given OptParser object.
+  ## Example:
+  ##
+  ## .. code-block:: nim
+  ##   var p = initOptParser("--left --debug:3 -l=4 -r:2")
+  ##   for kind, key, val in p.getopt():
+  ##     case kind
+  ##     of cmdArgument:
+  ##       filename = key
+  ##     of cmdLongOption, cmdShortOption:
+  ##       case key
+  ##       of "help", "h": writeHelp()
+  ##       of "version", "v": writeVersion()
+  ##     of cmdEnd: assert(false) # cannot happen
+  ##   if filename == "":
+  ##     # no filename has been given, so we show the help:
+  ##     writeHelp()
+  p.pos = 0
+  while true:
+    next(p)
+    if p.kind == cmdEnd: break
+    yield (p.kind, p.key, p.val)
+
 when declared(paramCount):
   iterator getopt*(): GetoptResult =
-    ## This is an convenience iterator for iterating over the command line.
-    ## This uses the OptParser object. Example:
+    ## This is an convenience iterator for iterating over the command line arguments.
+    ## This create a new OptParser object.
+    ## See above for a more detailed example
     ##
     ## .. code-block:: nim
-    ##   var
-    ##     filename = ""
     ##   for kind, key, val in getopt():
-    ##     case kind
-    ##     of cmdArgument:
-    ##       filename = key
-    ##     of cmdLongOption, cmdShortOption:
-    ##       case key
-    ##       of "help", "h": writeHelp()
-    ##       of "version", "v": writeVersion()
-    ##     of cmdEnd: assert(false) # cannot happen
-    ##   if filename == "":
-    ##     # no filename has been given, so we show the help:
-    ##     writeHelp()
+    ##     # this will iterate over all arguments passed to the cmdline.
+    ##     continue
+    ##
     var p = initOptParser()
     while true:
       next(p)
> 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337