gpt4 book ai didi

linux - Erlang 脚本中的大小写表达式

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:06 26 4
gpt4 key购买 nike

我正在测试我的 erlang 脚本,脚本的内容是这样的:

#!/usr/bin/env escript
main([Action]) ->
case Action of
start -> start();
stop -> stop()
end;
main(_) ->
usage().

usage() ->
io:format("usage: factorial integer\n"),
halt(1).

start() -> io:format("Start.~n").
stop() -> io:format("Stop.~n").

但是当我尝试运行脚本时,我遇到了这个问题:

./escript start
escript: exception error: no case clause matching "start"

是什么导致了这个问题?参数格式有误吗?

最佳答案

命令行参数作为字符串传递给 main/1,而不是原子,所以你需要匹配 "start""stop",不是开始停止:

...
main([Action]) ->
case Action of
"start" -> start();
"stop" -> stop()
end;
...

除非你的实际代码更复杂/不同,否则你也可以在函数子句中直接匹配["start"]["stop"]:

main(["start"]) -> start();
main(["stop"]) -> stop();
main(_) ->
usage().

这也会为 ./escript foo 调用 usage(),同时您的原始脚本会崩溃,这可能是您想要的,也可能不是您想要的。

关于linux - Erlang 脚本中的大小写表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com