【问题标题】:Erlang command lineErlang 命令行
【发布时间】:2014-02-01 22:54:22
【问题描述】:

我需要将两个参数传递给我的 Erlang 代码。它在 Erlang shell 中运行良好。

2> crop:fall_velocity(x,23).
  21.23205124334434

但是我应该如何在没有 Erlang shell 的情况下运行 Erlang 代码。像普通的python,c程序一样。 ./program_name(不传递 $1 $2 参数)。

我正在尝试这个

erl -noshell -s crop fall_velocity(x,20) -s init stop

但它给出了意外的令牌错误。

【问题讨论】:

    标签: erlang erlang-shell erl


    【解决方案1】:

    作为documentation states-s 将提供的所有参数仅作为一个原子列表传递,-run 执行相同但作为字符串列表。如果你想调用任意参数数量和类型的任意函数,你应该使用-eval:

    $ erl -noshell -eval 'io:format("test\n",[]),init:stop()'
    test
    $
    

    【讨论】:

      【解决方案2】:

      您可以使用escript 从命令行运行 Erlang 脚本。在该脚本中,您应该创建一个 main 函数,该函数将参数数组作为字符串。

      #!/usr/bin/env escript
      
      main(Args) ->
        io:format("Printing arguments:~n"),
        lists:foreach(fun(Arg) -> io:format("Got argument: ~p~n", [Arg]) end,Args).
      

      输出:

      ./escripter.erl hi what is your name 5 6 7 9
      Printing arguments:
      Got argument: "hi"
      Got argument: "what"
      Got argument: "is"
      Got argument: "your"
      Got argument: "name"
      Got argument: "5"
      Got argument: "6"
      Got argument: "7"
      Got argument: "9"
      

      【讨论】:

        猜你喜欢
        • 2017-08-10
        • 2018-06-25
        • 1970-01-01
        • 2012-02-28
        • 2013-04-24
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        相关资源
        最近更新 更多