https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-05-27T22:56:45ZRuby Issue Tracking SystemRuby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=526612015-05-27T22:56:45Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/52661/diff?detail_id=37964">diff</a>)</li><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li><li><strong>Assignee</strong> set to <i>nobu (Nobuyoshi Nakada)</i></li></ul><p>Very interesting idea.</p>
<p>This is not acceptable as-is though, because of the design that an <code>OptionParser</code> instance should not be modified (and may be frozen) at parsing.</p>
<p>Currently, you can get a hash by using <code>parse_in_order</code> directly, the keys will be strings however.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">config</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">opts</span><span class="p">.</span><span class="nf">__send__</span><span class="p">(</span><span class="ss">:parse_in_order</span><span class="p">,</span> <span class="sx">%w(--host localhost --port 8000 --verbose)</span><span class="p">,</span> <span class="n">config</span><span class="p">.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:[]=</span><span class="p">))</span>
<span class="nb">p</span> <span class="n">config</span> <span class="c1">#=> {"host"=>"localhost", "port"=>8000, "verbose"=>true}</span>
</code></pre>
<p>What you want is a method like this?</p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=526692015-05-28T14:58:50Zinjekt (Lee Jarvis)ljjarvis@gmail.com
<ul></ul><blockquote>
<p>This is not acceptable as-is though, because of the design that an <code>OptionParser</code> instance should not be modified (and may be frozen) at parsing.</p>
</blockquote>
<p>Yes good point.</p>
<blockquote>
<p>Currently, you can get a hash by using <code>parse_in_order</code> directly</p>
</blockquote>
<p>I noticed that too. This functionality is not exposed though, and I think it should be.</p>
<blockquote>
<p>the keys will be strings however</p>
</blockquote>
<p>I think having strings is better than nothing, but I do think symbols would be better.</p>
<blockquote>
<p>What you want is a method like this?</p>
</blockquote>
<p>Yes exactly. This will remove much of the boilerplate I mentioned above. However, the functionality should be exposed (I'm not sure if your code was suggesting that this is a documentable feature or just a workaround). This is made a little more difficult with the design that an <code>OptionParser</code> instance should not be modified at parse time. Do you have any other ideas for a workaround?</p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=526712015-05-28T17:57:35Zzzak (zzak _)
<ul></ul><blockquote>
<p>I think having strings is better than nothing, but I do think symbols would be better.</p>
</blockquote>
<p>You want symbols, I'm guessing for kwargs?</p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=526892015-05-29T12:00:06Zinjekt (Lee Jarvis)ljjarvis@gmail.com
<ul></ul><blockquote>
<p>You want symbols, I'm guessing for kwargs?</p>
</blockquote>
<p>Sure, but not necessarily just for that. It's very common to see these config hashes contain symbol keys, so no reason to not provide them as such.</p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=527092015-06-02T01:26:17Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Lee Jarvis wrote:</p>
<blockquote>
<p>Yes exactly. This will remove much of the boilerplate I mentioned above. However, the functionality should be exposed (I'm not sure if your code was suggesting that this is a documentable feature or just a workaround). This is made a little more difficult with the design that an <code>OptionParser</code> instance should not be modified at parse time. Do you have any other ideas for a workaround?</p>
</blockquote>
<p>An idea is to add a keyword argument to <code>OptionParser#parse</code> and so on.</p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=538422015-08-17T13:46:14Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p><a href="https://github.com/nobu/ruby/tree/feature/11191-optparse-into" class="external">https://github.com/nobu/ruby/tree/feature/11191-optparse-into</a></p> Ruby master - Feature #11191: Add #to_h method to OptionParserhttps://bugs.ruby-lang.org/issues/11191?journal_id=559772016-01-06T08:23:00Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul><p>Applied in changeset r53444.</p>
<hr>
<p>optparse.rb: into kwdarg</p>
<ul>
<li>lib/optparse.rb (OptionParser#order!): add <code>into</code> optional<br>
keyword argument to store the results. [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add #to_h method to OptionParser (Closed)" href="https://bugs.ruby-lang.org/issues/11191">#11191</a>]</li>
</ul>