필요에 따라 --help
옵션을 다시 정의 할 수 있습니다. 이 버전에서
require 'optparse'
#create parsers
opts = OptionParser.new()
opts.banner = "Usage: example.rb [options]"
opts.separator("test optparse with --help[=full]")
opts.on("-v", "--[no-]verbose", "Run verbosely") { |v|
puts "->Verbose ist #{v.inspect}"
}
opts.on("-r", "--repeat REPEAT", "Repeat REPEAT times") { |v|
puts "->Repeat ist #{v.inspect}"
}
#Define your own --help
opts.on("-h", "--help [HELP]", "Help") { |v|
case v
when 'full' #write original help
puts opts.help
when nil, '' #write script specific help
puts opts.banner
opts.summarize([], opts.summary_width) { |helpline|
#make your own decision on each helpline
#~ puts helpline #puts each line
puts helpline unless helpline =~ /-v/ #ignore -v
}
else
puts opts.banner
puts <<helpmessage
Undefined --help option. Please use 'full' or no option
#{File.basename(__FILE__)} --help
#{File.basename(__FILE__)} --help=full
helpmessage
end
}
opts.parse!
는
--help
모든 옵션을 보여줍니다,하지만
-v
. 직접 선택하거나 완전히 다른 도움을받을 수 있습니다.
감사합니다. 이것은 거의 정확하게 필요한 것입니다. 몇 가지 수정 작업을 통해 원하는대로 할 수 있습니다. 고마워. –