bmitc

bmitc

How to pass strings containing commas to a script using `mix run`?

Apologies if I am missing something silly, but I am unable to figure this out. I am trying to run an Elixir script using mix run, and I want to pass a single arbitrary string to it.

A sample script, say scripts/string_parser.exs looks like this:

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

This example only contains the relevant parts. Here’s some sample runs of the script:

> mix run scripts/string_parser.exs '{:ok}'
["{:ok}"]

> mix run scripts/string_parser.exs '{:ok,:test}' 
["{:ok", ":test}"]

> mix run scripts/string_parser.exs '{:ok\,:test}'
["{:ok\" :test}"]

> mix run scripts/string_parser.exs "{:ok,:test}" 
["{:ok", ":test}"]

> mix run scripts/string_parser.exs "{:ok\,:test}"
["{:ok\" :test}"]

How do I pass a string that contains a , as a command line argument to mix run <script>?

Note: This issue does not occur for running a script with elixir <script> <arguments>. For example,

> elixir scripts/string_parser.exs '{:ok, :test}'  
["{:ok, :test}"]

and all the other variations work as expected.

This either seems like a bug or I’m missing something.

Most Liked

cmo

cmo

@bmitc are you on Windows by any chance? mix is mix.bat or mix.ps1 and I think you’ll find that passing args on Windows is a nightmare due to the different shells and batch scripts vs PowerShell scripts.

My determination was that it is not worth the effort trying to handle them and built an exe for my release instead of <release>.bat. The Burrito project does something funky to handle the args on Windows too.

You might have better luck doing your dev in WSL.

Last Post!

bmitc

bmitc

I am indeed on Windows. I should have mentioned that, but it actually didn’t occur to me since elixir works as expected. I am on Windows 11 using PowerShell. My Elixir version is:

> elixir --version
Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit]

Elixir 1.13.3 (compiled with Erlang/OTP 22)

I typically bounce between Windows and WSL2 and sometimes Docker for my development, depending on what I’m doing.

Does anyone know why elixir works and mix doesn’t? It doesn’t seem to be solely a Windows problem if one works as expected, although I seem to recall running into something similar to this before with mix. Since Elixir supports Windows, I think this should probably work or be documented why. It would be great to understand why it doesn’t work. I haven’t read through the elixir and mix scripts in detail on Windows.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement