简体   繁体   English

Elixir-带参数的混合运行示例

[英]Elixir - Mix run example with arguments

Elixir is powered by Erlang OTP, which uses Mix as a build tool for creating and running applications. Elixir由Erlang OTP支持,后者使用Mix作为构建工具来创建和运行应用程序。

I am now learning elixir as a beginner 我现在作为初学者正在学习长生不老药

I can create a mix application from command line by specifying mix new sample and I write some code in this to learn the basics of elixir and mix. 我可以通过指定新的混合示例从命令行创建混合应用程序,并在其中编写一些代码以了解e剂和混合的基础知识。

exs and ex file could be run by using the command mix run sample.exs 可以使用命令mix run sample.exs运行exsex文件

I am trying to write a code to get a specific type of number ,say prime numbers between a particular range (100000,20000) 我正在尝试编写代码以获取特定类型的数字,例如在特定范围(100000,20000)之间的质数

I want to give the two numbers (100000,200000) as arguments to the mix run command like mix run sample.exs 100000,200000 and get the result in the given range. 我想将两个数字(100000,200000)作为混合运行命令的参数,例如mix run sample.exs 100000,200000,并在给定范围内得到结果。

Note - I dont want to build and executable using escript and need to use only the mix run command and also not mix run -e 注意-我不想使用escript进行构建和执行,并且只需要使用mix run命令,而不要使用run -e

How to get the args as input values in the exs file ? 如何在exs文件中将args作为输入值?

Thanks a lot 非常感谢

While System.argv/0 somewhat works in trivial cases, we usually use OptionParser for more or less sophisticated options processing. 尽管System.argv/0在平凡的情况下可以正常工作,但我们通常使用OptionParser进行或多或少的复杂选项处理。

["--start", "0", "--end", "42"]
|> OptionParser.parse(
  strict: [start: :integer, end: :integer]
)
|> IO.inspect()
#⇒ {[start: 0, end: 42], [], []}

To use these values: 要使用这些值:

{args, _, _} =
  OptionParser.parse(
    ["--start", "0", "--end", "42"],
    strict: [start: :integer, end: :integer]
  )
Enum.each(args[:start]..args[:end], &IO.inspect/1)

In order to get the arguments that were passed to your program, you need to use System.argv() . 为了获取传递给程序的参数,您需要使用System.argv() For example, given the following exs script: 例如,给定以下exs脚本:

args = System.argv()  
       |> IO.inspect()

and running elixir so_exs.exs input_1 input_2 (note I don't have a Mix project here so I am not using mix run ) you get the following. 并运行elixir so_exs.exs input_1 input_2 (请注意,我在这里没有Mix项目,因此我没有使用mix run ),您将获得以下信息。 The args variable now contains a list of all the arguments passed to the program: 现在, args变量包含传递给程序的所有参数的列表:

["input_1", "input_2"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM