debajit

debajit

Quickest way to call a function with command line arguments?

I have a module with a function that applies a transformation on a string e.g. (this is for an Advent of Code problem, where I need to do this often)

defmodule M do
  def f(some_string) do
    # apply transformation on some_string
  end
end

I need to run M.f with a string passed from the command line. What is the quickest way to do this?

Please note that

  • This script will never be deployed to production
  • I just need an easy way to call an Elixir function with some arguments from the command line

Option 1: escript

This is clean but requires some extra code (which I’d like to avoid if possible):

# mix.exs

defmodule M.Mixfile do
  def project do
    [
      ...,
      escript: escript(),
      default_task: "escript.build",
    ]
  end
 
  defp escript do
    [main_module: M.CLI]
  end
end
# in m.ex

defmodule M do
  ...
end

defmodule M.CLI do
  def main(args) do
    string = hd(args)
    IO.puts M.f(string)
  end
end

Option 2: mix run

This requires a little extra typing — I would like to move the IO.puts inside the code if possible. This approach avoids having to update mix.exs which is nice.

From the shell

%  mix run -e 'IO.puts M.f "some_user_string"'

Is there an easier / quicker way to do this?

Marked As Solved

NobbZ

NobbZ

Option 3: an *.exs

Just write an *.exs.

# foo.exs
defmodule M do
  def f(arg), do: IO.inspect arg
end

System.argv()
|> Enum.inspect(&M.f/1)

Then run it with elixir foo.exs print me

11
Post #2

Also Liked

debajit

debajit

Just for reference, I ended up using a slightly tweaked syntax:

# foo.exs
defmodule M do
  def f(arg) do
    "transformed #{arg}"
  end
end

[arg1, arg2] = System.argv
IO.puts M.f(arg1)

This allows pattern matching for the command line args, and it fails when an incorrect number of args is specified.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

We're in Beta

About us Mission Statement