Skip to content

Add optional positional parameter #55

@bkuhlmann

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

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