我有一个 Bourne Shell 脚本,其中包含多个函数,并允许通过以下方式调用:
my.sh <func_name> <param1> <param2>
在内部,func_name()
将使用param1
and调用param2
。
我想创建一个help
只列出所有可用函数的函数,即使没有参数。
问题:如何从脚本内部获取脚本中所有函数名称的列表?
我想避免解析它并寻找函数模式。太容易出错了。
更新:代码。希望我的help()
功能像main()
- 添加到代码中的功能会自动添加到帮助中。
#!/bin/sh
# must work with "set -e"
foo ()
{
echo foo: -$1-$2-$3-
return 0
}
# only runs if there are parameters
# exits
main ()
{
local cmd="$1"
shift
local rc=0
$cmd "$@" || rc=$?
exit $rc
}
if [[ "$*" ]]
then
main "$@"
die "how did we get here?"
fi