Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby OptionParser: hiding help text for a command option

Tags:

ruby

Ruby "OptionParser will automatically generate help screens for you from this description" [http://ruby.about.com/od/advancedruby/a/optionparser.htm]

Is there a way to remove the help text for a command option. I can use a hidden command, but rather having a command option (switch) and hide its help context.

like image 498
oakist Avatar asked Dec 14 '12 01:12

oakist


1 Answers

I was able to throw together a not-so-elegant solution to this. It will hide the option from the main help screen, it sounds like it might fit your needs:

require 'optparse'

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options]"

  opts.on("-a", "--argument 1,2,3", Array, "Array of arguments") { |a| options[:array] = a  }
  opts.on("-v", "--verbose", "Verbose output") { |v| options[:verbose] = true }
  opts.on("-h", "--help", "Display this help") do
    hidden_switch = "--argument"
    #Typecast opts to a string, split into an array of lines, delete the line 
    #if it contains the argument, and then rejoins them into a string
    puts opts.to_s.split("\n").delete_if { |line| line =~ /#{hidden_switch}/ }.join("\n") 
    exit
  end
end

If you were to run the --help, you would see this output:

Usage: test.rb [options]
    -v, --verbose                    Verbose output
    -h, --help                       Display this help
like image 120
Eugene Avatar answered Nov 12 '22 00:11

Eugene