bash-opts-example - Example Bash script processing short and long command-line options, including grouped short options

#!/usr/bin/env bash

help_line="Example Bash script processing short and long command-line options, including grouped short options"
web_desc_line="Example Bash script processing short and long command-line options, including grouped short options"

debug=false

usage() {
    echo "Usage: $0 [options]"
    echo "Options:"
    grep "# arg:" $0 | grep -v "grep" | sed 's/^ *//; s/).*# *arg://'
    echo "Try ${0##*/} -h for more information"
    exit 1
}

# Split combined short arguments (eg -abc) into individual arguments (-a -b -c). Remove = symbols.
new_args=()
while [[ "$1" != "" ]]; do
    if [[ $1 == -* && $1 != --* && ${#1} -gt 2 ]]; then
        for ((i=1; i<${#1}; i++)); do
            arg="${1:i:1}"
            if [[ $arg == "=" ]]; then new_args+=("${1:i+1}"); break; fi
            if [[ $arg == "d" ]]; then debug=true; else new_args+=("-$arg"); fi
        done
    else
        if [[ $1 == --*=* ]]; then new_args+=("${1%%=*}"); new_args+=("${1##*=}")
        elif [[ $1 == "-d" || $1 == "--debug" ]]; then debug=true; 
        else new_args+=("$1"); fi
    fi
    shift
done

if $debug; then
    echo "DEBUG ENABLED"
    printf "args: "; printf '%s ' "${new_args[@]}"; echo
    # set -x
fi

# Process the arguments
set -- "${new_args[@]}"
while [[ "$1" != "" ]]; do
    case "$1" in
        -v|--value) # arg: <value> Set a value.
            value="$2"
            if [[ "$value" == "" || "$value" == -* ]]; then
                echo "Error: Missing value for $1"
                usage
            fi
            shift
            ;;
        -f|--flag) # arg:          Set a flag
            flag=true
            ;;
        -*|--*)
            echo "Unknown option: $1"
            usage
            ;;
        *)
            break
            ;;
    esac
    shift
done