Actions
Misc #14812
closedQuestion: Option Parser
    Misc #14812:
    Question: Option Parser 
  
Status:
Closed
Assignee:
-
Description
Hi,
Is there an option / way to ignore undeclared options and get them in a resultset so, I can handle them in a custom way?
Example scenario: Program A redirects unsupported arguments to another Program B called from inside program A.
Thanks.
        
          
          Updated by nobu (Nobuyoshi Nakada) over 7 years ago
          
          
        
        
      
      - Status changed from Open to Closed
 
Just rescue OptionParser::InvalidOption.
As it can't tell if an unknown option has its argument or not, I didn't add such methods.
require 'optparse'
unknown = []
ARGV.options do |opt|
  opt.on("--foo=FOO") {|v| @foo = v}
  begin
    opt.order! {|arg| unknown << arg}
  rescue OptionParser::InvalidOption => e
    unknown.concat(e.args)
    redo
  end
end
p unknown
p @foo
Actions