Default Value support
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Ruby
- Star
- 71
- Fork
- 27
- Merge trung bình
- 3 giờ 3 phút
- Pull request đã merge (30 ngày)
- 2
Mô tả
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
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với các điểm vào OptionParser#on, #define và #parse! được nêu trong lớp con được đề xuất, rồi xem xét cách đối số into hiện có được điền. Xác nhận hành vi mong đợi đối với các giá trị mặc định khi có và không có into, sau đó tìm các bài kiểm thử OptionParser hiện có và sử dụng chúng để xác định tiêu chí hoàn thành.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- ruby
- Lĩnh vực
- cli
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 35/100