rubyoptionparser

ruby option parser. Is there a way to add a hidden option?


Is there a way in ruby to add an option to optionParser that only the users who who know about it can add it, and it won't show in the options list?

Thanks!


Solution

  • I think the easiest way would be to separately parse the hidden option before calling parse! on the option parser.

    if ARGV.delete '--secret'
      ...
    end
    
    OptionParser.new do |opts|
      ...
    end.parse!
    

    Though that means you'll need to manually handle the hidden option parsing. Alternatively, you could define a separate OptionParser for the hidden options and rescue OptionParser::InvalidOption when it parses, and then parse with the normal parser. But that would be sensitive to the order in which options are specified.