Having said all of that that, something like this might be useful when writing custom Mix tasks inside Elixir projects!
Having said all of that that, something like this might be useful when writing custom Mix tasks inside Elixir projects!
Things of interest for CLI stuff:
#!/usr/bin/env elixir
Use Mix.install like so Mix.install([
{:jason, "~> 1.4"},
])
Execute shell commands {output, status_code} = System.cmd("ls", ["-la"])
For handling files and paths: Path.join/2
__ENV__.file
Path.dirname/1
Path.expand/1
File.lstat/1
File.read/1
Parsing arguments argv = System.argv()
OptionParser.parse(argv, [strict: [switch: :boolean], aliases: [s: :switch]])
exit({:shutdown, status_code}) # for shutting down with an error
For printing colors (from docs) iex> IO.ANSI.format(["Hello, ", :red, :bright, "world!"], true)why? what factors lead you to this conclusion?
there's so mamy examples of low grade opinion sharing in the comments. going just a little further to say why you think something, what factoes are good or bad, can provide a real starting point where folks can learn & engage usefully, rather than just subject each other to utterly shallow opinion. another example from elsewhere:
> Elixir is ill-suited to command line apps out of the box.
we should have actual substance to our conversations; again, why?
(my own guess is that beam vm has to get started & is mind of heavyweight... i dunno if that cost even necessarily has to be paid for each program launch or no, maybe there's some trickery to quickly start new cli processes in existing vms. just guessing, not my scene. but what would be negative factors? no one says.)
I can think of three languages that I would use based on that: Go, Rust and if I was feeling adventurous or wanted to be able to execute scripts, Common Lisp (it's surprisingly fast, in the same league as Rust, for small programs).
Does Elixir compare positively to those in these categories?
EDIT: some people on this thread say it takes 200ms just to start an Elixir program. That's three times slower than Java, which is generally not considered a good language for CLIs.
So a web interface, an API interface, a CLI interface, etc are all treated the same way since the code doing the work isn’t tightly integrated to any of them.
From that perspective, Elixir can be great for CLI.
The equivalent approach in most other languages would be to create a single web API and have everything talk to it via REST, etc.
It’s closer to thinking of everything having an RPC interface by default I suppose.
The BEAM VM is unique (at least compared to mainstream languages) in that everything is run as a preempted, stackful coroutine. The result is an environment reminiscent of an operating system.
There are a number of applications(Erlang term) that start by default that may not be required, which can be disabled.
Unless you're writing a CLI utility that natively interacts with BEAM VM services without having to de/serialize JSON via a REST API first
For complex applications bash (or shellscript in general) is also rather ill-suited as it has a lot of footguns and can be hard to maintain.
These days I'd rather suffer through the verbosity of Go or strict memory semantics of Rust (which are no concern in most CLI applications) than write more than a dozen lines of Python. Personally, while I think the compiled world has settled onto a few very good languages, the scripting ecosystem still to this day leaves a lot to be desired.
https://engineering.fb.com/2022/07/27/developer-tools/progra...