Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "getopts" bugs and bad interactions with "shift"



On Nov 23,  6:22pm, Adam Spiers wrote:
> Subject: Re: "getopts" bugs and bad interactions with "shift"
> Bart Schaefer (schaefer@xxxxxxxxxxxxxxxxxxxxxxx) wrote:
> > Question:  Is "getopts" based on some standard or emulated behavior of
> > some other shell, or is it strictly a zsh thing?
> 
> getopt is part of GNU libc [...]

In case it wasn't obvious from Zefram's post, "getopts" != "getopt".

The GNU "getopt" command (short-args-only form) is very roughly equivalent
to

    function getopt () {
	optstr=":$1"; shift
	got=()
	ret=0
	while getopts "$optstr" opt
        do
	    if [[ $opt == [?] ]]
	    then
		echo getopt: invalid option -- "$OPTARG" 1>&2
		ret=1
	    else
		got=("$got[@]" "-$opt" ${OPTARG:+"$OPTARG"})
	    fi
	done
	shift $OPTIND	# $((OPTIND-1)) if 3.0.7 or before
	echo "" "$got[@]" -- "$@"
	return $ret
    }



Messages sorted by: Reverse Date, Date, Thread, Author