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