哪些字符是 GNU getopt 的有效短选项?
Which chars are valid shortopts for GNU getopt?
我想知道哪些字符是 GNU getopt 的有效短选项,它以许多不同的语言实现,例如 Bash 或 PHP。
我没有找到定义它的官方文档,或者我看多了。
我测试了以下 Bash 脚本:
#!/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
结果:
$ ./test -a%2x
-a was triggered!
-% was triggered!
-2 was triggered!
Invalid option: -x
所以我可以说至少 -% 和 -2 是有效的短选项,或者 Bash 的 getopt 实现不正确。
哪些字符是 excluded/illegal 短选项?我知道 ':' 显然被排除在外,而那个 '?'被定义为总是非法的,因此 '-?'主要用于显示使用信息页面。
POSIX 说:
All option characters allowed by Utility Syntax Guideline 3 are allowed in optstring. The implementation may accept other characters as an extension.
Guideline 3 说:
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.
反过来,alnum class定义为这些字符:
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
Linux getopt(3)
联机帮助页说:
The use of '+' and '-' in optstring is a GNU extension.
所以它也接受那些。如果其他人为你工作,我想那很好,但要注意使用未记录的行为。
我想知道哪些字符是 GNU getopt 的有效短选项,它以许多不同的语言实现,例如 Bash 或 PHP。
我没有找到定义它的官方文档,或者我看多了。
我测试了以下 Bash 脚本:
#!/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
结果:
$ ./test -a%2x
-a was triggered!
-% was triggered!
-2 was triggered!
Invalid option: -x
所以我可以说至少 -% 和 -2 是有效的短选项,或者 Bash 的 getopt 实现不正确。
哪些字符是 excluded/illegal 短选项?我知道 ':' 显然被排除在外,而那个 '?'被定义为总是非法的,因此 '-?'主要用于显示使用信息页面。
POSIX 说:
All option characters allowed by Utility Syntax Guideline 3 are allowed in optstring. The implementation may accept other characters as an extension.
Guideline 3 说:
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.
反过来,alnum class定义为这些字符:
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
Linux getopt(3)
联机帮助页说:
The use of '+' and '-' in optstring is a GNU extension.
所以它也接受那些。如果其他人为你工作,我想那很好,但要注意使用未记录的行为。