使用命令行选项的 ruby 习惯用法
我正在尝试通过移植一个中型(非 OO)perl 程序来选择 ruby。 我个人的习惯之一是这样设置选项:
use Getopt::Std;
our $opt_v; # be verbose
getopts('v');
# and later ...
$opt_v && print "something interesting\n";
在 Perl 中,我咬紧牙关让 $opt_v (有效地)成为全局变量。
在 ruby 中,或多或少完全相同的是
require 'optparse'
opts.on("-v", "--[no-]verbose", TrueClass, "Run verbosely") {
|$opt_verbose|
}
opts.parse!
end
$opt_verbose 是类可以访问的全局变量。 让类了解这样的全局标志似乎......呃......错误。 执行此操作的 OO 惯用方式是什么?
- 让主例程处理所有与选项相关的内容,并让类将其返回决定如何处理的内容?
- 类是否实现了可选行为(例如,知道如何变得冗长)并通过 attr_writer 之类的东西设置模式?
更新:感谢您建议 optparse 的答案,但我应该更清楚,我所询问的不是如何处理命令行选项,而是更多之间的关系命令行选项有效地设置全局程序状态和类,理想情况下应该独立于此类事物。
I'm trying to pick up ruby by porting a medium-sized (non-OO) perl program. 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.
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. Having classes know about global flags like that seems ... er ... wrong. What's the OO-idiomatic way of doing this?
- 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 (e.g., know how to be verbose) and set a mode via an attr_writer sort of thing?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不久前,我偶然发现了这篇博文(作者:Todd Werth),其中呈现了相当长的框架用于 Ruby 中的命令行脚本。 他的框架使用了一种混合方法,其中应用程序代码被封装在一个应用程序类中,该应用程序类被实例化,然后通过调用应用程序对象上的“run”方法来执行。 这允许将选项存储在类范围的实例变量中,以便应用程序对象中的所有方法都可以访问它们,而无需将它们暴露给脚本中可能使用的任何其他对象。
我倾向于使用这种技术,其中选项包含在一个对象中,并在方法调用上使用 attr_writers 或选项参数将相关选项传递给任何其他对象。 这样,外部类中包含的任何代码都可以与选项本身隔离——如果您的选项设置为,则无需担心
thingy
类中主例程中变量的命名thingy.verbose=true
attr_writer 或thingy.process(true)
调用。A while back I ran across this blog post (by Todd Werth) which presented a rather lengthy skeleton for command-line scripts in 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. 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. 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 athingy.verbose=true
attr_writer orthingy.process(true)
call.optparse 库是标准发行版的一部分,所以你不需要任何第三方的东西就可以使用它。
我个人没有使用过它,但是 rails 似乎广泛使用它< /a> 和 rspec 也是如此,我想这是一个相当可靠的信任票
这个例子来自 Rails 的
script/console
似乎展示了如何非常轻松且良好地使用它The optparse library is part of the standard distribution, so you'll be able to use it without requiring any third party stuff.
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
This example from rails'
script/console
seems to show how to use it pretty easily and nicelyGoogle 上的首次点击 “processing command line options in ruby”是一篇关于 Trollop 的文章,看起来成为这项工作的好工具。
The first hit on google for "processing command line options in ruby" is an article about Trollop which seems to be a good tool for this job.