Skip to content

Default Value support #67

Open
Open

Description

@TApplencourt

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

Activity

  1. nobu commented on Apr 15, 2024

    @nobu
    Member

    I think this can conflict with #55.
    What do you think?

  2. TApplencourt commented on Apr 15, 2024

    @TApplencourt
    Author

    Thanks for the link! Sorry that I didn't find it before. It indeed solves the same problem.

    The options: alternative improve the help message.
    instance.on("--demo_b [TEXT]", default: "fallback" will generate the error message:

    Usage: test [options]
            --demo_b [TEXT]              Default value: fallback
    

    I'm not sure if it's possible to do the same with #55 approach.

    A corned case can be if the developer modifies the default value in the block. instance.on("--demo_b [TEXT]") { |value = 0| value+1 }. I think it makes sense to print default : 0 in the help message and not default : 1. I don't know how to implement it with #55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions