Skip to content

Add optional positional parameter #55

Closed
ruby/ruby
#9901

Description

@bkuhlmann

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

Activity

  1. added 5 commits that reference this issue on Feb 9, 2024
    b093e03
    e6be9b7
    5db0663
    9d53e74
    2c6767b
  2. added a commit that references this issue on Aug 11, 2024
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