I would like to know which chars are valid shortopts for the GNU getopt, which is implemented in many different languages, like Bash or PHP.
I didn't find an official documentation where it is defined, or I have overread it.
I tested following Bash script:
#!/bin/bash
while getopts ":a%2" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
%)
echo "-% was triggered!" >&2
;;
2)
echo "-2 was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
The result:
$ ./test -a%2x
-a was triggered!
-% was triggered!
-2 was triggered!
Invalid option: -x
So I can tell that at least -% and -2 are valid short opts, or the getopt implementation of Bash is not correct.
Which characters are excluded/illegal short opts? I know that ':' is excluded, obviously, and that '?' is defined as always illegal, therefore '-?' is mostly used to show an usage information page.
POSIX says:
All option characters allowed by Utility Syntax Guideline 3 are allowed in optstring. The implementation may accept other characters as an extension.
Guideline 3 says:
Each option name should be a single alphanumeric character (the alnum character classification) from the portable character set. The -W (capital-W) option shall be reserved for vendor options. Multi-digit options should not be allowed.
In turn, the alnum class is defined as these characters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9
The Linux getopt(3)
manpage says:
The use of '+' and '-' in optstring is a GNU extension.
So it accepts those as well. If others work for you, I guess that's good, but beware of making use of undocumented behaviour.