Shellcheck: getopts: detect option letters handled in case statement but missing from optstring

Created on 17 May 2017  路  4Comments  路  Source: koalaman/shellcheck

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/sh

usage() {
        echo "$0 [-a text] [-b]"
        exit 1
}

while getopts a: opt; do
        case "$opt" in
                a) foo=$OPTARG ;;
                b) alias bar=true ;;
                *) usage ;;
        esac
 done

echo "$foo"

Here's what shellcheck currently says:

No issues detected!

Here's what I wanted or expected to see:

Something along the lines of Attempting to handle option "b" not present in getopts' optstring.

Most helpful comment

This feature has been added in 4243c6a. It only works for getopts (not getopt) in strictly canonical while loops like this one.

Great suggestion. It's high frequency so it'll actually trigger, high precision so it won't be noisy, and high level so you'll be impressed when it's caught ^^

All 4 comments

This feature has been added in 4243c6a. It only works for getopts (not getopt) in strictly canonical while loops like this one.

Great suggestion. It's high frequency so it'll actually trigger, high precision so it won't be noisy, and high level so you'll be impressed when it's caught ^^

Incorrect commit link.
I think this is the correct one https://github.com/koalaman/shellcheck/commit/070a465b64f07600

Oops! Yes, that's the one

Awesome, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

erwinkramer picture erwinkramer  路  5Comments

tdmalone picture tdmalone  路  3Comments

balloonpopper picture balloonpopper  路  4Comments

quchen picture quchen  路  3Comments

szepeviktor picture szepeviktor  路  4Comments