2012-12-14 45 views
4

Ruby「OptionParser將自動從您的描述中爲您生成幫助屏幕」 [http://ruby.about.com/od/advancedruby/a/optionparser.htm]Ruby OptionParser:隱藏命令選項的幫助文本

有沒有辦法刪除命令選項的幫助文本。 我可以使用隱藏命令,而是使用命令選項(切換)並隱藏其幫助上下文。

回答

2

我能夠把一個不那麼優雅的解決方案放在一起。它會隱藏在主幫助屏幕的選項,這聽起來像它可能適合您的需要:如果您運行--help

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 

,你會看到這樣的輸出:

Usage: test.rb [options] 
    -v, --verbose     Verbose output 
    -h, --help      Display this help