Project

General

Profile

Actions

Misc #14812

closed

Question: Option Parser

Added by xz0r (xz0r xz0r) almost 6 years ago. Updated almost 6 years ago.

Status:
Closed
Assignee:
-
[ruby-core:87359]

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) almost 6 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

Also available in: Atom PDF

Like0
Like0