To add to the other comments . . . the version of wc
that I have handy seems to handle its options like this:
#!/bin/bash
options=()
files=()
while (( $# > 0 )) ; do
if [[ "$1" = --help || "$1" = --version ]] ; then
wc "$1" # print help-message or version-message
exit
elif [[ "${1:0:1}" = - ]] ; then
while getopts cmlLw opt ; do
if [[ "$opt" = '?' ]] ; then
wc "$1" # print error-message
exit
fi
options+="$opt"
done
shift $((OPTIND-1))
OPTIND=1
else
files+="$1"
shift
fi
done
wc "${options[@]}" "${files[@]}"
(The above could be refined further, by using a separate variable for each of the five possible options, to highlight the fact that wc
doesn't care about the order its options appear in, and doesn't care if a given option appears multiple times.)