Problem: You want to support –really-long-option-names in your bash script and getopts won’t do it.
More problems: You don’t want to use getopt because you want cross platform support or your platform isn’t supported.
More problems than that: I can’t help with existential issues.
Solution: Rewrite the long options into short options and then use getopts.
./args.sh -b -c -a “yada yada” –some-other-gnu –long-gnu-option
content of args.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/bin/bash for arg do delim="" case "$arg" in #translate --gnu-long-options to -g (short options) --some-other-gnu) args="${args}-g ";; --long-gnu-option) args="${args}-l ";; #pass through anything else *) [[ "${arg:0:1}" == "-" ]] || delim="\"" args="${args}${delim}${arg}${delim} ";; esac done #Reset the positional parameters to the short options eval set -- $args while getopts ":a:gl" option 2>/dev/null do case $option in g) echo "some other gnu option";; l) echo "long gnu option";; a) echo ${OPTARG[@]};; *) echo $OPTARG is an unrecognized option;; esac done |
Output:
b is an unrecognized option
c is an unrecognized option
yada yada
some other gnu option
long gnu option
In case you still care: The bash builtin getopts function can be used to parse long options by putting a dash character followed by a colon into the optspec. I posted an example script here:
http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/7680682#7680682