t / t0040-parse-options.shon commit parse-options: new option type to treat an option-like parameter as an argument. (580d5bf)
   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    -i, --integer <n>     get a integer
  15    -j <n>                get a integer, too
  16
  17string options
  18    -s, --string <string>
  19                          get a string
  20    --string2 <str>       get another string
  21    --st <st>             get another string (pervert ordering)
  22    -o <str>              get another string
  23
  24magic arguments
  25    --quux                means --quux
  26
  27EOF
  28
  29test_expect_success 'test help' '
  30        ! test-parse-options -h > output 2> output.err &&
  31        test ! -s output &&
  32        git diff expect.err output.err
  33'
  34
  35cat > expect << EOF
  36boolean: 2
  37integer: 1729
  38string: 123
  39EOF
  40
  41test_expect_success 'short options' '
  42        test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
  43        git diff expect output &&
  44        test ! -s output.err
  45'
  46cat > expect << EOF
  47boolean: 2
  48integer: 1729
  49string: 321
  50EOF
  51
  52test_expect_success 'long options' '
  53        test-parse-options --boolean --integer 1729 --boolean --string2=321 \
  54                > output 2> output.err &&
  55        test ! -s output.err &&
  56        git diff expect output
  57'
  58
  59cat > expect << EOF
  60boolean: 1
  61integer: 13
  62string: 123
  63arg 00: a1
  64arg 01: b1
  65arg 02: --boolean
  66EOF
  67
  68test_expect_success 'intermingled arguments' '
  69        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
  70                > output 2> output.err &&
  71        test ! -s output.err &&
  72        git diff expect output
  73'
  74
  75cat > expect << EOF
  76boolean: 0
  77integer: 2
  78string: (not set)
  79EOF
  80
  81test_expect_success 'unambiguously abbreviated option' '
  82        test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
  83        test ! -s output.err &&
  84        git diff expect output
  85'
  86
  87test_expect_success 'unambiguously abbreviated option with "="' '
  88        test-parse-options --int=2 > output 2> output.err &&
  89        test ! -s output.err &&
  90        git diff expect output
  91'
  92
  93test_expect_success 'ambiguously abbreviated option' '
  94        test-parse-options --strin 123;
  95        test $? = 129
  96'
  97
  98cat > expect << EOF
  99boolean: 0
 100integer: 0
 101string: 123
 102EOF
 103
 104test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 105        test-parse-options --st 123 > output 2> output.err &&
 106        test ! -s output.err &&
 107        git diff expect output
 108'
 109
 110cat > expect.err << EOF
 111error: did you mean \`--boolean\` (with two dashes ?)
 112EOF
 113
 114test_expect_success 'detect possible typos' '
 115        ! test-parse-options -boolean > output 2> output.err &&
 116        test ! -s output &&
 117        git diff expect.err output.err
 118'
 119
 120cat > expect <<EOF
 121boolean: 0
 122integer: 0
 123string: (not set)
 124arg 00: --quux
 125EOF
 126
 127test_expect_success 'keep some options as arguments' '
 128        test-parse-options --quux > output 2> output.err &&
 129        test ! -s output.err &&
 130        git diff expect output
 131'
 132
 133test_done