From a1c06a3938ce239a8af399f75d1b4aa67cec0e0d Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 9 Oct 2021 11:49:23 -0700 Subject: [PATCH] Updated SC2213 (markdown) --- SC2213.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/SC2213.md b/SC2213.md index c6d34bf..79a237e 100644 --- a/SC2213.md +++ b/SC2213.md @@ -34,4 +34,26 @@ Either add a case to handle the flag, or remove it from the `getopts` option str ### Exceptions: -None. \ No newline at end of file +ShellCheck may not correctly recognize less canonical uses of `while getopts ..; do case ..;`, such as when modifying the variable before using it: + +``` +while getopts "rf-:" OPT; do + if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG + OPT="${OPTARG%%=*}" # extract long option name + OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty) + OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` + fi + + case "$OPT" in + r) ... ;; + f) ... ;; + my-long-option) ... ;; + esac +done +``` + +In such cases you can do one of: + +* [[Ignore]] the warning. +* Use the external tool `getopt` (no "s") which supports long options natively. +* Rewrite to not modify the variable first, in this case by instead doing it in a `-)` branch. \ No newline at end of file