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
Also Liked
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.
4
Popular in Questions
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
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
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










