I would like to write code that prints out all the docstrings in a module.
For example, here is my simple module:

"doc this module"
module TestModule

"add a to b"
function fx(a,b)
    a+b
end

export fx
end

using TestModule



The following works, but is very fragile:

fnm = 0  # otherwise, get error
nms = names(TestModule)
for nm in nms
    fnm = eval(nm)
    docnm = @doc(fnm)
    println(stringmime("text/plain",docnm))
end


It produces

doc this module


add a to b 


Without the "fnm = 0", I get the error:

LoadError: "fnm" is not defined in module Main


If I try to wrap this loop in a function, I can get multiple behaviors.
The following is the best case, and assumes that I already ran the
previous block of code:


function printDocs(mod)
    nms = names(mod)

    for nm in nms
        fnm = eval(nm)
        docnm = @doc(fnm)
        println(stringmime("text/plain",docnm))
    end    
end
printDocs(TestModule) 


produces


add a to b


add a to b


(it lost the module, and turned it into fx)

If I run the function without previously printing the documentation, I get

LoadError: "fnm" is not defined in module Main 


So, I try

fnm = 0
printDocs(TestModule) 


Which gives me the lovely documentation for zero:


0 (zero; BrE: `/ˈzɪərəʊ/` or AmE: `/ˈziːroʊ/`) is both a number and the 
numerical digit used to represent that number in numerals. It fulfills a 
central role in mathematics as the additive identity of the integers, real 
numbers, and many other algebraic structures. As a digit, 0 is used as a 
placeholder in place value systems. Names for the number 0 in English 
include zero, nought or (US) naught (`/ˈnɔːt/`), nil, or — in contexts 
where at least one adjacent digit distinguishes it from the letter "O" — oh 
or o (`/ˈoʊ/`). Informal or slang terms for zero include zilch and zip. 
Ought and aught (/ˈɔːt/), as well as cipher, have also been used 
historically. 



If anyone can explain this, it would be very helpful.
I am using Julia 0.4.0 on a Mac running OS 10.11

Thanks,
  Dan
 

Reply via email to