summary refs log tree commit diff stats
path: root/lib/pure/parseopt.nim
diff options
context:
space:
mode:
authorDaniil Yarancev <21169548+Yardanico@users.noreply.github.com>2018-06-05 21:25:45 +0300
committerGitHub <noreply@github.com>2018-06-05 21:25:45 +0300
commit642641359821b6a63c6cf7edaaa45873b7ea59c7 (patch)
tree627af3020528cb916b3174bd94304307ca875c77 /lib/pure/parseopt.nim
parentfb44c522e6173528efa8035ecc459c84887d0167 (diff)
parent3cbc07ac7877b03c605498760fe198e3200cc197 (diff)
downloadNim-642641359821b6a63c6cf7edaaa45873b7ea59c7.tar.gz
Merge pull request #2 from nim-lang/devel
Update
Diffstat (limited to 'lib/pure/parseopt.nim')
-rw-r--r--lib/pure/parseopt.nim139
1 files changed, 99 insertions, 40 deletions
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index 23568edb9..58e1be0e4 100644
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -11,11 +11,23 @@
 ## It supports one convenience iterator over all command line options and some
 ## lower-level features.
 ##
-## Supported syntax:
+## Supported syntax with default empty ``shortNoVal``/``longNoVal``:
 ##
 ## 1. short options - ``-abcd``, where a, b, c, d are names
 ## 2. long option - ``--foo:bar``, ``--foo=bar`` or ``--foo``
 ## 3. argument - everything else
+##
+## When ``shortNoVal``/``longNoVal`` are non-empty then the ':' and '=' above
+## are still accepted, but become optional.  Note that these option key sets
+## must be updated along with the set of option keys taking no value, but
+## keys which do take values need no special updates as their set evolves.
+##
+## When option values begin with ':' or '=' they need to be doubled up (as in
+## ``--delim::``) or alternated (as in ``--delim=:``).
+##
+## The common ``--`` non-option argument delimiter appears as an empty string
+## long option key.  ``OptParser.cmd``, ``OptParser.pos``, and
+## ``os.parseCmdLine`` may be used to complete parsing in that case.
 
 {.push debugger: off.}
 
@@ -32,37 +44,37 @@ type
     cmdShortOption            ## a short option ``-c`` detected
   OptParser* =
       object of RootObj ## this object implements the command line parser
-    cmd: string
-    pos: int
+    cmd*: string              #  cmd,pos exported so caller can catch "--" as..
+    pos*: int                 # ..empty key or subcmd cmdArg & handle specially
     inShortState: bool
+    shortNoVal: set[char]
+    longNoVal: seq[string]
     kind*: CmdLineKind        ## the dected command line token
     key*, val*: TaintedString ## key and value pair; ``key`` is the option
                               ## or the argument, ``value`` is not "" if
                               ## the option was given a value
 
-{.deprecated: [TCmdLineKind: CmdLineKind, TOptParser: OptParser].}
-
 proc parseWord(s: string, i: int, w: var string,
-               delim: set[char] = {'\x09', ' ', '\0'}): int =
+               delim: set[char] = {'\x09', ' '}): int =
   result = i
-  if s[result] == '\"':
+  if result < s.len and s[result] == '\"':
     inc(result)
-    while not (s[result] in {'\0', '\"'}):
+    while result < s.len and s[result] != '\"':
       add(w, s[result])
       inc(result)
-    if s[result] == '\"': inc(result)
+    if result < s.len and s[result] == '\"': inc(result)
   else:
-    while not (s[result] in delim):
+    while result < s.len and s[result] notin delim:
       add(w, s[result])
       inc(result)
 
 when declared(os.paramCount):
   proc quote(s: string): string =
-    if find(s, {' ', '\t'}) >= 0 and s[0] != '"':
+    if find(s, {' ', '\t'}) >= 0 and s.len > 0 and s[0] != '"':
       if s[0] == '-':
         result = newStringOfCap(s.len)
-        var i = parseWord(s, 0, result, {'\0', ' ', '\x09', ':', '='})
-        if s[i] in {':','='}:
+        var i = parseWord(s, 0, result, {' ', '\x09', ':', '='})
+        if i < s.len and s[i] in {':','='}:
           result.add s[i]
           inc i
         result.add '"'
@@ -78,11 +90,19 @@ when declared(os.paramCount):
   # we cannot provide this for NimRtl creation on Posix, because we can't
   # access the command line arguments then!
 
-  proc initOptParser*(cmdline = ""): OptParser =
+  proc initOptParser*(cmdline = "", shortNoVal: set[char]={},
+                      longNoVal: seq[string] = @[]): OptParser =
     ## inits the option parser. If ``cmdline == ""``, the real command line
-    ## (as provided by the ``OS`` module) is taken.
+    ## (as provided by the ``OS`` module) is taken.  If ``shortNoVal`` is
+    ## provided command users do not need to delimit short option keys and
+    ## values with a ':' or '='.  If ``longNoVal`` is provided command users do
+    ## not need to delimit long option keys and values with a ':' or '='
+    ## (though they still need at least a space).  In both cases, ':' or '='
+    ## may still be used if desired.  They just become optional.
     result.pos = 0
     result.inShortState = false
+    result.shortNoVal = shortNoVal
+    result.longNoVal = longNoVal
     if cmdline != "":
       result.cmd = cmdline
     else:
@@ -94,47 +114,73 @@ when declared(os.paramCount):
     result.key = TaintedString""
     result.val = TaintedString""
 
+  proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char]={},
+                      longNoVal: seq[string] = @[]): OptParser =
+    ## inits the option parser. If ``cmdline.len == 0``, the real command line
+    ## (as provided by the ``OS`` module) is taken. ``shortNoVal`` and
+    ## ``longNoVal`` behavior is the same as for ``initOptParser(string,...)``.
+    result.pos = 0
+    result.inShortState = false
+    result.shortNoVal = shortNoVal
+    result.longNoVal = longNoVal
+    result.cmd = ""
+    if cmdline.len != 0:
+      for i in 0..<cmdline.len:
+        result.cmd.add quote(cmdline[i].string)
+        result.cmd.add ' '
+    else:
+      for i in countup(1, paramCount()):
+        result.cmd.add quote(paramStr(i).string)
+        result.cmd.add ' '
+    result.kind = cmdEnd
+    result.key = TaintedString""
+    result.val = TaintedString""
+
 proc handleShortOption(p: var OptParser) =
   var i = p.pos
   p.kind = cmdShortOption
   add(p.key.string, p.cmd[i])
   inc(i)
   p.inShortState = true
-  while p.cmd[i] in {'\x09', ' '}:
+  while i < p.cmd.len and p.cmd[i] in {'\x09', ' '}:
     inc(i)
     p.inShortState = false
-  if p.cmd[i] in {':', '='}:
-    inc(i)
+  if i < p.cmd.len and p.cmd[i] in {':', '='} or
+      card(p.shortNoVal) > 0 and p.key.string[0] notin p.shortNoVal:
+    if i < p.cmd.len and p.cmd[i] in {':', '='}:
+      inc(i)
     p.inShortState = false
-    while p.cmd[i] in {'\x09', ' '}: inc(i)
+    while i < p.cmd.len and p.cmd[i] in {'\x09', ' '}: inc(i)
     i = parseWord(p.cmd, i, p.val.string)
-  if p.cmd[i] == '\0': p.inShortState = false
+  if i >= p.cmd.len: p.inShortState = false
   p.pos = i
 
 proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
   ## parses the first or next option; ``p.kind`` describes what token has been
   ## parsed. ``p.key`` and ``p.val`` are set accordingly.
   var i = p.pos
-  while p.cmd[i] in {'\x09', ' '}: inc(i)
+  while i < p.cmd.len and p.cmd[i] in {'\x09', ' '}: inc(i)
   p.pos = i
   setLen(p.key.string, 0)
   setLen(p.val.string, 0)
   if p.inShortState:
     handleShortOption(p)
     return
-  case p.cmd[i]
-  of '\0':
+  if i >= p.cmd.len:
     p.kind = cmdEnd
-  of '-':
+    return
+  if p.cmd[i] == '-':
     inc(i)
-    if p.cmd[i] == '-':
-      p.kind = cmdLongoption
+    if i < p.cmd.len and p.cmd[i] == '-':
+      p.kind = cmdLongOption
       inc(i)
-      i = parseWord(p.cmd, i, p.key.string, {'\0', ' ', '\x09', ':', '='})
-      while p.cmd[i] in {'\x09', ' '}: inc(i)
-      if p.cmd[i] in {':', '='}:
-        inc(i)
-        while p.cmd[i] in {'\x09', ' '}: inc(i)
+      i = parseWord(p.cmd, i, p.key.string, {' ', '\x09', ':', '='})
+      while i < p.cmd.len and p.cmd[i] in {'\x09', ' '}: inc(i)
+      if i < p.cmd.len and p.cmd[i] in {':', '='} or
+          len(p.longNoVal) > 0 and p.key.string notin p.longNoVal:
+        if i < p.cmd.len and p.cmd[i] in {':', '='}:
+          inc(i)
+        while i < p.cmd.len and p.cmd[i] in {'\x09', ' '}: inc(i)
         p.pos = parseWord(p.cmd, i, p.val.string)
       else:
         p.pos = i
@@ -154,7 +200,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedSt
   ## Example:
   ##
   ## .. code-block:: nim
-  ##   var p = initOptParser("--left --debug:3 -l=4 -r:2")
+  ##   var p = initOptParser("--left --debug:3 -l -r:2")
   ##   for kind, key, val in p.getopt():
   ##     case kind
   ##     of cmdArgument:
@@ -174,17 +220,30 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedSt
     yield (p.kind, p.key, p.val)
 
 when declared(initOptParser):
-  iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
-    ## 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
+  iterator getopt*(cmdline: seq[TaintedString] = commandLineParams(),
+                   shortNoVal: set[char]={}, longNoVal: seq[string] = @[]):
+             tuple[kind: CmdLineKind, key, val: TaintedString] =
+    ## This is an convenience iterator for iterating over command line arguments.
+    ## This creates a new OptParser.  See the above ``getopt(var OptParser)``
+    ## example for using default empty ``NoVal`` parameters.  This example is
+    ## for the same option keys as that example but here option key-value
+    ## separators become optional for command users:
     ##
     ## .. code-block:: nim
-    ##   for kind, key, val in getopt():
-    ##     # this will iterate over all arguments passed to the cmdline.
-    ##     continue
+    ##   for kind, key, val in getopt(shortNoVal = { 'l' },
+    ##                                longNoVal = @[ "left" ]):
+    ##     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 == "":
+    ##     writeHelp()
     ##
-    var p = initOptParser()
+    var p = initOptParser(cmdline, shortNoVal=shortNoVal, longNoVal=longNoVal)
     while true:
       next(p)
       if p.kind == cmdEnd: break
^
cfa04c3 ^
9bbfc2b ^
2307cb3 ^
0537f81 ^
3850fba ^
876d629 ^
0537f81 ^
876d629 ^
0537f81 ^
876d629 ^
0537f81 ^
876d629 ^
0537f81 ^
bb9e23a ^
2aa8c67 ^

3850fba ^
1937379 ^
de495ae ^

70a15b3 ^

9f962b7 ^

0fc3ed2 ^



5a04d0c ^
0fc3ed2 ^





5a04d0c ^
e27165c ^
0fc3ed2 ^



5a04d0c ^
0fc3ed2 ^




5a04d0c ^
0fc3ed2 ^
8c535db ^
0fc3ed2 ^
5a04d0c ^
0fc3ed2 ^

5a04d0c ^
0fc3ed2 ^
e27165c ^
0fc3ed2 ^





70a15b3 ^

5a04d0c ^
9f962b7 ^

5a04d0c ^

6587959 ^
2b3e09c ^
6587959 ^
0fc3ed2 ^
e27165c ^


26a98d0 ^

70a15b3 ^
0fc3ed2 ^
0fc3ed2 ^


bdf9b27 ^
5a04d0c ^
0fc3ed2 ^
0fc3ed2 ^


bdf9b27 ^
0fc3ed2 ^
5a04d0c ^
0fc3ed2 ^
5a04d0c ^
0fc3ed2 ^






5a04d0c ^
0fc3ed2 ^


5a04d0c ^
e27165c ^
e27165c ^

5a04d0c ^
e27165c ^


e27165c ^
5a04d0c ^



e27165c ^
e27165c ^

5a04d0c ^
e27165c ^


e27165c ^
5a04d0c ^



0fc3ed2 ^

0fc3ed2 ^


5a04d0c ^
059efba ^
0fc3ed2 ^

0fc3ed2 ^



5a04d0c ^
70a15b3 ^
5d8e7a3 ^

bb9e23a ^

e27165c ^


0fc3ed2 ^


1937379 ^

70a15b3 ^
1937379 ^
92bd683 ^

e0448d7 ^
1937379 ^

457136a ^
1937379 ^
d7ee9a7 ^
457136a ^
d7ee9a7 ^
457136a ^
d7ee9a7 ^

457136a ^
d7ee9a7 ^

457136a ^
d7ee9a7 ^

2b3e09c ^
d7ee9a7 ^
d0d22ff ^
e27165c ^
d7ee9a7 ^
e27165c ^
92bd683 ^
92bd683 ^

c1ba40a ^
e990b1b ^


1937379 ^
c1ba40a ^
5017659 ^
6659de6 ^
1937379 ^
c1d8201 ^
1937379 ^
c1ba40a ^
6659de6 ^
c1ba40a ^
c1ba40a ^

26995dd ^
c1ba40a ^

e990b1b ^
1937379 ^
c1ba40a ^

26995dd ^
c1ba40a ^
e27165c ^
5d8e7a3 ^
c1ba40a ^


26995dd ^

















2b3e09c ^
d6795f9 ^





8bbc1ff ^
d6795f9 ^

1937379 ^
91d79eb ^
3422d92 ^


4818672 ^
26a98d0 ^
91d79eb ^

1937379 ^
91d79eb ^
70a15b3 ^
91d79eb ^
4818672 ^
91d79eb ^

1937379 ^
91d79eb ^
4818672 ^
70a15b3 ^
4818672 ^
70a15b3 ^
4818672 ^
91d79eb ^
1937379 ^
91d79eb ^
4818672 ^
91d79eb ^
1937379 ^
91d79eb ^
70a15b3 ^
91d79eb ^
4818672 ^
91d79eb ^


1937379 ^
91d79eb ^


e27165c ^
70a15b3 ^

91d79eb ^
e27165c ^



91d79eb ^


1937379 ^
91d79eb ^


e27165c ^
70a15b3 ^

91d79eb ^
e27165c ^

91d79eb ^
1937379 ^
91d79eb ^

9d80e92 ^
91d79eb ^
4818672 ^
91d79eb ^
1937379 ^
91d79eb ^



4818672 ^
91d79eb ^
d0d22ff ^
e27165c ^
91d79eb ^
e27165c ^
4818672 ^
d6795f9 ^

4818672 ^

4818672 ^

2b3e09c ^
c1d8201 ^
780105c ^
b1ee70d ^
780105c ^
1ede1c3 ^
b1ee70d ^
70a15b3 ^
b1ee70d ^






780105c ^
b1ee70d ^
780105c ^
1ede1c3 ^
b1ee70d ^
70a15b3 ^
b1ee70d ^








780105c ^
c1d8201 ^
780105c ^
c1d8201 ^
780105c ^
1ede1c3 ^
222a11a ^
70a15b3 ^
222a11a ^



a40d2c4 ^
77ba943 ^
222a11a ^



c1d8201 ^
780105c ^
c1d8201 ^
780105c ^
1ede1c3 ^
e27165c ^
70a15b3 ^
e27165c ^



bce43b5 ^

a40d2c4 ^

e27165c ^

c1d8201 ^
780105c ^
c1d8201 ^
780105c ^
1ede1c3 ^
e27165c ^
70a15b3 ^
e27165c ^



bce43b5 ^



a40d2c4 ^



e27165c ^

780105c ^
1937379 ^

70a15b3 ^
222a11a ^
780105c ^
1937379 ^

70a15b3 ^
2b4e141 ^



c1d8201 ^
780105c ^

1937379 ^
222a11a ^
1937379 ^
222a11a ^
f72185a ^
222a11a ^
c1d8201 ^
780105c ^
1ede1c3 ^
222a11a ^
70a15b3 ^
222a11a ^

bce43b5 ^
222a11a ^


c1d8201 ^
1937379 ^
222a11a ^
780105c ^

fd5a47f ^
780105c ^



222a11a ^
c1d8201 ^
1937379 ^
bb9e23a ^
780105c ^
fd5a47f ^
780105c ^
fd5a47f ^
780105c ^
bb9e23a ^
780105c ^


bb9e23a ^
c1d8201 ^
1937379 ^
222a11a ^
















1937379 ^
222a11a ^


c1d8201 ^
1ede1c3 ^
222a11a ^


3422d92 ^
1ede1c3 ^
3422d92 ^
222a11a ^


e27165c ^

f42c3f8 ^
e27165c ^

f42c3f8 ^
e27165c ^










9d80e92 ^

e27165c ^






9d80e92 ^

e27165c ^



















1ede1c3 ^
059efba ^
1ede1c3 ^
de495ae ^
1937379 ^


de495ae ^





1ede1c3 ^
1937379 ^
de495ae ^
059efba ^
1937379 ^


de495ae ^


1937379 ^
de495ae ^






1ede1c3 ^

de495ae ^
059efba ^
1937379 ^


de495ae ^

70a15b3 ^
1937379 ^
de495ae ^






1ede1c3 ^
1937379 ^
de495ae ^
059efba ^
1937379 ^

de495ae ^










69eeefe ^
de495ae ^













26a98d0 ^






9d80e92 ^

26a98d0 ^



9d80e92 ^



70a15b3 ^
3dd5413 ^
de495ae ^
70a15b3 ^
de495ae ^






70a15b3 ^


307e875 ^
de495ae ^

70a15b3 ^

de495ae ^
9e9e8ed ^

2aa8c67 ^

222a11a ^






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744