Default Value support
まだ誰も着手していません。
評価
調査の方向性
提案されたサブクラスに示されている OptionParser#on、#define、#parse! のエントリポイントから始め、既存の into 引数がどのように設定されるかを確認します。into がある場合とない場合のデフォルト値について期待される動作を確認し、その後、既存の OptionParser テストを見つけ、それらを使って完了条件を定義します。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Ruby
- スター
- 71
- フォーク
- 27
- 平均マージ
- 3時間 3分
- マージ済み PR(30日)
- 2
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ruby/optparse のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
documentation
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
バグ
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
voxpupuli/puppet-epel#186 · コメント 1 件 ·
-
external_created_at is no longer used for the message timestamp since the new message UI (v4.4.0) オープンBug Frontend
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100