简体   繁体   English

用于使用命令行选项的ruby习语

[英]ruby idioms for using command-line options

I'm trying to pick up ruby by porting a medium-sized (non-OO) perl program. 我试图通过移植一个中型(非OO)perl程序来获取ruby。 One of my personal idioms is to set options like this: 我个人的习惯之一是设置这样的选项:

use Getopt::Std;
our $opt_v;  # be verbose
getopts('v');
# and later ...
$opt_v && print "something interesting\n";

In perl, I kind of grit my teeth and let $opt_v be (effectively) a global. 在perl中,我有点咬牙切齿,让$ opt_v(有效地)成为全球性的。

In ruby,the more-or-less exact equivalent would be 在红宝石中,或多或少精确的等价物

require 'optparse'
    opts.on("-v", "--[no-]verbose", TrueClass, "Run verbosely") {
        |$opt_verbose|
    }
    opts.parse!
end

where $opt_verbose is a global that classes could access. 其中$ opt_verbose是类可以访问的全局。 Having classes know about global flags like that seems ... er ... wrong. 让课程知道这样的全球旗帜似乎......呃......错了。 What's the OO-idiomatic way of doing this? OO惯用的方法是什么?

  • Let the main routine take care of all option-related stuff and have the classes just return things to it that it decides how to deal with? 让主程序处理所有与选项相关的东西,并让类只返回它决定如何处理它的东西?
  • Have classes implement optional behaviour (eg, know how to be verbose) and set a mode via an attr_writer sort of thing? 让类实现可选行为(例如,知道如何详细)并通过attr_writer类型设置模式?

updated: Thanks for the answers suggesting optparse, but I should have been clearer that it's not how to process command-line options I'm asking about, but more the relationship between command-line options that effectively set a global program state and classes that should ideally be independent of that sort of thing. 更新:感谢建议optparse的答案,但我应该更清楚的是,它不是如何处理我正在询问的命令行选项,而是更多关于有效设置全局程序状态的命令行选项与类之间的关系理想情况下应该独立于那种事情。

A while back I ran across this blog post (by Todd Werth) which presented a rather lengthy skeleton for command-line scripts in Ruby. 前段时间我遇到了这篇博客文章 (由Todd Werth撰写),它为Ruby中的命令行脚本提供了一个相当冗长的框架。 His skeleton uses a hybrid approach in which the application code is encapsulated in an application class which is instantiated, then executed by calling a "run" method on the application object. 他的骨架使用混合方法,其中应用程序代码封装在实例化的应用程序类中,然后通过在应用程序对象上调用“run”方法来执行。 This allowed the options to be stored in a class-wide instance variable so that all methods in the application object can access them without exposing them to any other objects that might be used in the script. 这允许将选项存储在类范围的实例变量中,以便应用程序对象中的所有方法都可以访问它们,而不会将它们暴露给可能在脚本中使用的任何其他对象。

I would lean toward using this technique, where the options are contained in one object and use either attr_writers or option parameters on method calls to pass relevant options to any additional objects. 我倾向于使用这种技术,其中选项包含在一个对象中,并在方法调用上使用attr_writers或option参数将相关选项传递给任何其他对象。 This way, any code contained in external classes can be isolated from the options themselves -- no need to worry about the naming of the variables in the main routine from within the thingy class if your options are set with a thingy.verbose=true attr_writer or thingy.process(true) call. 这样,外部类中包含的任何代码都可以与选项本身隔离 - 如果使用thingy.verbose=true attr_writer设置选项,则无需担心在thingy类中主例程中变量的命名。或者thingy.process(true)调用。

The optparse library is part of the standard distribution, so you'll be able to use it without requiring any third party stuff. optparse库是标准发行版的一部分,因此您无需任何第三方内容即可使用它。

I haven't used it personally, but rails seems to use it extensively and so does rspec , which I guess is a pretty solid vote of confidence 我没有亲自使用它,但rails似乎广泛使用它所以rspec也是如此 ,我猜这是一个相当坚定的信任投票

This example from rails' script/console seems to show how to use it pretty easily and nicely rails的script/console这个例子似乎很容易和很好地展示了如何使用它

关于“ 处理红宝石中的命令行选项 ”谷歌的第一个热门话题是一篇关于Trollop的文章,它似乎是这项工作的好工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM