Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate option names #139

Open
cllns opened this issue Nov 27, 2024 · 0 comments · May be fixed by #143
Open

Prevent duplicate option names #139

cllns opened this issue Nov 27, 2024 · 0 comments · May be fixed by #143

Comments

@cllns
Copy link
Member

cllns commented Nov 27, 2024

Describe the bug

dry-cli allows one to define duplicate options that override each other. However, the all show up when running --help.

While in a single class this is easy to avoid, when building many commands and sharing behavior, it's easy to accidentally include extra options. See: hanami/cli#278

To Reproduce

In test.rb:

require "dry/cli"

module Foo
  module CLI
    module Commands
      extend Dry::CLI::Registry

      class Request < Dry::CLI::Command
        option :mode, default: "http", values: %w[http http2]
        option :mode, default: "foo", values: %w[bar baz]

        def call(**options) = puts "Performing a request (mode: #{options.fetch(:mode)})"
      end

      register "request", Request
    end
  end
end

Dry::CLI.new(Foo::CLI::Commands).call

Then

# ruby test.rb request --help
Command:
  test.rb request

Usage:
  test.rb request

Options:
  --mode=VALUE                      # : (http/http2), default: "http"
  --mode=VALUE                      # : (bar/baz), default: "foo"
  --help, -h                        # Print this help

It shows both --modes. Same thing happens if they're identical, too.

Expected behavior

I would expect either:

  1. The second option to override the first one and completely remove the first one from --help (since this is what actually happens when you try to execute it with that option. only bar/baz are valid, not http/http2.
  2. An error to be raised, saying it's not allowed

My environment

Ruby 3.3, dry-cli 1.2.0 (latest as of writing this)

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

Successfully merging a pull request may close this issue.

1 participant