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

Support mutually exclusive options #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gustavothecoder
Copy link

Resolve #128

This PR adds support to mutually exclusive options (i.e. options that cannot be used together). The implemented API is exemplified bellow. I'm not totally satisfied with the current API, so any suggestions will be appreciated.

require "dry/cli"

class Greetings < Dry::CLI::Command
  mutually_exclusive_options [
    [:english, {desc: "Chooses English", type: :flag}],
    [:spanish, {desc: "Chooses Spanish", type: :flag}],
    [:portuguese, {desc: "Chooses Portuguese", type: :flag}]
  ] 

  def call(options)
    if options.key?(:english)
      puts "Good morning"
    elsif options.key?(:spanish)
      puts "Buenos días"
    elsif options.key?(:portuguese)
      puts "Bom dia"
    end
  end
end

# $ foo greetings --english
# Good morning

# $ foo greetings --english --spanish
# ERROR: "foo greetings" was called with arguments "--english --spanish"

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

Successfully merging this pull request may close these issues.

Mutually-exclusive arguments or options
1 participant