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