t / t0040-parse-options.shon commit Merge branch 'mb/reword-autocomplete-message' (aca226e)
   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    --yes                 get a boolean
  14    -D, --no-doubt        begins with 'no-'
  15    -B, --no-fear         be brave
  16    -b, --boolean         increment by one
  17    -4, --or4             bitwise-or boolean with ...0100
  18    --neg-or4             same as --no-or4
  19
  20    -i, --integer <n>     get a integer
  21    -j <n>                get a integer, too
  22    -m, --magnitude <n>   get a magnitude
  23    --set23               set integer to 23
  24    -t <time>             get timestamp of <time>
  25    -L, --length <str>    get length of <str>
  26    -F, --file <file>     set file to <file>
  27
  28String options
  29    -s, --string <string>
  30                          get a string
  31    --string2 <str>       get another string
  32    --st <st>             get another string (pervert ordering)
  33    -o <str>              get another string
  34    --list <str>          add str to list
  35
  36Magic arguments
  37    --quux                means --quux
  38    -NUM                  set integer to NUM
  39    +                     same as -b
  40    --ambiguous           positive ambiguity
  41    --no-ambiguous        negative ambiguity
  42
  43Standard options
  44    --abbrev[=<n>]        use <n> digits to display SHA-1s
  45    -v, --verbose         be verbose
  46    -n, --dry-run         dry run
  47    -q, --quiet           be quiet
  48    --expect <string>     expected output in the variable dump
  49
  50EOF
  51
  52test_expect_success 'test help' '
  53        test_must_fail test-parse-options -h >output 2>output.err &&
  54        test_must_be_empty output.err &&
  55        test_i18ncmp expect output
  56'
  57
  58mv expect expect.err
  59
  60check () {
  61        what="$1" &&
  62        shift &&
  63        expect="$1" &&
  64        shift &&
  65        test-parse-options --expect="$what $expect" "$@"
  66}
  67
  68check_unknown_i18n() {
  69        case "$1" in
  70        --*)
  71                echo error: unknown option \`${1#--}\' >expect ;;
  72        -*)
  73                echo error: unknown switch \`${1#-}\' >expect ;;
  74        esac &&
  75        cat expect.err >>expect &&
  76        test_must_fail test-parse-options $* >output 2>output.err &&
  77        test_must_be_empty output &&
  78        test_i18ncmp expect output.err
  79}
  80
  81test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
  82test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
  83test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
  84test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
  85test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
  86
  87test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
  88test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
  89
  90test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
  91test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
  92
  93test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
  94test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
  95
  96test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
  97
  98test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
  99
 100test_expect_success 'OPT_MAGNITUDE() simple' '
 101        check magnitude: 2345678 -m 2345678
 102'
 103
 104test_expect_success 'OPT_MAGNITUDE() kilo' '
 105        check magnitude: 239616 -m 234k
 106'
 107
 108test_expect_success 'OPT_MAGNITUDE() mega' '
 109        check magnitude: 104857600 -m 100m
 110'
 111
 112test_expect_success 'OPT_MAGNITUDE() giga' '
 113        check magnitude: 1073741824 -m 1g
 114'
 115
 116test_expect_success 'OPT_MAGNITUDE() 3giga' '
 117        check magnitude: 3221225472 -m 3g
 118'
 119
 120cat >expect <<\EOF
 121boolean: 2
 122integer: 1729
 123magnitude: 16384
 124timestamp: 0
 125string: 123
 126abbrev: 7
 127verbose: 2
 128quiet: 0
 129dry run: yes
 130file: prefix/my.file
 131EOF
 132
 133test_expect_success 'short options' '
 134        test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
 135        >output 2>output.err &&
 136        test_cmp expect output &&
 137        test_must_be_empty output.err
 138'
 139
 140cat >expect <<\EOF
 141boolean: 2
 142integer: 1729
 143magnitude: 16384
 144timestamp: 0
 145string: 321
 146abbrev: 10
 147verbose: 2
 148quiet: 0
 149dry run: no
 150file: prefix/fi.le
 151EOF
 152
 153test_expect_success 'long options' '
 154        test-parse-options --boolean --integer 1729 --magnitude 16k \
 155                --boolean --string2=321 --verbose --verbose --no-dry-run \
 156                --abbrev=10 --file fi.le --obsolete \
 157                >output 2>output.err &&
 158        test_must_be_empty output.err &&
 159        test_cmp expect output
 160'
 161
 162test_expect_success 'missing required value' '
 163        test_expect_code 129 test-parse-options -s &&
 164        test_expect_code 129 test-parse-options --string &&
 165        test_expect_code 129 test-parse-options --file
 166'
 167
 168cat >expect <<\EOF
 169boolean: 1
 170integer: 13
 171magnitude: 0
 172timestamp: 0
 173string: 123
 174abbrev: 7
 175verbose: -1
 176quiet: 0
 177dry run: no
 178file: (not set)
 179arg 00: a1
 180arg 01: b1
 181arg 02: --boolean
 182EOF
 183
 184test_expect_success 'intermingled arguments' '
 185        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
 186                >output 2>output.err &&
 187        test_must_be_empty output.err &&
 188        test_cmp expect output
 189'
 190
 191cat >expect <<\EOF
 192boolean: 0
 193integer: 2
 194magnitude: 0
 195timestamp: 0
 196string: (not set)
 197abbrev: 7
 198verbose: -1
 199quiet: 0
 200dry run: no
 201file: (not set)
 202EOF
 203
 204test_expect_success 'unambiguously abbreviated option' '
 205        test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
 206        test_must_be_empty output.err &&
 207        test_cmp expect output
 208'
 209
 210test_expect_success 'unambiguously abbreviated option with "="' '
 211        test-parse-options --expect="integer: 2" --int=2
 212'
 213
 214test_expect_success 'ambiguously abbreviated option' '
 215        test_expect_code 129 test-parse-options --strin 123
 216'
 217
 218test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 219        test-parse-options --expect="string: 123" --st 123
 220'
 221
 222cat >typo.err <<\EOF
 223error: did you mean `--boolean` (with two dashes ?)
 224EOF
 225
 226test_expect_success 'detect possible typos' '
 227        test_must_fail test-parse-options -boolean >output 2>output.err &&
 228        test_must_be_empty output &&
 229        test_cmp typo.err output.err
 230'
 231
 232cat >typo.err <<\EOF
 233error: did you mean `--ambiguous` (with two dashes ?)
 234EOF
 235
 236test_expect_success 'detect possible typos' '
 237        test_must_fail test-parse-options -ambiguous >output 2>output.err &&
 238        test_must_be_empty output &&
 239        test_cmp typo.err output.err
 240'
 241
 242test_expect_success 'keep some options as arguments' '
 243        test-parse-options --expect="arg 00: --quux" --quux
 244'
 245
 246cat >expect <<\EOF
 247boolean: 0
 248integer: 0
 249magnitude: 0
 250timestamp: 1
 251string: (not set)
 252abbrev: 7
 253verbose: -1
 254quiet: 1
 255dry run: no
 256file: (not set)
 257arg 00: foo
 258EOF
 259
 260test_expect_success 'OPT_DATE() works' '
 261        test-parse-options -t "1970-01-01 00:00:01 +0000" \
 262                foo -q >output 2>output.err &&
 263        test_must_be_empty output.err &&
 264        test_cmp expect output
 265'
 266
 267cat >expect <<\EOF
 268Callback: "four", 0
 269boolean: 5
 270integer: 4
 271magnitude: 0
 272timestamp: 0
 273string: (not set)
 274abbrev: 7
 275verbose: -1
 276quiet: 0
 277dry run: no
 278file: (not set)
 279EOF
 280
 281test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
 282        test-parse-options --length=four -b -4 >output 2>output.err &&
 283        test_must_be_empty output.err &&
 284        test_cmp expect output
 285'
 286
 287>expect
 288
 289test_expect_success 'OPT_CALLBACK() and callback errors work' '
 290        test_must_fail test-parse-options --no-length >output 2>output.err &&
 291        test_i18ncmp expect output &&
 292        test_i18ncmp expect.err output.err
 293'
 294
 295cat >expect <<\EOF
 296boolean: 1
 297integer: 23
 298magnitude: 0
 299timestamp: 0
 300string: (not set)
 301abbrev: 7
 302verbose: -1
 303quiet: 0
 304dry run: no
 305file: (not set)
 306EOF
 307
 308test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
 309        test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
 310        test_must_be_empty output.err &&
 311        test_cmp expect output
 312'
 313
 314test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
 315        test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
 316        test_must_be_empty output.err &&
 317        test_cmp expect output
 318'
 319
 320test_expect_success 'OPT_BIT() works' '
 321        test-parse-options --expect="boolean: 6" -bb --or4
 322'
 323
 324test_expect_success 'OPT_NEGBIT() works' '
 325        test-parse-options --expect="boolean: 6" -bb --no-neg-or4
 326'
 327
 328test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
 329        test-parse-options --expect="boolean: 6" + + + + + +
 330'
 331
 332test_expect_success 'OPT_NUMBER_CALLBACK() works' '
 333        test-parse-options --expect="integer: 12345" -12345
 334'
 335
 336cat >expect <<\EOF
 337boolean: 0
 338integer: 0
 339magnitude: 0
 340timestamp: 0
 341string: (not set)
 342abbrev: 7
 343verbose: -1
 344quiet: 0
 345dry run: no
 346file: (not set)
 347EOF
 348
 349test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
 350        test-parse-options --no-ambig >output 2>output.err &&
 351        test_must_be_empty output.err &&
 352        test_cmp expect output
 353'
 354
 355cat >>expect <<\EOF
 356list: foo
 357list: bar
 358list: baz
 359EOF
 360test_expect_success '--list keeps list of strings' '
 361        test-parse-options --list foo --list=bar --list=baz >output &&
 362        test_cmp expect output
 363'
 364
 365test_expect_success '--no-list resets list' '
 366        test-parse-options --list=other --list=irrelevant --list=options \
 367                --no-list --list=foo --list=bar --list=baz >output &&
 368        test_cmp expect output
 369'
 370
 371test_expect_success 'multiple quiet levels' '
 372        test-parse-options --expect="quiet: 3" -q -q -q
 373'
 374
 375test_expect_success 'multiple verbose levels' '
 376        test-parse-options --expect="verbose: 3" -v -v -v
 377'
 378
 379test_expect_success '--no-quiet sets --quiet to 0' '
 380        test-parse-options --expect="quiet: 0" --no-quiet
 381'
 382
 383test_expect_success '--no-quiet resets multiple -q to 0' '
 384        test-parse-options --expect="quiet: 0" -q -q -q --no-quiet
 385'
 386
 387test_expect_success '--no-verbose sets verbose to 0' '
 388        test-parse-options --expect="verbose: 0" --no-verbose
 389'
 390
 391test_expect_success '--no-verbose resets multiple verbose to 0' '
 392        test-parse-options --expect="verbose: 0" -v -v -v --no-verbose
 393'
 394
 395test_done