Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9131317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:07:24+00:00 2026-06-17T08:07:24+00:00

With Thor one can use method_option to set the options for a particular task.

  • 0

With Thor one can use method_option to set the options for a particular task. To set the options for all tasks in a class one can use class_option. But what about the case where one wants some tasks of a class, but not all, to share options?

In the following task1 and task2 shares options but they do not share all options and they share no options with task3.

require 'thor'

class Cli < Thor
  desc 'task1', 'Task 1'
  method_option :type, :type => :string, :required => true, :default => 'foo'
  def task1
  end

  desc 'task2', 'Task 2'
  method_option :type, :type => :string, :required => true, :default => 'foo'
  method_option :value, :type => :numeric
  def task2
  end

  desc 'task3', 'Task 3'
  method_option :verbose, :type => :boolean, :aliases => '-v'
  def task3
  end
end

Cli.start(ARGV)

The problem with stating method_option :type, :type => :string, :required => true, :default => 'foo' for both task1 and task2 is that it violates the DRY principle. Is there an idiomatic way of handling this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:07:25+00:00Added an answer on June 17, 2026 at 8:07 am

    method_option is defined in thor.rb and it takes the following parameters according to the documentation:

    • name<Symbol>:: The name of the argument.
    • options<Hash>:: Described below.

    Knowing this you can store the parameters to method_option in an array and expand that array into separate parameters as method_option is called.

    require 'thor'
    
    class Cli < Thor
      shared_options = [:type, {:type => :string, :required => true, :default => 'foo'}]
    
      desc 'task1', 'Task 1'
      method_option *shared_options
      def task1
      end
    
      desc 'task2', 'Task 2'
      method_option *shared_options
      method_option :value, :type => :numeric
      def task2
      end
    
      desc 'task3', 'Task 3'
      method_option :verbose, :type => :boolean, :aliases => '-v'
      def task3
      end
    end
    
    Cli.start(ARGV)
    

    I have no idea if this is idiomatic and I do not think it is that elegant. Still, it is better than violating the DRY principle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use the popular Thor gem to create a daemonized task.
I want to run some thor task on rails 3 in production, but I
This is Thor task: desc 'readkeys', 'Read keys' method_option :password, :type => :string, :desc
I want to call a thor task from a Migration. With rake I can
I have problems listing my tasks in thor, even with the simplest program: class
class B12 < Thor desc write, write data into the index method_option :methods, :desc
Thor like Rake (and Make) has task management. If I call a task multiple
I have a Thor script that uses several methods class Update < Thor desc
I'm trying to use the Thor::Actions template method to generate some C++ test file
I am writing a Rails 3.2 generator and would like to use Thor::Shell::Basic instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.