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
21
22EOF
23
24test_expect_success 'test help' '
25 ! test-parse-options -h > output 2> output.err &&
26 test ! -s output &&
27 git diff expect.err output.err
28'
29
30cat > expect << EOF
31boolean: 2
32integer: 1729
33string: 123
34EOF
35
36test_expect_success 'short options' '
37 test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
38 git diff expect output &&
39 test ! -s output.err
40'
41cat > expect << EOF
42boolean: 2
43integer: 1729
44string: 321
45EOF
46
47test_expect_success 'long options' '
48 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
49 > output 2> output.err &&
50 test ! -s output.err &&
51 git diff expect output
52'
53
54cat > expect << EOF
55boolean: 1
56integer: 13
57string: 123
58arg 00: a1
59arg 01: b1
60arg 02: --boolean
61EOF
62
63test_expect_success 'intermingled arguments' '
64 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
65 > output 2> output.err &&
66 test ! -s output.err &&
67 git diff expect output
68'
69
70cat > expect << EOF
71boolean: 0
72integer: 2
73string: (not set)
74EOF
75
76test_expect_success 'unambiguously abbreviated option' '
77 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
78 test ! -s output.err &&
79 git diff expect output
80'
81
82test_expect_success 'unambiguously abbreviated option with "="' '
83 test-parse-options --int=2 > output 2> output.err &&
84 test ! -s output.err &&
85 git diff expect output
86'
87
88test_expect_failure 'ambiguously abbreviated option' '
89 test-parse-options --strin 123;
90 test $? != 129
91'
92
93test_done