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