![Unix 系统中的多字符选项](https://linux22.com/image/221916/Unix%20%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84%E5%A4%9A%E5%AD%97%E7%AC%A6%E9%80%89%E9%A1%B9.png)
我有一个用例,其中我需要支持多个字符选项。目前我正在使用 getopts 的单字符选项。多字符选项是可取的。执行此操作的最佳方法是什么?我遇到过为这个用例实现手动解析器的文章,但这对于性能来说是最佳的吗?
我希望像 -ab 这样的东西仅被视为 -ab 而不是 -a -b 。这也是一种良好的编码习惯吗?
这样做只是为了使选项更有意义,而不是单个字符选项,后者不提供有关该选项的完整信息。
重要提示:我还想要带有这些多字符选项的 optargs 。示例 -ab "sdfd" 。
这是代码
while getopts ":s:p:q:j:o" opt; do
case ${opt} in
s)
only_save=TRUE
new_tok=$OPTARG
;;
p)
only_upload_enum_json=TRUE
enum="job_status"
new_tok=$OPTARG
;;
q)
only_upload_enum_json=TRUE
enum="lifecycle_state"
new_tok=$OPTARG
;;
j)
only_download_enum_json=TRUE
enum=$OPTARG
;;
o)
only_download=TRUE
;;
\?)
echo " -s <token>"
echo " -p <token>"
echo " -q <token>"
echo " -j <enum_name>"
echo " -o <no value needed>"
exit;
;;
esac
done
这里,对于 job_status ,最好使用 -js 而不是 -p 来使其更有意义。对于lifecycle_state 也是如此
答案1
就我个人而言,我在名为 的函数中使用getopt
,而不是,请参阅下面的 shell-script-template 。getopts
option_handling
如果您想通过选项传递参数,您可以:
假设您的选项是 -z 'something'...
[...]
--options hVvz:
[...]
-z)args="$2"; shift2;;
你想看一下/usr/share/doc/util-linux/examples/getopt-parse.bash
——它应该是预安装的。
这是模板:
#!/usr/bin/env -S bash -
#===============================================================================
#
# FILE: <<filename here>>
#
# USAGE:
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR:
# ORGANIZATION:
# CREATED:
# LICENSE: BSD-3-CLAUSE
# REVISION: ---
#===============================================================================
#=== Init ======================================================================
set -o nounset # exit on unset variables.
set -o errexit # exit on any error.
set -o errtrace # any trap on ERR is inherited
#set -o xtrace # show expanded command before execution.
unalias -a # avoid rm being aliased to rm -rf and similar issues
LANG=C # avoid locale issues
VERBOSE= # Don't be verbose, unless given '-v'-option
ScriptVersion="1.0"
trap "cleanup" EXIT SIGTERM
#=== Functions =================================================================
usage (){
echo "
Usage : ${0##/*/} [options] [--]
Options:
-h|--help Display this message
-V|--version Display script version
-v|--verbose Print informational text
"
exit 0
} # ---------- end of function usage ----------
option_handling () {
# see /usr/share/doc/util-linux/examples/getopt-parse.bash
OPTS=$(getopt --name "$0" \
--options hVv \
--longoptions help,version,verbose \
--shell bash \
-- "$@") \
|| (echo; echo "See above and try \"$0 --help\""; echo ; exit 1)
eval set -- "$OPTS"
unset OPTS
while true ; do
case "$1" in
-h|--help)
usage
;;
-V|--version)
echo "$0 -- Version $ScriptVersion"; exit 0
;;
-v|--verbose)
VERBOSE=true
shift
;;
--)
shift ; break
;;
*)
echo "I don't know what to do with \"$1\". Try $0 --help"
exit 1
;;
esac
done
} # ---------- end of function option_handling ----------
cleanup () { # Will be called by the trap above, no need to call it manually.
:
} # ---------- end of function cleanup ----------
# see https://github.com/markgraf/flatten.sh about this
. ~/scripting/library.bash/lazy.lib
#=== Main ======================================================================
main () {
option_handling "$@"
} # ---------- end of function main ----------
main "$@"
#=== End =======================================================================