Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
diff tests/common.t @ 803:2cd233b137ec
common.subr: Implement postprocess_getopts_for_long() to process long options in combination with the Shell's internal getopts()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 04 Nov 2024 11:08:15 +0100 |
| parents | |
| children | 6cf3e1021862 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/common.t Mon Nov 04 11:08:15 2024 +0100 @@ -0,0 +1,316 @@ +Basic tests of common.subr + +Shell is /bin/sh. + + +Setup +===== + + $ set -u + $ . "${TESTDIR}/testsetup.sh" + $ _p_datadir="${TESTDIR}/../share/local-bsdtools" + $ . "${_p_datadir}/common.subr" + + +Getopts +======= + +Normal successfull calls + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts "ab:-:" opt --long-a --long-b=value-of-b arg ; do + > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b=" "" --long-b=value-of-b arg + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > \?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + + $ checkyesno opt_a + $ checkyesno opt_b + $ [ "${opt_b_val}" = "value-of-b" ] + $ [ -z "${error}" ] + +Not null-terminated long options speccifications + + $ OPTIND=1 + > getopts "ab:-:" opt --long-a --long-b arg + > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b" + /bin/sh: ERROR: Missing null terminator for long option specifications + [1] + $ [ "${opt}" = '?' ] + $ [ -z "${OPTARG+SET}" ] + +Not null-terminated long options specs (alternate error handling) + + $ OPTIND=1 + > getopts ":ab:-:" opt --long-a --long-b arg + > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b" + [1] + $ [ "${opt}" = ':' ] + $ [ -n "${OPTARG+SET}" ] + $ [ -z "${OPTARG}" ] + +Illegal long option + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts "ab:-:" opt --long-unknown ; do + > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b=" "" --long-unknown + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > \?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + /bin/sh: ERROR: Illegal option --long-unknown + $ [ "${error}" = "foo" ] + +Illegal long option (alternate error handling) + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts ":ab:-:" opt --long-unknown ; do + > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b=" "" --long-unknown + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > \?) + > error="${OPTARG}" + > ;; + > *) + > error=bar + > ;; + > esac + > done + $ [ "${error}" = "long-unknown" ] + +Missing required option argument + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts "ab:-:" opt --long-a --long-b arg ; do + > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b=" "" --long-b arg + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > ?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + /bin/sh: ERROR: option argument required for option --long-b + $ [ "${error}" = "foo" ] + +Missing required option argument (alternate error handling) + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts ":ab:-:" opt --long-a --long-b arg ; do + > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b=" "" --long-b arg + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > :) + > error="${OPTARG}" + > ;; + > *) + > error=bar + > ;; + > esac + > done + $ [ "${error}" = "long-b" ] + +Option argument value not allowed + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts "ab:-:" opt --long-a=a-value arg ; do + > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b=" "" --long-a=a-value arg + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > ?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + /bin/sh: ERROR: no option argument allowed for option --long-a + $ [ "${error}" = "foo" ] + +Missing required option argument (alternate error handling) + + $ error='' + > opt_a=no + > opt_b=no + > unset opt_b_val + > OPTIND=1 + > while getopts ":ab:-:" opt --long-a=a-value arg ; do + > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b=" "" --long-a=a-value arg + > case "$opt" in + > a|long-a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > b|long-b) opt_b=yes + > [ -n "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > opt_b_val="${OPTARG}" + > ;; + > :) + > error="${OPTARG}" + > ;; + > *) + > error=bar + > ;; + > esac + > done + $ [ "${error}" = "long-a" ] + +Invalid character in long option + + $ error='' + > opt_a=no + > OPTIND=1 + > while getopts "ab:-:" opt --long=a ; do + > postprocess_getopts_for_long "ab:-:" opt "long=a" "" --long=a arg + > case "$opt" in + > a|long=a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > \?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=a + $ [ "${error}" = "foo" ] + +Invalid character in long option (alternate error handling) + + $ error='' + > opt_a=no + > OPTIND=1 + > while getopts ":a-:" opt --long=a ; do + > postprocess_getopts_for_long ":a-:" opt "long=a" "" --long=a arg + > case "$opt" in + > a|long=a) opt_a=yes + > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" + > ;; + > \?) + > error="${OPTARG}" + > ;; + > *) + > error=bar + > ;; + > esac + > done + $ [ "${error}" = "long=a" ] + +Invalid character in long option + + $ error='' + > opt_b=no + > OPTIND=1 + > while getopts "b:-:" opt --long=a ; do + > postprocess_getopts_for_long "b:-:" opt "long=b=" "" --long=b=value arg + > case "$opt" in + > b|long=b) opt_b=yes + > ;; + > \?) + > error=foo + > ;; + > *) + > error=bar + > ;; + > esac + > done + /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=b= + $ [ "${error}" = "foo" ] + +Invalid character in long option (alternate error handling) + + $ error='' + > opt_b=no + > OPTIND=1 + > while getopts ":b:-:" opt --long=a ; do + > postprocess_getopts_for_long ":b:-:" opt "long=b=" "" --long=b=value arg + > case "$opt" in + > b|long=b) opt_b=yes + > ;; + > \?) + > error="${OPTARG}" + > ;; + > *) + > error=bar + > ;; + > esac + > done + $ [ "${error}" = "long=b=" ]
