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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:45:40+00:00 2026-05-16T16:45:40+00:00

Is there any way to kick off OptionParser several times in one Ruby program,

  • 0

Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options?

For example:

$ myscript.rb --subsys1opt a --subsys2opt b

Here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where ‘a’ is processed first, followed by ‘b’ in separate OptionParser object; each time picking options only relevant for that context.
A final phase could check that there is nothing unknown left after each part processed theirs.

The use cases are:

  1. In a loosely coupled front-end program, where various components have different arguments, I don’t want ‘main’ to know about everything, just to delegate sets of arguments/options to each part.

  2. Embedding some larger system like RSpec into my application, and I’d to simply pass a command-line through their options without my wrapper knowing those.

I’d be OK with some delimiter option as well, like -- or --vmargs in some Java apps.

There are lots of real world examples for similar things in the Unix world (startx/X, git plumbing and porcelain), where one layer handles some options but propagates the rest to the lower layer.

Out of the box, this doesn’t seem to work. Each OptionParse.parse! call will do exhaustive processing, failing on anything it doesn’t know about.
I guess I’d happy to skip unknown options.

Any hints, perhaps alternative approaches are welcome.

  • 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-05-16T16:45:40+00:00Added an answer on May 16, 2026 at 4:45 pm

    Assuming the order in which the parsers will run is well defined, you can just store the extra options in a temporary global variable and run OptionParser#parse! on each set of options.

    The easiest way to do this is to use a delimiter like you alluded to. Suppose the second set of arguments is separated from the first by the delimiter --. Then this will do what you want:

    opts = OptionParser.new do |opts|
      # set up one OptionParser here
    end
    
    both_args = $*.join(" ").split(" -- ")
    $extra_args = both_args[1].split(/\s+/)
    opts.parse!(both_args[0].split(/\s+/))
    

    Then, in the second code/context, you could do:

    other_opts = OptionParser.new do |opts|
      # set up the other OptionParser here
    end
    
    other_opts.parse!($extra_args)
    

    Alternatively, and this is probably the “more proper” way to do this, you could simply use OptionParser#parse, without the exclamation point, which won’t remove the command-line switches from the $* array, and make sure that there aren’t options defined the same in both sets. I would advise against modifying the $* array by hand, since it makes your code harder to understand if you are only looking at the second part, but you could do that. You would have to ignore invalid options in this case:

    begin
        opts.parse
    rescue OptionParser::InvalidOption
        puts "Warning: Invalid option"
    end
    

    The second method doesn’t actually work, as was pointed out in a comment. However, if you have to modify the $* array anyway, you can do this instead:

    tmp = Array.new
    
    while($*.size > 0)
        begin
            opts.parse!
        rescue OptionParser::InvalidOption => e
            tmp.push(e.to_s.sub(/invalid option:\s+/,''))
        end
    end
    
    tmp.each { |a| $*.push(a) }
    

    It’s more than a little bit hack-y, but it should do what you want.

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

Sidebar

Related Questions

Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert
Is there any way to use Google's API to retrieve a user's current zipcode
Is there any way to overload the > (greater than) operator, to be able
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there any way to stop animation in iOS 3 ? I know about:
Is there any way to update nested documents by id or some other field?
Is there any way in which I can automatically convert a Custom Class Object
Is there any way to achieve compiling Node.js scripts as native code, like Hip-Hop
Is there any way to stop or terminate long running Oracle query in JDBC

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.