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