getopt_simple.tests 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Simple usage cases for getopts.
  2. #
  3. # OPTIND is either not touched at all (first loop with getopts,
  4. # relying on shell startup init), or getopts state is reset
  5. # before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0".
  6. #
  7. # Each option is a separate argument (no "-abc"). This conceptually
  8. # needs only $OPTIND to hold getopts state.
  9. #
  10. # We check that loop does not stop on unknown option (sets "?"),
  11. # stops on _first_ non-option argument.
  12. (
  13. echo "*** no OPTIND, optstring:'ab' args:-a -b c"
  14. var=QWERTY
  15. while getopts "ab" var -a -b c; do
  16. echo "var:'$var' OPTIND:$OPTIND"
  17. done
  18. # unfortunately, "rc:0" is shown since while's overall exitcode is "success"
  19. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  20. # Resetting behavior =1
  21. echo "*** OPTIND=1, optstring:'ab' args:-a -b c"
  22. OPTIND=1
  23. while getopts "ab" var -a -b c; do
  24. echo "var:'$var' OPTIND:$OPTIND"
  25. done
  26. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  27. # Resetting behavior =0
  28. echo "*** OPTIND=0, optstring:'ab' args:-a -b c"
  29. OPTIND=0
  30. while getopts "ab" var -a -b c; do
  31. echo "var:'$var' OPTIND:$OPTIND"
  32. done
  33. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  34. # Resetting behavior "unset"
  35. echo "*** unset OPTIND, optstring:'ab' args:-a -b c"
  36. unset OPTIND
  37. while getopts "ab" var -a -b c; do
  38. echo "var:'$var' OPTIND:$OPTIND"
  39. done
  40. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  41. # What is the final exitcode?
  42. echo "*** optstring:'ab' args:-a -b c"
  43. unset OPTIND
  44. getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND"
  45. getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND"
  46. getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND"
  47. # Where would it stop? c or -c?
  48. echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d"
  49. unset OPTIND
  50. while getopts "ab" var -a c -c -b d; do
  51. echo "var:'$var' OPTIND:$OPTIND"
  52. done
  53. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  54. # What happens on unknown option?
  55. echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d"
  56. unset OPTIND
  57. while getopts "ab" var -a -c -b d; do
  58. echo "var:'$var' OPTIND:$OPTIND"
  59. done
  60. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  61. # ORTERR=0 suppresses error message?
  62. echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d"
  63. unset OPTIND
  64. OPTERR=0
  65. while getopts "ab" var -a -c -b d; do
  66. echo "var:'$var' OPTIND:$OPTIND"
  67. done
  68. echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
  69. ) 2>&1 \
  70. | sed -e 's/ unrecognized option: / invalid option -- /' \
  71. -e 's/ illegal option -- / invalid option -- /' \