# How to access function documentation in a script

**URL:** <https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 12, 2020, 9:38pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466 "2020-04-12T21:38:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [April 12, 2020, 9:38pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/1 "2020-04-12T21:38:33Z")

</div>

Is there a way to access the documentation string associated with a function that is part of a module?

Consider this example:

```julia
module Test

export f

"""
    f(x)

Return `x` squared.
"""
function f(x)
    return x^2
end

end # module Test

using .Test

function getDocString(g) # I would like g::Union{Symbol, String}
    return @doc g # unsure what to put here
end

println(getDocString(f))
# note that `println(@doc f)` does work, but it's not what I want.

```

Running this as a script gives the error:

```julia
No documentation found.

Binding `g` does not exist.

```

Is there a way to write a function that takes a function argument (represented as a symbol/string) and returns its docstring? I’m probably not understanding how modules relate docstrings to their corresponding functions. I know that I can use `@doc f` to access function documentation, but I’d like to use symbols/strings to get this result as well. `@doc` is great to use in the REPL. However, I’m looking for a more script-oriented approach. 😄

As a clarifying example, say I generated the symbol `:f` and I know there’s a function defined in `Test` with that name. How would I get `f`’s docstring with only a symbol?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 12, 2020, 10:16pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/2 "2020-04-12T22:16:26Z")

</div>

From the macro expansion it looks like you just need `Docs.doc(g)`.

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [April 12, 2020, 10:28pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/3 "2020-04-12T22:28:54Z")

</div>

Thanks for the quick response, @Vasily_Pisarev!

Are you suggesting something like this?

```julia
function getDocString(g::Symbol)
    return Docs.doc(g)
end

println(getDocString(g))

```

If so, this still gives an error. I may be misunderstanding your response. Also, I’d like to get the docstring from a **symbol** (`:g`), not `g`. I hope this clarifies. It’s certainly a weird question. 😉

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 12, 2020, 10:33pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/4 "2020-04-12T22:33:09Z")

</div>

```julia
function getDocString(g::Symbol)
    return Docs.doc(Docs.Binding(Main, g))
end

```

?  
(I’m just copy-pasting `@macroexpand @doc g` output 😉)

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [April 12, 2020, 10:38pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/5 "2020-04-12T22:38:25Z")

</div>

Unfortunately, this doesn’t work for my example above. See the output below:

```julia
#...
using .Test

function getDocString(g::Symbol)
    # return Docs.doc(Docs.Binding(Test, g)) # Also doesn't work
    return Docs.doc(Docs.Binding(Main, g))
end

println(getDocString(:g))

```

Using this in the example script above, I get the error:

```julia
No documentation found.

Binding `g` does not exist.

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 12, 2020, 10:44pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/6 "2020-04-12T22:44:32Z")

</div>

But is there a binding for `:g` in `Test`? From the snippet in OP, the only binding in `Test` is `:f`.

---

<div class="post-metadata">

**Author:** ![jonathan-durbin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan-durbin/32/11468_2.png) [@jonathan-durbin](https://discourse.julialang.org/u/jonathan-durbin)\
**Post date:** [April 12, 2020, 10:45pm UTC](https://discourse.julialang.org/t/how-to-access-function-documentation-in-a-script/37466/7 "2020-04-12T22:45:57Z")

</div>

🤦‍♂️

Thanks a lot! Maybe I need to eat something. 😆
