t / t0040-parse-options.shon commit revision: new rev^-n shorthand for rev^n..rev (8779351)
   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 --int=2 >output 2>output.err &&
 212        test_must_be_empty output.err &&
 213        test_cmp expect output
 214'
 215
 216test_expect_success 'ambiguously abbreviated option' '
 217        test_expect_code 129 test-parse-options --strin 123
 218'
 219
 220cat >expect <<\EOF
 221boolean: 0
 222integer: 0
 223magnitude: 0
 224timestamp: 0
 225string: 123
 226abbrev: 7
 227verbose: -1
 228quiet: 0
 229dry run: no
 230file: (not set)
 231EOF
 232
 233test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 234        test-parse-options --st 123 >output 2>output.err &&
 235        test_must_be_empty output.err &&
 236        test_cmp expect output
 237'
 238
 239cat >typo.err <<\EOF
 240error: did you mean `--boolean` (with two dashes ?)
 241EOF
 242
 243test_expect_success 'detect possible typos' '
 244        test_must_fail test-parse-options -boolean >output 2>output.err &&
 245        test_must_be_empty output &&
 246        test_cmp typo.err output.err
 247'
 248
 249cat >typo.err <<\EOF
 250error: did you mean `--ambiguous` (with two dashes ?)
 251EOF
 252
 253test_expect_success 'detect possible typos' '
 254        test_must_fail test-parse-options -ambiguous >output 2>output.err &&
 255        test_must_be_empty output &&
 256        test_cmp typo.err output.err
 257'
 258
 259cat >expect <<\EOF
 260boolean: 0
 261integer: 0
 262magnitude: 0
 263timestamp: 0
 264string: (not set)
 265abbrev: 7
 266verbose: -1
 267quiet: 0
 268dry run: no
 269file: (not set)
 270arg 00: --quux
 271EOF
 272
 273test_expect_success 'keep some options as arguments' '
 274        test-parse-options --quux >output 2>output.err &&
 275        test_must_be_empty output.err &&
 276        test_cmp expect output
 277'
 278
 279cat >expect <<\EOF
 280boolean: 0
 281integer: 0
 282magnitude: 0
 283timestamp: 1
 284string: (not set)
 285abbrev: 7
 286verbose: -1
 287quiet: 1
 288dry run: no
 289file: (not set)
 290arg 00: foo
 291EOF
 292
 293test_expect_success 'OPT_DATE() works' '
 294        test-parse-options -t "1970-01-01 00:00:01 +0000" \
 295                foo -q >output 2>output.err &&
 296        test_must_be_empty output.err &&
 297        test_cmp expect output
 298'
 299
 300cat >expect <<\EOF
 301Callback: "four", 0
 302boolean: 5
 303integer: 4
 304magnitude: 0
 305timestamp: 0
 306string: (not set)
 307abbrev: 7
 308verbose: -1
 309quiet: 0
 310dry run: no
 311file: (not set)
 312EOF
 313
 314test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
 315        test-parse-options --length=four -b -4 >output 2>output.err &&
 316        test_must_be_empty output.err &&
 317        test_cmp expect output
 318'
 319
 320>expect
 321
 322test_expect_success 'OPT_CALLBACK() and callback errors work' '
 323        test_must_fail test-parse-options --no-length >output 2>output.err &&
 324        test_i18ncmp expect output &&
 325        test_i18ncmp expect.err output.err
 326'
 327
 328cat >expect <<\EOF
 329boolean: 1
 330integer: 23
 331magnitude: 0
 332timestamp: 0
 333string: (not set)
 334abbrev: 7
 335verbose: -1
 336quiet: 0
 337dry run: no
 338file: (not set)
 339EOF
 340
 341test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
 342        test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
 343        test_must_be_empty output.err &&
 344        test_cmp expect output
 345'
 346
 347test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
 348        test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
 349        test_must_be_empty output.err &&
 350        test_cmp expect output
 351'
 352
 353cat >expect <<\EOF
 354boolean: 6
 355integer: 0
 356magnitude: 0
 357timestamp: 0
 358string: (not set)
 359abbrev: 7
 360verbose: -1
 361quiet: 0
 362dry run: no
 363file: (not set)
 364EOF
 365
 366test_expect_success 'OPT_BIT() works' '
 367        test-parse-options -bb --or4 >output 2>output.err &&
 368        test_must_be_empty output.err &&
 369        test_cmp expect output
 370'
 371
 372test_expect_success 'OPT_NEGBIT() works' '
 373        test-parse-options -bb --no-neg-or4 >output 2>output.err &&
 374        test_must_be_empty output.err &&
 375        test_cmp expect output
 376'
 377
 378test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
 379        test-parse-options + + + + + + >output 2>output.err &&
 380        test_must_be_empty output.err &&
 381        test_cmp expect output
 382'
 383
 384cat >expect <<\EOF
 385boolean: 0
 386integer: 12345
 387magnitude: 0
 388timestamp: 0
 389string: (not set)
 390abbrev: 7
 391verbose: -1
 392quiet: 0
 393dry run: no
 394file: (not set)
 395EOF
 396
 397test_expect_success 'OPT_NUMBER_CALLBACK() works' '
 398        test-parse-options -12345 >output 2>output.err &&
 399        test_must_be_empty output.err &&
 400        test_cmp expect output
 401'
 402
 403cat >expect <<\EOF
 404boolean: 0
 405integer: 0
 406magnitude: 0
 407timestamp: 0
 408string: (not set)
 409abbrev: 7
 410verbose: -1
 411quiet: 0
 412dry run: no
 413file: (not set)
 414EOF
 415
 416test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
 417        test-parse-options --no-ambig >output 2>output.err &&
 418        test_must_be_empty output.err &&
 419        test_cmp expect output
 420'
 421
 422cat >>expect <<\EOF
 423list: foo
 424list: bar
 425list: baz
 426EOF
 427test_expect_success '--list keeps list of strings' '
 428        test-parse-options --list foo --list=bar --list=baz >output &&
 429        test_cmp expect output
 430'
 431
 432test_expect_success '--no-list resets list' '
 433        test-parse-options --list=other --list=irrelevant --list=options \
 434                --no-list --list=foo --list=bar --list=baz >output &&
 435        test_cmp expect output
 436'
 437
 438cat >expect <<\EOF
 439boolean: 0
 440integer: 0
 441magnitude: 0
 442timestamp: 0
 443string: (not set)
 444abbrev: 7
 445verbose: -1
 446quiet: 3
 447dry run: no
 448file: (not set)
 449EOF
 450
 451test_expect_success 'multiple quiet levels' '
 452        test-parse-options -q -q -q >output 2>output.err &&
 453        test_must_be_empty output.err &&
 454        test_cmp expect output
 455'
 456
 457cat >expect <<\EOF
 458boolean: 0
 459integer: 0
 460magnitude: 0
 461timestamp: 0
 462string: (not set)
 463abbrev: 7
 464verbose: 3
 465quiet: 0
 466dry run: no
 467file: (not set)
 468EOF
 469
 470test_expect_success 'multiple verbose levels' '
 471        test-parse-options -v -v -v >output 2>output.err &&
 472        test_must_be_empty output.err &&
 473        test_cmp expect output
 474'
 475
 476cat >expect <<\EOF
 477boolean: 0
 478integer: 0
 479magnitude: 0
 480timestamp: 0
 481string: (not set)
 482abbrev: 7
 483verbose: -1
 484quiet: 0
 485dry run: no
 486file: (not set)
 487EOF
 488
 489test_expect_success '--no-quiet sets --quiet to 0' '
 490        test-parse-options --no-quiet >output 2>output.err &&
 491        test_must_be_empty output.err &&
 492        test_cmp expect output
 493'
 494
 495cat >expect <<\EOF
 496boolean: 0
 497integer: 0
 498magnitude: 0
 499timestamp: 0
 500string: (not set)
 501abbrev: 7
 502verbose: -1
 503quiet: 0
 504dry run: no
 505file: (not set)
 506EOF
 507
 508test_expect_success '--no-quiet resets multiple -q to 0' '
 509        test-parse-options -q -q -q --no-quiet >output 2>output.err &&
 510        test_must_be_empty output.err &&
 511        test_cmp expect output
 512'
 513
 514cat >expect <<\EOF
 515boolean: 0
 516integer: 0
 517magnitude: 0
 518timestamp: 0
 519string: (not set)
 520abbrev: 7
 521verbose: 0
 522quiet: 0
 523dry run: no
 524file: (not set)
 525EOF
 526
 527test_expect_success '--no-verbose sets verbose to 0' '
 528        test-parse-options --no-verbose >output 2>output.err &&
 529        test_must_be_empty output.err &&
 530        test_cmp expect output
 531'
 532
 533cat >expect <<\EOF
 534boolean: 0
 535integer: 0
 536magnitude: 0
 537timestamp: 0
 538string: (not set)
 539abbrev: 7
 540verbose: 0
 541quiet: 0
 542dry run: no
 543file: (not set)
 544EOF
 545
 546test_expect_success '--no-verbose resets multiple verbose to 0' '
 547        test-parse-options -v -v -v --no-verbose >output 2>output.err &&
 548        test_must_be_empty output.err &&
 549        test_cmp expect output
 550'
 551
 552test_done