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 7955105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:38:06+00:00 2026-06-04T03:38:06+00:00

I have an option parsing gem that provides a DSL for creating commands. In

  • 0

I have an option parsing gem that provides a DSL for creating commands. In practice, it often ends up looking like this:

option :ints,    Integer, arity: [1, -1], on_multiple: :append
option :floats,  Float,   arity: [1, -1], on_multiple: :append
option :complex, Complex, arity: [1, -1], on_multiple: :append

These are class methods. Not very DRY, as you can see. It would be better to write something like this:

scope arity: [1, -1], on_multiple: :append do
  option :ints,    Integer
  option :floats,  Float
  option :complex, Complex
end

To have the options hash given to scope transparently merged with the one given to option. That’s where I’m stuck. I’m not sure where to store the common options so that I can merge them later.

Any ideas?


option forwards everything to Option#new:

def option(*args, &block)
  # self is a class that represents a command
  self.options << Option.new(*args, &block)
end

As requested, here’s the code, with usage of support gems removed:

def initialize(key, *args, &block)
  # Retrieve the options hash from the argument array.
  options = args.last.is_a?(Hash) ? args.pop : {}

  # The rest of the implementation...
  type = args.find { |arg| arg.is_a? Module }

  strings = args.flatten.select do |arg|
    arg.is_a? String
  end.group_by do |arg|
    arg =~ Parser::Regexp::SWITCH ? :switches : :description
  end

  self.key = key
  self.names = strings.fetch(:switches) { [ Option.name_from(key) ] }
  self.description = options.fetch :description, strings.fetch(:description, []).first
  self.on_multiple = options.fetch :on_multiple, :replace
  self.arity = options.fetch :arity, nil
  self.default = options.fetch :default, nil
  self.required = options.fetch :required, false
  self.type = type || String
  self.handler = block
end

Original on GitHub.

  • 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-04T03:38:08+00:00Added an answer on June 4, 2026 at 3:38 am

    Take a look at Object#with_options from Rails, and swipe that for your own use.

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

Sidebar

Related Questions

I am parsing XML with DOM i have some data tags like: <data> <option>abc</option>
I have written this gem that has more than 10,000 generated classes. While installing
I have an option pane that is displayed upon closing my Application ( windowClosing()
riting a very simple search using like and have an option to omit options,
I'm soon to launch a beta app and this have the option to create
I have tried to append <option> inside <select> using javascript by parsing data from
I have an app that is parsing the html page and extracts some text
I know that any language is capable of parsing XML; I'm really just looking
Since GLSL doesn't have an include-file option I'm trying to add this by using
I have a XML and it has an attribute option or combo box, parsing

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.