]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0040-parse-options.sh
api-parse-options.txt: Introduce documentation for parse options API
[thirdparty/git.git] / t / t0040-parse-options.sh
CommitLineData
beb47437
JS
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
10cat > expect.err << EOF
11usage: 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
17string 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
24magic arguments
25 --quux means --quux
26
beb47437
JS
27EOF
28
29test_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
35cat > expect << EOF
36boolean: 2
37integer: 1729
38string: 123
39EOF
40
41test_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'
46cat > expect << EOF
47boolean: 2
48integer: 1729
49string: 321
50EOF
51
52test_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
59cat > expect << EOF
60boolean: 1
61integer: 13
62string: 123
63arg 00: a1
64arg 01: b1
65arg 02: --boolean
66EOF
67
68test_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
75cat > expect << EOF
76boolean: 0
77integer: 2
78string: (not set)
79EOF
80
81test_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
87test_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 93test_expect_success 'ambiguously abbreviated option' '
7f275b91 94 test-parse-options --strin 123;
41ac414e 95 test $? = 129
7f275b91
JS
96'
97
243e0614
JS
98cat > expect << EOF
99boolean: 0
100integer: 0
101string: 123
102EOF
103
104test_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
110cat > expect.err << EOF
111error: did you mean \`--boolean\` (with two dashes ?)
112EOF
113
114test_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
120cat > expect <<EOF
121boolean: 0
122integer: 0
123string: (not set)
124arg 00: --quux
125EOF
126
127test_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 133test_done