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

  • SEARCH
  • Home
  • 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 8580765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:53:49+00:00 2026-06-11T20:53:49+00:00

I work on a structure of classes to realize a cli script. And I

  • 0

I work on a structure of classes to realize a cli script.
And I would like create class method to register a command.
When a register a command, I want automatically generate a getter for this.

So, I have this structure :

lib/my_lib/commands.rb
lib/my_lib/commands/setup_command.rb

And then content of file :

# lib/my_lib/commands.rb
method MyLib
  method Commands

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def register_command(*opts)
        command = opts.size == 0 ? {} : opts.extract_options!
        ...
      end

      def register_options(*opts)
        options = opts.size == 0 ? {} : opts.extract_options!
        ...
      end
    end

    class AbstractCommand
      def name
        ...
      end

      def description
        ...
      end

      def run
        raise Exception, "Command '#{self.clas.name}' invalid"
      end
    end

  end
end
# lib/my_lib/commands/setup_command.rb
module MyLib
  module Commands
    class SetupCommand < AbstractCommand
      include MyLib::Commands

      register_command :name        => "setup",
                       :description => "setup the application"

      def run
        puts "Yeah, my command is running"
      end

    end
  end
end

That I want :

# my_cli_script

#!/usr/bin/env ruby
require 'my_lib/commands/setup_command'

command = MyLib::Commands::SetupCommand.new
puts command.name # => "setup"
puts command.description # => "setup the application"
puts command.run # => "Yeah, my command is running"
  • 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-11T20:53:50+00:00Added an answer on June 11, 2026 at 8:53 pm

    I’d do it somehow like this:

    class CommandDummy
    
      def self.register_command(options = {})
        define_method(:name)        { options[:name] }
        define_method(:description) { options[:name] }
      end
    
      register_command :name        => "setup",
                       :description => "setup the application"
    
      def run
        puts "Yeah, my command is running"
      end
    end
    
    c = CommandDummy.new
    puts c.name          # => "setup"
    puts c.description   # => "setup the application"
    

    add:

    Instead of opts.size == 0 you might use opts.empty?

    edit:

    Just played a bit

    # NOTE: I've no idea where to use stuff like this!
    class CommandDummy
      # Add methods, which returns a given String
      def self.add_method_strings(options = {})
        options.each { |k,v| define_method(k) { v } }
      end
    
      add_method_strings :name        => "setup",
                         :description => "setup the application",
                         :run         => "Yeah, my command is running",
                         :foo         => "bar"
    end
    
    c = CommandDummy.new
    puts c.name          # => "setup"
    puts c.description   # => "setup the application"
    puts c.run           # => "Yeah, my command is running"
    puts c.foo           # => "bar"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an object structure and I want all sub classes of the
I'm new to CoffeeScript, and I like the CoffeeScript classes, but can't work out
I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is
I want to create a table with the following structure: Row1 datacolumn1 datacolumn2 datacolumn2
We have a project structure like following: sharedlib (lib-project, containing classes, that are useful
I am planning to work in TRIE data structure for which I need a
My code work in three step: I fetch a collection with a structure. I
I have created a Structure for my XML to work with JAXB Unmarshalling The
I have created a procedure with this structure but it doesn't work for datetime
Lets say I have a structure struct s { std::deque<Object> q; //won't work with

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.