1
votes

I am testing my erlang escript, the content of escriptis this:

#!/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").

But when I try to run the escript, I got this problem:

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

What caused this problem? Is the format of argument wrong?

1

1 Answers

1
votes

Command line arguments are passed as strings to main/1, not atoms, so you need to match "start" and "stop", not start and stop:

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

Unless your actual code is more complicated/different, you can also match ["start"] and ["stop"] in the function clause directly:

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

This will call usage() for ./escript foo as well while your original script would crash, which may or may not be what you want.