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'classOptionParserWithDefaultAndValidation < OptionParserdefinitialize# Dictionary to save the default values passed by `on`@defaults={}superenddefparse!(argv=default_argv,into: nil)# Merge the default value with the `into` dictinto.merge!(@defaults)unlessinto.nil?# This will populate the `into` dict (overwriting our default if needed)superenddefdefine(*opts, &block)switch=super@defaults[switch.switch_name]=@tmp_defaultunless@tmp_default.nil?switchenddefon(*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}"unlessdefault.nil?super(*opts, &block)endend
Hi,
Thanks a lot for this gem!
I wrote a little hacky class that adds support for
defaultvalues. So you can do: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
intofor me to duplicate thekey(one into the theonand one into theinto.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!