t / t0040-parse-options.shon commit Merge branch 'jk/commit-v-strip' (47a7925)
   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    -4, --or4             bitwise-or boolean with ...0100
  15
  16    -i, --integer <n>     get a integer
  17    -j <n>                get a integer, too
  18    --set23               set integer to 23
  19    -t <time>             get timestamp of <time>
  20    -L, --length <str>    get length of <str>
  21
  22String options
  23    -s, --string <string>
  24                          get a string
  25    --string2 <str>       get another string
  26    --st <st>             get another string (pervert ordering)
  27    -o <str>              get another string
  28    --default-string      set string to default
  29
  30Magic arguments
  31    --quux                means --quux
  32
  33Standard options
  34    --abbrev[=<n>]        use <n> digits to display SHA-1s
  35    -v, --verbose         be verbose
  36    -n, --dry-run         dry run
  37    -q, --quiet           be quiet
  38
  39EOF
  40
  41test_expect_success 'test help' '
  42        test_must_fail test-parse-options -h > output 2> output.err &&
  43        test ! -s output &&
  44        test_cmp expect.err output.err
  45'
  46
  47cat > expect << EOF
  48boolean: 2
  49integer: 1729
  50timestamp: 0
  51string: 123
  52abbrev: 7
  53verbose: 2
  54quiet: no
  55dry run: yes
  56EOF
  57
  58test_expect_success 'short options' '
  59        test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
  60        test_cmp expect output &&
  61        test ! -s output.err
  62'
  63
  64cat > expect << EOF
  65boolean: 2
  66integer: 1729
  67timestamp: 0
  68string: 321
  69abbrev: 10
  70verbose: 2
  71quiet: no
  72dry run: no
  73EOF
  74
  75test_expect_success 'long options' '
  76        test-parse-options --boolean --integer 1729 --boolean --string2=321 \
  77                --verbose --verbose --no-dry-run --abbrev=10 \
  78                > output 2> output.err &&
  79        test ! -s output.err &&
  80        test_cmp expect output
  81'
  82
  83test_expect_success 'missing required value' '
  84        test-parse-options -s;
  85        test $? = 129 &&
  86        test-parse-options --string;
  87        test $? = 129
  88'
  89
  90cat > expect << EOF
  91boolean: 1
  92integer: 13
  93timestamp: 0
  94string: 123
  95abbrev: 7
  96verbose: 0
  97quiet: no
  98dry run: no
  99arg 00: a1
 100arg 01: b1
 101arg 02: --boolean
 102EOF
 103
 104test_expect_success 'intermingled arguments' '
 105        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
 106                > output 2> output.err &&
 107        test ! -s output.err &&
 108        test_cmp expect output
 109'
 110
 111cat > expect << EOF
 112boolean: 0
 113integer: 2
 114timestamp: 0
 115string: (not set)
 116abbrev: 7
 117verbose: 0
 118quiet: no
 119dry run: no
 120EOF
 121
 122test_expect_success 'unambiguously abbreviated option' '
 123        test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
 124        test ! -s output.err &&
 125        test_cmp expect output
 126'
 127
 128test_expect_success 'unambiguously abbreviated option with "="' '
 129        test-parse-options --int=2 > output 2> output.err &&
 130        test ! -s output.err &&
 131        test_cmp expect output
 132'
 133
 134test_expect_success 'ambiguously abbreviated option' '
 135        test-parse-options --strin 123;
 136        test $? = 129
 137'
 138
 139cat > expect << EOF
 140boolean: 0
 141integer: 0
 142timestamp: 0
 143string: 123
 144abbrev: 7
 145verbose: 0
 146quiet: no
 147dry run: no
 148EOF
 149
 150test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 151        test-parse-options --st 123 > output 2> output.err &&
 152        test ! -s output.err &&
 153        test_cmp expect output
 154'
 155
 156cat > typo.err << EOF
 157error: did you mean \`--boolean\` (with two dashes ?)
 158EOF
 159
 160test_expect_success 'detect possible typos' '
 161        test_must_fail test-parse-options -boolean > output 2> output.err &&
 162        test ! -s output &&
 163        test_cmp typo.err output.err
 164'
 165
 166cat > expect <<EOF
 167boolean: 0
 168integer: 0
 169timestamp: 0
 170string: (not set)
 171abbrev: 7
 172verbose: 0
 173quiet: no
 174dry run: no
 175arg 00: --quux
 176EOF
 177
 178test_expect_success 'keep some options as arguments' '
 179        test-parse-options --quux > output 2> output.err &&
 180        test ! -s output.err &&
 181        test_cmp expect output
 182'
 183
 184cat > expect <<EOF
 185boolean: 0
 186integer: 0
 187timestamp: 1
 188string: default
 189abbrev: 7
 190verbose: 0
 191quiet: yes
 192dry run: no
 193arg 00: foo
 194EOF
 195
 196test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
 197        test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
 198                foo -q > output 2> output.err &&
 199        test ! -s output.err &&
 200        test_cmp expect output
 201'
 202
 203cat > expect <<EOF
 204Callback: "four", 0
 205boolean: 5
 206integer: 4
 207timestamp: 0
 208string: (not set)
 209abbrev: 7
 210verbose: 0
 211quiet: no
 212dry run: no
 213EOF
 214
 215test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
 216        test-parse-options --length=four -b -4 > output 2> output.err &&
 217        test ! -s output.err &&
 218        test_cmp expect output
 219'
 220
 221cat > expect <<EOF
 222Callback: "not set", 1
 223EOF
 224
 225test_expect_success 'OPT_CALLBACK() and callback errors work' '
 226        test_must_fail test-parse-options --no-length > output 2> output.err &&
 227        test_cmp expect output &&
 228        test_cmp expect.err output.err
 229'
 230
 231cat > expect <<EOF
 232boolean: 1
 233integer: 23
 234timestamp: 0
 235string: (not set)
 236abbrev: 7
 237verbose: 0
 238quiet: no
 239dry run: no
 240EOF
 241
 242test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
 243        test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
 244        test ! -s output.err &&
 245        test_cmp expect output
 246'
 247
 248# --or4
 249# --no-or4
 250
 251test_done