Pry 中的 ls 命令如何接受 -l 作为参数?

joh*_*nja 2 ruby pry pry-rails

我最近发现ls在 pry 中可以采用这样的参数:ls -l。

我最初的问题是该-l部分实际上是什么 - 它显然不是字符串或符号,并且没有l定义局部变量或方法,那么幕后是否还有其他事情发生?

作为我的问题的扩展,只是lspry 定义的“正常”Ruby 方法,还是它的行为略有不同?

我还注意到,如果传递字符串 ( ls 'l') 或符号 ( ls :l),您会得到不同的输出。是否有可能选项的完整参考?

Hat*_*es_ 5

传递-l工作是因为该方法将整行计算为字符串pry_eval。从这里开始,它将开头与现有命令进行匹配,并提取其余部分作为要传入的选项。来自 Pry 文档:

Pry 会话中的几乎所有功能都是作为命令实现的。命令不是方法,必须从行首开始,中间没有空格。命令支持灵活的语法并允许以与 shell 命令相同的方式使用“选项”

您可以通过运行查看完整的选项列表ls -h。这将返回:

-m, --methods               Show public methods defined on the Object (default)
-M, --instance-methods      Show methods defined in a Module or Class
-p, --ppp                   Show public, protected (in yellow) and private (in green) methods
-q, --quiet                 Show only methods defined on object.singleton_class and object.class
-v, --verbose               Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)
-g, --globals               Show global variables, including those builtin to Ruby (in cyan)
-l, --locals                Show locals, including those provided by Pry (in red)
-c, --constants             Show constants, highlighting classes (in blue), and exceptions (in purple)
-i, --ivars                 Show instance variables (in blue) and class variables (in bright blue)
-G, --grep                  Filter output by regular expression
-h, --help                  Show this message.
Run Code Online (Sandbox Code Playgroud)