<p>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:</p>

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

<p>In perl, I kind of grit my teeth and let $opt_v be (effectively) a global. </p>

<p>In ruby,the more-or-less exact equivalent would be </p>

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

<p>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?</p>

<ul>
<li>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?</li>
<li>Have classes implement optional behaviour (e.g., know how to be verbose) and set a mode via an attr_writer sort of thing?</li>
</ul>

<p><em>updated:</em> Thanks for the answers suggesting optparse, but I should have been clearer that it's not <em>how</em> 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.</p>

[{'answer_id': 38741, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 1, 'selected': False, 'text': '<p>The <a href="http://www.rubyinside.com/trollop-command-line-option-parser-for-ruby-944.html" rel="nofollow noreferrer">first hit</a> on google for "<a href="http://www.google.com/search?q=processing%20command%20line%20options%20in%20ruby&amp;sourceid=mozilla2&amp;ie=utf-8&amp;oe=utf-8" rel="nofollow noreferrer">processing command line options in ruby</a>" is an article about <a href="http://trollop.rubyforge.org/" rel="nofollow noreferrer">Trollop</a> which seems to be a good tool for this job.</p>\n'}, {'answer_id': 38761, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html" rel="nofollow noreferrer">optparse</a> library is part of the standard distribution, so you\'ll be able to use it without requiring any third party stuff.</p>\n\n<p>I haven\'t used it personally, but <a href="http://github.com/rails/rails/search?q=optparse&amp;choice=code" rel="nofollow noreferrer">rails seems to use it extensively</a> and <a href="http://github.com/dchelimsky/rspec/tree/master/lib/spec/runner/option_parser.rb" rel="nofollow noreferrer">so does rspec</a>, which I guess is a pretty solid vote of confidence</p>\n\n<p><a href="http://github.com/rails/rails/tree/master/railties/lib/commands/console.rb" rel="nofollow noreferrer">This example from rails\' <code>script/console</code></a> seems to show how to use it pretty easily and nicely</p>\n'}, {'answer_id': 41033, 'author': 'Nathan Fritz', 'author_id': 4142, 'author_profile': 'https://Stackoverflow.com/users/4142', 'pm_score': 3, 'selected': True, 'text': '<p>A while back I ran across <a href="http://blog.toddwerth.com/entries/5" rel="nofollow noreferrer">this blog post</a> (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.</p>\n\n<p>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 <code>thingy</code> class if your options are set with a <code>thingy.verbose=true</code> attr_writer or <code>thingy.process(true)</code> call.</p>\n'}]
['https://Stackoverflow.com/questions/38713', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3979/']