One of the things I find myself doing a lot is dropping into the terminal, firing up
iex and then using the help system (e.g.
h Enum.count) to get information about a routine. Then I kill the shell and go on about my day.
Is there already an elixir command line tool that prints the help for a module or function directly from the shell prompt? (starts
iex implicitly, runs help, then returns to the shell)
I am thinking of something kinda like ‘ri’ in the Ruby universe.
Did you try
h command? Something like
h Module.method. e.g.
Edit: Sorry, misunderstood the question.
Yes. I can do that once I’ve gotten into
iex of course. But I want to start from the shell prompt without having to start
I suppose you could make an escript that calls the Code.get_docs function. IEx.helpers.h is a set of fancy functions that ultimately end up calling Code.get_docs(modulename, :docs) and/or Code.get_docs(Enum, :moduledoc). You can see the relevant code from IEx here: https://github.com/elixir-lang/elixir/blob/master/lib/iex/lib/iex/introspection.ex and you can experiement with Code.get_docs from inside iex to see how that is the real powerhouse here
If you are using an editor you can use a package which will get you the help using a similar mechanism. There is https://github.com/slashmili/alchemist.vim for vim and https://github.com/tonini/alchemist.el for emacs. Or you could spin up an alchemist server (https://github.com/tonini/alchemist-server) and query it (not sure how hard it would be).
I know this is not directly an answer, but I use Dash on macOS for quickly looking up documentation. It works really really well with Elixir’s documentation.
There’s also Zeal for other OSes
You can even use Dash from within iex if you want.
This is what I use, and I have to say it is pretty awesome def recommend …
You can evaluate arbitrary Elixir commands from the shell, like so:
$ elixir -e "IO.puts 1+1"
Thus, to access documentation you can invoke the
h/1 macro from the module
$ elixir -e "require IEx.Helpers; IEx.Helpers.h(Enum.count)"
Obviously, writing the above line every time you want to look up the Elixir documentation for a module or function is very tedious, so instead you could create an executable shell script – say
exdoc – then add it to your
Note: Don’t forget to
chmod u+x on it.
if [[ -z "$1" ]]; then
echo "USAGE: $(basename $0) [name]"
elixir -e "require IEx.Helpers; IEx.Helpers.h($1)"
Elixir Docs Command
$ exdoc Enum.count
$ exdoc Kernel
I created a small escript to do this exact thing (if you’re still interested) - GitHub - silbermm/exdoc_cli: CLI for displaying exdoc