]>
Commit | Line | Data |
---|---|---|
beb47437 JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Johannes Schindelin | |
4 | # | |
5 | ||
6 | test_description='our own option parser' | |
7 | ||
8 | . ./test-lib.sh | |
9 | ||
10 | cat > expect.err << EOF | |
11 | usage: test-parse-options <options> | |
12 | ||
13 | -b, --boolean get a boolean | |
14 | -i, --integer <n> get a integer | |
15 | -j <n> get a integer, too | |
16 | ||
17 | string options | |
18 | -s, --string <string> | |
19 | get a string | |
20 | --string2 <str> get another string | |
243e0614 | 21 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 22 | -o <str> get another string |
beb47437 | 23 | |
580d5bff PH |
24 | magic arguments |
25 | --quux means --quux | |
26 | ||
beb47437 JS |
27 | EOF |
28 | ||
29 | test_expect_success 'test help' ' | |
30 | ! test-parse-options -h > output 2> output.err && | |
31 | test ! -s output && | |
3af82863 | 32 | test_cmp expect.err output.err |
beb47437 JS |
33 | ' |
34 | ||
35 | cat > expect << EOF | |
36 | boolean: 2 | |
37 | integer: 1729 | |
38 | string: 123 | |
39 | EOF | |
40 | ||
41 | test_expect_success 'short options' ' | |
42 | test-parse-options -s123 -b -i 1729 -b > output 2> output.err && | |
3af82863 | 43 | test_cmp expect output && |
beb47437 JS |
44 | test ! -s output.err |
45 | ' | |
46 | cat > expect << EOF | |
47 | boolean: 2 | |
48 | integer: 1729 | |
49 | string: 321 | |
50 | EOF | |
51 | ||
52 | test_expect_success 'long options' ' | |
53 | test-parse-options --boolean --integer 1729 --boolean --string2=321 \ | |
54 | > output 2> output.err && | |
55 | test ! -s output.err && | |
3af82863 | 56 | test_cmp expect output |
beb47437 JS |
57 | ' |
58 | ||
59 | cat > expect << EOF | |
60 | boolean: 1 | |
61 | integer: 13 | |
62 | string: 123 | |
63 | arg 00: a1 | |
64 | arg 01: b1 | |
65 | arg 02: --boolean | |
66 | EOF | |
67 | ||
68 | test_expect_success 'intermingled arguments' ' | |
69 | test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ | |
70 | > output 2> output.err && | |
71 | test ! -s output.err && | |
3af82863 | 72 | test_cmp expect output |
beb47437 JS |
73 | ' |
74 | ||
7f275b91 JS |
75 | cat > expect << EOF |
76 | boolean: 0 | |
77 | integer: 2 | |
78 | string: (not set) | |
79 | EOF | |
80 | ||
81 | test_expect_success 'unambiguously abbreviated option' ' | |
82 | test-parse-options --int 2 --boolean --no-bo > output 2> output.err && | |
83 | test ! -s output.err && | |
3af82863 | 84 | test_cmp expect output |
7f275b91 JS |
85 | ' |
86 | ||
87 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
88 | test-parse-options --int=2 > output 2> output.err && | |
89 | test ! -s output.err && | |
3af82863 | 90 | test_cmp expect output |
7f275b91 JS |
91 | ' |
92 | ||
41ac414e | 93 | test_expect_success 'ambiguously abbreviated option' ' |
7f275b91 | 94 | test-parse-options --strin 123; |
41ac414e | 95 | test $? = 129 |
7f275b91 JS |
96 | ' |
97 | ||
243e0614 JS |
98 | cat > expect << EOF |
99 | boolean: 0 | |
100 | integer: 0 | |
101 | string: 123 | |
102 | EOF | |
103 | ||
104 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' | |
105 | test-parse-options --st 123 > output 2> output.err && | |
106 | test ! -s output.err && | |
3af82863 | 107 | test_cmp expect output |
243e0614 JS |
108 | ' |
109 | ||
3a9f0f41 PH |
110 | cat > expect.err << EOF |
111 | error: did you mean \`--boolean\` (with two dashes ?) | |
112 | EOF | |
113 | ||
114 | test_expect_success 'detect possible typos' ' | |
115 | ! test-parse-options -boolean > output 2> output.err && | |
116 | test ! -s output && | |
3af82863 | 117 | test_cmp expect.err output.err |
3a9f0f41 PH |
118 | ' |
119 | ||
580d5bff PH |
120 | cat > expect <<EOF |
121 | boolean: 0 | |
122 | integer: 0 | |
123 | string: (not set) | |
124 | arg 00: --quux | |
125 | EOF | |
126 | ||
127 | test_expect_success 'keep some options as arguments' ' | |
128 | test-parse-options --quux > output 2> output.err && | |
129 | test ! -s output.err && | |
3af82863 | 130 | test_cmp expect output |
580d5bff PH |
131 | ' |
132 | ||
beb47437 | 133 | test_done |