-
Notifications
You must be signed in to change notification settings - Fork 26
Closed
ruby/ruby
#9901Description
Overview
Hello. 👋 I was wondering if it'd be possible to add optional positional parameter support since it's awkward to use a control parameter (i.e. actual || fallback) in the body of an #on method when an optional positional parameter could be provided via the block arguments.
Steps to Recreate
Here's a snippet for illustration purposes:
require "optparse"
parser = OptionParser.new do |instance|
instance.on("--demo_a [TEXT]") { |value| puts value || "fallback" }
instance.on("--demo_b [TEXT]") { |value = "fallback"| puts value }
end
parser.parse ["--demo_a"] # "fallback"
parser.parse %w[--demo_a hello] # "hello"
parser.parse ["--demo_b"] # "" <- 😢
parser.parse %w[--demo_b hello] # "hello"As per the official option parser documentation, the --demo_a implementation is the recommended way to solve this problem but I'd really like to support the --demo_b implementation since it would clean up the code and is more intuitive/idiomatic.
Environment
- Ruby: ruby 3.2.2 (2023-03-30 revision e51014f9c0) +YJIT [arm64-darwin22.4.0]
- Option Parser: 0.3.1
Metadata
Metadata
Assignees
Labels
No labels