summary refs log tree commit diff stats
path: root/tests/misc/tparseopt.nim
blob: 47be05bac9e6953c282ed39bd3d3cd185427add4 (plain) (blame)
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
discard """
  output: '''
parseopt
first round
kind: cmdLongOption	key:val  --  left:
second round
kind: cmdLongOption	key:val  --  left:
kind: cmdLongOption	key:val  --  debug:3
kind: cmdShortOption	key:val  --  l:4
kind: cmdShortOption	key:val  --  r:2
cmdLongOption foo
cmdLongOption path
parseoptNoVal
kind: cmdLongOption	key:val  --  left:
kind: cmdLongOption	key:val  --  debug:3
kind: cmdShortOption	key:val  --  l:
kind: cmdShortOption	key:val  --  r:2
kind: cmdLongOption	key:val  --  debug:2
kind: cmdLongOption	key:val  --  debug:1
kind: cmdShortOption	key:val  --  r:1
kind: cmdShortOption	key:val  --  r:0
kind: cmdShortOption	key:val  --  l:
kind: cmdShortOption	key:val  --  r:4
kind: cmdLongOption	key:val  --  debug:
cmdShortOption key: v value: ''
cmdArgument key: ABC value: ''
cmdShortOption key: v value: 'ABC'
cmdShortOption key: v value: ''
cmdArgument key: ABC value: ''
cmdShortOption key: v value: ''
cmdArgument key: ABC value: ''
cmdShortOption key: j value: '4'
cmdArgument key: ok value: ''
'''
joinable: false
"""

when defined(testament_tparseopt):
  import os
  proc main() =
    let args = commandLineParams()
    echo args
    for i, ai in args:
      echo "arg ", i, " ai.len:", ai.len, " :{", ai, "}"
  main()
else:
  from parseopt import nil

  block:
    echo "parseopt"
    for kind, key, val in parseopt.getopt():
      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val

    # pass custom cmdline arguments
    echo "first round"
    var argv = "--left --debug:3 -l=4 -r:2"
    var p = parseopt.initOptParser(argv)
    for kind, key, val in parseopt.getopt(p):
      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
      break
    # reset getopt iterator and check arguments are returned correctly.
    echo "second round"
    for kind, key, val in parseopt.getopt(p):
      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val

    # bug #9619
    var x = parseopt.initOptParser(@["--foo:", "--path"],
        allowWhitespaceAfterColon = false)
    for kind, key, val in parseopt.getopt(x):
      echo kind, " ", key

  block:
    echo "parseoptNoVal"
    # test NoVal mode with custom cmdline arguments
    var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4 --debug:"
    var p = parseopt.initOptParser(argv,
                                    shortNoVal = {'l'}, longNoVal = @["left"])
    for kind, key, val in parseopt.getopt(p):
      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val

  import osproc, os, strutils
  from stdtest/specialpaths import buildDir
  import stdtest/unittest_light

  block: # fix #9951
    template runTest(parseoptCustom) =
      var p = parseoptCustom.initOptParser(@["echo \"quoted\""])
      let expected = when defined(windows):
        """"echo \"quoted\"""""
      else:
        """'echo "quoted"'"""
      assertEquals parseoptCustom.cmdLineRest(p), expected

      doAssert "a5'b" == "a5\'b"

      let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
      var p2 = parseoptCustom.initOptParser(args)
      let expected2 = when defined(windows):
        """a1b "a2 b" "" a4\"b a5'b a6\b a7'b"""
      else:
        """a1b 'a2 b' '' 'a4"b' 'a5'"'"'b' 'a6\b' 'a7'"'"'b'"""
      doAssert "a5'b" == "a5\'b"
      assertEquals parseoptCustom.cmdLineRest(p2), expected2
    runTest(parseopt)

  block: # fix #9842
    let exe = buildDir / "D20190112T145450".addFileExt(ExeExt)
    defer:
      when not defined(windows):
        # workaround #10359 ; innocuous to skip since we're saving under `buildDir`
        removeFile exe
    let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
    let cmd = "$# c -r --verbosity:0 -o:$# -d:testament_tparseopt $# $#" %
      [getCurrentCompilerExe(), exe, currentSourcePath(),
          args.quoteShellCommand]
    var ret = execCmdEx(cmd, options = {})
    if ret.exitCode != 0:
      # before bug fix, running cmd would show:
      # sh: -c: line 0: unexpected EOF while looking for matching `"'\n
      echo "exitCode: ", ret.exitCode, " cmd:", cmd
      doAssert false
    stripLineEnd(ret.output)
    assertEquals ret.output,
      """
@["a1b", "a2 b", "", "a4\"b", "a5\'b", "a6\\b", "a7\'b"]
arg 0 ai.len:3 :{a1b}
arg 1 ai.len:4 :{a2 b}
arg 2 ai.len:0 :{}
arg 3 ai.len:4 :{a4"b}
arg 4 ai.len:4 :{a5'b}
arg 5 ai.len:4 :{a6\b}
arg 6 ai.len:4 :{a7'b}"""



  block:
    let args = @["-v", "ABC"]
    var p = parseopt.initOptParser(args, shortnoVal = {'n'}, longnoVal = @["novalue"])
    for kind, key, val in parseopt.getopt(p):
      echo kind," key: ", key, " value: '", val, "'"

    var r = parseopt.initOptParser(@["-v ABC"], shortnoVal = {'n'}, longnoVal = @["novalue"])
    for kind, key, val in parseopt.getopt(r):
      echo kind," key: ", key, " value: '", val, "'"

    var s = parseopt.initOptParser("-v ABC", shortnoVal = {'v'}, longnoVal = @["novalue"])
    for kind, key, val in parseopt.getopt(s):
      echo kind," key: ", key, " value: '", val, "'"

    var m = parseopt.initOptParser("-v ABC", shortnoVal = {'n'}, longnoVal = @["novalue"])
    for kind, key, val in parseopt.getopt(m):
      echo kind," key: ", key, " value: '", val, "'"

    var n = parseopt.initOptParser("-j4 ok", shortnoVal = {'n'}, longnoVal = @["novalue"])
    for kind, key, val in parseopt.getopt(n):
      echo kind," key: ", key, " value: '", val, "'"