Skip to content

Disable required check for tree and skip check_at_least_one! if disable_required_check#925

Open
JoelTowell wants to merge 2 commits into
rails:mainfrom
JoelTowell:disable-required-for-tree
Open

Disable required check for tree and skip check_at_least_one! if disable_required_check#925
JoelTowell wants to merge 2 commits into
rails:mainfrom
JoelTowell:disable-required-for-tree

Conversation

@JoelTowell
Copy link
Copy Markdown
Contributor

🌈

Problems

  1. tree command does not have the required check disabled by default. The purpose of tree is to reveal the structure of the CLI and I feel we should not need to pass required arguments, which will be redundant in this context, for it to work.
  2. check_at_least_one! runs when parsing commands for which disable_required_check has been set. I take it that at least one is a disjunctive form of required. This means that if I have set class_at_least_one :foo, :bar then my_cli help will not work as intended; we must pass either foo or bar to the help command.

Reproduction

# Gemfile
source "https://rubygems.org"

gem "thor"
# lib/my_cli.rb
require "thor"

class MyCli < Thor
  class_option :account, type: :string, required: true
  class_option :admin, type: :boolean
  class_option :user, type: :boolean
  class_at_least_one :admin, :user

  default_task :auth

  desc "auth", "Authorize the user"
  def auth
    puts "Authorizing user with role: #{role} for account: #{account}"
  end

  def self.exit_on_failure?
    true
  end

  private

  def role
    if options[:admin]
      "admin"
    elsif options[:user]
      "user"
    end
  end

  def account
    options[:account]
  end
end

MyCli.start if __FILE__ == $PROGRAM_NAME
$ bundle install

Problem One

$ bundle exec ruby lib/my_cli.rb tree

Output:

No value provided for required options '--account'

Problem Two

$ bundle exec ruby lib/my_cli.rb help

Output:

Not found at least one of required options '--admin', '--user'

Solution

  1. Update disable_required_check to include :tree in default array assigned to @disable_required_check
  2. Update parse to skip check_at_least_one! validation when @disable_required_check

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.

1 participant