Eiji
Is there here document and here string support in Elixir?
I have tried to write mix task or even generate escript binary file, but it looks like that none of those ways supports some useful shell input methods. I tried to inspect arguments simply with IO.inspect(args)
here-document (<< operator)
A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.
Source: Here Documents
What I have tried:
# Escript example:
$ ./example --test <<EOL
line 1
line 2
line 3
EOL
["--test"]
# Mix task example:
mix example.demo --test <<EOL
line 1
line 2
line 3
EOL
["--test"]
here-string (<<< operator)
A here string can be considered as a stripped-down form of a here document .
It consists of nothing more than COMMAND <<< $WORD ,
where $WORD is expanded and fed to the stdin of COMMAND .
Source: Here Strings
What I have tried:
# Escript example:
./example --test <<< 5*4
["--test"]
# Mix task example:
mix example.demo --test <<< 5*4
["--test"]
Maybe I need to compile Erlang with some extra flags or maybe it’s just not supported at all?
Marked As Solved
alco
I see. So the question is not about Elixir at all, it’s about passing multiple lines as a single positional argument to a program when running it in a shell.
The common practice is to use quotes like NobbZ has suggested. You could use shell substitution but that would look rather weird:
$ elixir -e "IO.inspect System.argv" -- --test $(cat <<EOL
bar
baz)
EOL
)
["--test", "bar", "baz)"]
# Must use quotes around the shell substitution
$ elixir -e "IO.inspect System.argv" -- --test "$(cat <<EOL
"bar"
baz)
EOL
)"
["--test", "\"bar\"\nbaz)"]
Also Liked
NobbZ
No program will ever see << except for the shell, which then does some magic to turn it into the started programs stdin.
This is by design.
If --test option expects further data from stdin document as such in your programs manual, if it does not, then do not try to do stdin magic.
NobbZ
Heredocs/-strings are written to stdin of your application, but from the output you show, it seems as if you only inspect parsed options.
foo <<< bar is just syntactic sugar for echo bar | foo, while << is (roughly) equivalent to echo "l1\nl2\nl3\n" | foo.
NobbZ
Last Post!
NobbZ
No, the best way is to say --test reads from stdin in its documentation or even better, adhere to the same convention that cat does. Treat the “value” as a file name and read from there, and if that “value” is -, just read from stdin.
In my opinion that output would add unnecessary noise.
Popular in Questions
Other popular topics
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









