ruby 选项解析应该在哪里?
我想知道在使用传统的(bin/、lib/、test/)文件夹层次结构创建 gem 时,在 ruby 中进行选项解析的最佳位置在哪里。
具体来说,应该在 (bin/) 中的可执行文件中还是在 lib/ 中的运行程序类中解析选项?为什么?
I'm wondering where would be the best place to do option parsing in ruby, when creating a gem, with the traditional (bin/, lib/, test/) folder hierarchy.
Specifically, should options be parsed in the executable file in (bin/) or in a runner class in lib/? And why?
我见过两种方式都这样做。就我个人而言,我认为这很大程度上取决于您的宝石;如果选项解析对于二进制文件是唯一的,请将其放入二进制文件中,并将必要的选项传递给您的库。 ruby-debug 的二进制文件 就是一个很好的例子。但是,如果您的参数与您的类相关,例如像 Heroku 中那样传递命令
Command
类,随意解析库中的选项。I've seen it done both ways. Personally, I think much of it depends on your gem; if the option parsing is unique to the binary, put it in the binary, and pass the necessary options to your library. ruby-debug's binary is a good example. However, if your arguments are tied to your classes, such as passing around commands as in Heroku's
Command
class, feel free to parse the options in your library.