英文:
User specified errors with getopts colon feature
问题
I want the ability to specify whether to use Normal Error Reporting
or Silent Error Reporting
provided with getopts
.
然后我可以像这样调用我的bash函数
mari -s -n VAL -z VAL
-s
将设置静默错误报告(即设置 opstring=":n:z:"
),否则将使用普通错误报告(opstring="n:z:"
)。
目前我正在函数内部硬编码 opstring
。
mari()
{
impl=""
if [[ "$impl" == "SILENT" ]]; then
opstring=":n:z:" # Short options string
else
opstring="n:z:"
fi
while getopts "$opstring" opname; do
case ${opname} in
("n")
if [ -n "$OPTARG" ]; then
echo "The string is not empty"
else
echo "The string is empty"
fi
;;
("z")
if [ -z "$OPTARG" ]; then
echo "The string is empty"
else
echo "The string is not empty"
fi
;;
(?)
## Invalid Option Found, OPNAME set to '?'
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
(:)
## Required option argument not found, OPNAME set to ':'
echo "Option -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
}
英文:
I want the ability to specify whether to use Normal Error Reporting
or Silent Error Reporting
provided with getopts
.
Then I would be able to call my bash function like this
mari -s -n VAL -z VAL
The -s
would set Silent Error Reporting (i.e. set opstring=":n:z:"
), otherwise Normal Error Reporting (opstring="n:z:"
) will be used.
Currently I am hardwiring opstring
inside the function.
mari()
{
impl=""
if [[ "$impl" == "SILENT" ]]; then
opstring=":n:z:" # Short options string
else
opstring="n:z:"
fi
while getopts "$opstring" opname; do
case ${opname} in
("n")
if [ -n "$OPTARG" ]; then
echo "The string is not empty"
else
echo "The string is empty"
fi
;;
("z")
if [ -z "$OPTARG" ]; then
echo "The string is empty"
else
echo "The string is not empty"
fi
;;
(?)
## Invalid Option Found, OPNAME set to '?'
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
(:)
## Required option argument not found, OPNAME set to ':'
echo "Option -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
}
答案1
得分: 1
修改opstring
一旦遇到s
。
?
是通配符,它匹配任何内容。你需要引用它。
mari() {
local opstring
opstring="sn:z:"
while getopts "$opstring" opname; do
case ${opname} in
"s")
opstring=":${opstring##:}"
;;
"n")
if [ -n "$OPTARG" ]; then
echo "字符串不为空"
else
echo "字符串为空"
fi
;;
"z")
if [ -z "$OPTARG" ]; then
echo "字符串为空"
else
echo "字符串不为空"
fi
;;
'?')
## 无效选项,将OPNAME设置为'?'。
echo "无效选项: -$OPTARG" 1>&2
exit 1
;;
':')
## 所需的选项参数未找到,将OPNAME设置为':'。
echo "选项 -$OPTARG 需要一个参数" 1>&2
exit 1
;;
esac
done
}
echo "没有无效选项:"
mari -s -bla
echo "无效选项:"
mari -bla
英文:
Modify the opstring once s
is encountered.
?
is a glob, it matches anything. You have to quote it.
mari() {
local opstring
opstring="sn:z:"
while getopts "$opstring" opname; do
case ${opname} in
"s")
opstring=":${opstring##:}"
;;
"n")
if [ -n "$OPTARG" ]; then
echo "The string is not empty"
else
echo "The string is empty"
fi
;;
"z")
if [ -z "$OPTARG" ]; then
echo "The string is empty"
else
echo "The string is not empty"
fi
;;
'?')
## Invalid Option Found, OPNAME set to '?'
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
':')
## Required option argument not found, OPNAME set to ':'
echo "Option -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
}
echo "no invalid option:"
mari -s -bla
echo "invalid option:"
mari -bla
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论