about summary refs log tree commit diff
path: root/Test/V12zparseopts.ztst
blob: d7fc33f72ef381aecb7a2def4a4490c1bd0ce689 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Test zparseopts from the zsh/zutil module

%prep

  if zmodload zsh/zutil 2> /dev/null; then
    # Produce a string representing an associative array ordered by its keys
    order_assoc() {
      local -a _arr
      for 2 in "${(@kP)1}"; do
        _arr+=( "${(q-)2} ${(q-)${(P)1}[$2]}" )
      done
      print -r - ${(j< >)${(@o)_arr}}
    }
  else
    ZTST_unimplemented="can't load the zsh/zutil module for testing"
  fi

%test

  () {
    local -a optv
    zparseopts -a optv - a b: c:- z
    print -r - ret: $?, optv: $optv, argv: $argv
  } -ab1 -c -d -e -z
0:zparseopts -a
>ret: 0, optv: -a -b 1 -c-d, argv: -ab1 -c -d -e -z

  () {
    local -A opts
    zparseopts -A opts - a b: c:- z
    print -r - ret: $?, opts: "$( order_assoc opts )", argv: $argv
  } -ab1 -c -d -e -z
0:zparseopts -A
>ret: 0, opts: -a '' -b 1 -c -d, argv: -ab1 -c -d -e -z

  () {
    local -a optv
    zparseopts -D -a optv - a b: c:- z
    print -r - ret: $?, optv: $optv, argv: $argv
  } -ab1 -c -d -e -z
0:zparseopts -D
>ret: 0, optv: -a -b 1 -c-d, argv: -e -z

  () {
    local -a optv
    zparseopts -E -a optv - a b: c:- z
    print -r - ret: $?, optv: $optv, argv: $argv
  } -ab1 -c -d -e -z
0:zparseopts -E
>ret: 0, optv: -a -b 1 -c-d -z, argv: -ab1 -c -d -e -z

  () {
    local -a optv
    zparseopts -D -E -a optv - a b: c:- z
    print -r - ret: $?, optv: $optv, argv: $argv
  } -ab1 -c -d -e -z
0:zparseopts -D -E
>ret: 0, optv: -a -b 1 -c-d -z, argv: -e

  for 1 in '-a -x -z' '-ax -z' '-a --x -z'; do
    () {
      local -a optv
      zparseopts -D -E -F -a optv - a b: c:- z
      print -r - ret: $?, optv: $optv, argv: $argv
    } $=1
  done
0:zparseopts -F
?(anon):zparseopts:2: bad option: x
>ret: 1, optv: , argv: -a -x -z
?(anon):zparseopts:2: bad option: x
>ret: 1, optv: , argv: -ax -z
?(anon):zparseopts:2: bad option: -
>ret: 1, optv: , argv: -a --x -z

  for 1 in '-a 1 2 3' '1 2 3'; do
    () {
      local -a optv=( -x -y -z )
      zparseopts -D -K -a optv - a b: c:- z
      print -r - ret: $?, optv: $optv, argv: $argv
    } $=1
  done
0:zparseopts -K -a
>ret: 0, optv: -a, argv: 1 2 3
>ret: 0, optv: -x -y -z, argv: 1 2 3

  for 1 in '-a 1 2 3' '1 2 3'; do
    () {
      local -A opts=( -b 1 -z '' )
      zparseopts -D -K -A opts - a b: c:- z
      print -r - ret: $?, opts: "$( order_assoc opts )", argv: $argv
    } $=1
  done
0:zparseopts -K -A
>ret: 0, opts: -a '' -b 1 -z '', argv: 1 2 3
>ret: 0, opts: -b 1 -z '', argv: 1 2 3

  () {
    local -a optv
    local -A opts
    zparseopts -D -M -a optv -A opts - a:=-aaa -aaa:
    print -r - ret: $?, optv: $optv, opts: "$( order_assoc opts )", argv: $argv
  } --aaa foo -a bar 1 2 3
0:zparseopts -M
>ret: 0, optv: --aaa bar, opts: --aaa bar, argv: 1 2 3

  () {
    local -a optv aa ab
    zparseopts -a optv - a=aa b:=ab c:- z
    print -r - ret: $?, optv: $optv, aa: $aa, ab: $ab, argv: $argv
  } -ab1 -c -d
0:multiple arrays
>ret: 0, optv: -c-d, aa: -a, ab: -b 1, argv: -ab1 -c -d

  for 1 in '-a - -b - - -b' '-a -- -b -- -- -b' '-a 1 -b - - -b'; do
    # -D alone strips - out
    () {
      local -a optv
      zparseopts -D -F -a optv - a b: c:- z
      print -r - '(-D   )' ret: $?, optv: $optv, argv: $argv
    } $=1
    # -D -E leaves - in
    () {
      local -a optv
      zparseopts -D -E -F -a optv - a b: c:- z
      print -r - '(-D -E)' ret: $?, optv: $optv, argv: $argv
    } $=1
  done
0:-/-- handling
>(-D   ) ret: 0, optv: -a, argv: -b - - -b
>(-D -E) ret: 0, optv: -a, argv: - -b - - -b
>(-D   ) ret: 0, optv: -a, argv: -b -- -- -b
>(-D -E) ret: 0, optv: -a, argv: -- -b -- -- -b
>(-D   ) ret: 0, optv: -a, argv: 1 -b - - -b
>(-D -E) ret: 0, optv: -a -b -, argv: 1 - -b

  # Escaping should always work, but it's optional on the first character
  for specs in '\+ \: \= \\' '+ : = \'; do
    () {
      local -a optv
      zparseopts -D -a optv - $=specs
      print -r - ret: $?, optv: $optv, argv: $argv
    } -+:=\\ 1 2 3
  done
  () {
    local -a optv
    zparseopts -D -a optv - '-\:\:\::'
    print -r - ret: $?, optv: $optv, argv: $argv
  } --:::foo 1 2 3
0:special characters in option names
>ret: 0, optv: -+ -: -= -\, argv: 1 2 3
>ret: 0, optv: -+ -: -= -\, argv: 1 2 3
>ret: 0, optv: --::: foo, argv: 1 2 3

  for specs in '-foo: -foobar' '-foobar -foo:'; do
    () {
      local -a optv
      zparseopts -a optv - $=specs
      print -r - ret: $?, optv: $optv, argv: $argv
    } --foobar 1 2 3
  done
0:overlapping option specs (scan order)
>ret: 0, optv: --foobar, argv: --foobar 1 2 3
>ret: 0, optv: --foo bar, argv: --foobar 1 2 3

  () {
    local -a optv
    zparseopts -a optv - a b: c:- z
    print -r - ret: $?, optv: $optv, argv: $argv
  } -ab1 -c
0:missing optarg
?(anon):zparseopts:2: missing argument for option: c
>ret: 1, optv: , argv: -ab1 -c