ruby / ruby/optparse

Default Value support

オープン
#67 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Ruby
スター
71
フォーク
27
平均マージ
3時間 3分
マージ済み PR(30日)
2

説明

Hi,

Thanks a lot for this gem!

I wrote a little hacky class that adds support for default values. So you can do:

parser.on('-m', '--tracing-mode MODE', 'Define the category of events traced', default: 'default')

I have no idea if you can find it interesting, but I use it quite often in my project, so I share it with you. Putting the default values into the into for me to duplicate the key (one into the the on and one into the into. I lead from some silly mistake due to a typo).

If you are interested, maybe I can try to do a cleaner PR. If you are not, please feel free to close this issue.

Thanks a lot!

require 'optparse'
class OptionParserWithDefaultAndValidation < OptionParser
  def initialize
    # Dictionary to save the default values passed by `on`
    @defaults = {}
    super
  end

  def parse!(argv = default_argv, into: nil)
    # Merge the default value with the `into` dict
    into.merge!(@defaults) unless into.nil?
    # This will populate the `into` dict (overwriting our default if needed)
    super
  end

  def define(*opts, &block)
    switch = super
    @defaults[switch.switch_name] = @tmp_default unless @tmp_default.nil?
    switch
  end

  def on(*opts, default, &block)
    # Save the default, which will be used in `refine.`
    @tmp_default = default
    # Default can contain Array (or Set).
    # We cannot use the [a].flatten trick.
    # So we use the respond_to one
    #   irb(main):028:0> default = Set[-1,2]
    #   => #<Set: {-1, 2}>
    #   irb(main):029:0> [default].flatten.join(',')
    #   => "#<Set: {-1, 2}>"
    #   irb(main):030:0> default.respond_to?(:flatten)? default.flatten.join(',') : default
    #   => "-1,2"
    opts << "Default: #{default.respond_to?(:flatten) ? default.flatten.join(',') : default}" unless default.nil?
    super(*opts, &block)
  end
end

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提案されたサブクラスに示されている OptionParser#on、#define、#parse! のエントリポイントから始め、既存の into 引数がどのように設定されるかを確認します。into がある場合とない場合のデフォルト値について期待される動作を確認し、その後、既存の OptionParser テストを見つけ、それらを使って完了条件を定義します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
ruby
領域
cli
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。