Command Line shortcut to Elixir's help

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.

4 Likes

Did you try h command? Something like h Module.method. e.g. h Enum.take.
Edit: Sorry, misunderstood the question.

1 Like

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 iex explicitly.

1 Like

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 :slight_smile:

2 Likes

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).

1 Like

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

2 Likes

You can even use Dash from within iex if you want.

https://github.com/philosophers-stone/ehelper

1 Like

This is what I use, and I have to say it is pretty awesome :slight_smile: def recommend …

1 Like

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 IEx.Helpers:

$ 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 PATH.

Example Script

Note: Don’t forget to chmod u+x on it.

#!/bin/bash

if [[ -z "$1" ]]; then
    echo "USAGE: $(basename $0) [name]"
    exit 1
fi

elixir -e "require IEx.Helpers; IEx.Helpers.h($1)"

Elixir Docs Command

$ exdoc Enum.count
$ exdoc Kernel
... etc

Cheers!

9 Likes

I created a small escript to do this exact thing (if you’re still interested) - GitHub - silbermm/exdoc_cli: CLI for displaying exdoc

2 Likes