Module 朱莉娅:列出模块中的docstring

Module 朱莉娅:列出模块中的docstring,module,julia,docstring,Module,Julia,Docstring,是否有一种方法可以从以下模块main中提取所有文档字符串,包括位于模块main中的嵌套模块的文档字符串,而不知道模块main中有哪些模块 "This is Module main" module main "This is fA()" fA(x) = x "This is module nested" module nested "This is fB()" fB(x) = x end end 我可以获取模块main和模块nested的文档字符串,假设我已经知道模块nested存在于

是否有一种方法可以从以下模块
main
中提取所有文档字符串,包括位于模块
main
中的嵌套模块
的文档字符串,而不知道模块
main
中有哪些模块

"This is Module main"
module main

"This is fA()"
fA(x) = x

"This is module nested"
module nested

"This is fB()"
fB(x) = x

end

end
我可以获取模块
main
和模块
nested
的文档字符串,假设我已经知道模块
nested
存在于模块
main

? main
This is Module main
? main.nested
This is Module nested
我熟悉
names()
,这很有帮助,但在最简单的咒语中没有这么多:

names(main, all = true)
7-element Array{Symbol,1}:
 Symbol("##meta#252")
 Symbol("#eval")     
 Symbol("#fA")       
 :eval               
 :fA                 
 :main               
 :nested             

解释
names()
的输出并不容易:显然
nested
main

中的一个模块。问题并不完全清楚,但这里有一些有用的代码:

# check if a symbol is a nested module
issubmodule(m::Module, s::Symbol) = isa(eval(m,s),Module) && module_name(m) != s

# get a list of all submodules of module m
submodules(m) = filter(x->issubmodule(m,x),names(m,true))

# get a list of all docstring bindings in a module
allbindings(m) = [ [y[2].data[:binding] for y in x[2].docs]
  for x in eval(m,Base.Docs.META)]

# recursively get all docstrings from a module and submodules
function alldocs(m)
    thedocs = map(x->Base.Docs.doc(x[1]),allbindings(m))
    return vcat(thedocs,map(x->alldocs(eval(m,x)),submodules(m))...)
end
现在你有:

julia> alldocs(main)
  This is fA()
  This is Module main
  This is module nested
  This is fB()

希望这有帮助。

isdefined(main,:fB)
返回
false
,这不是非常直观,除非
isdefined
仅用于检查嵌套模块的周围,而不是其中的内部。考虑到问题并不完全清楚,谢谢!这些东西还能用吗<代码>?main.nested
现在返回
错误:UndefVarError:NestedModule未定义