# File lib/rubigen/options.rb, line 5 5: def self.included(base) 6: base.extend(ClassMethods) 7: class << base 8: if respond_to?(:inherited) 9: alias_method :inherited_without_options, :inherited 10: end 11: alias_method :inherited, :inherited_with_options 12: end 13: end
Adds general options like -h and —quiet. Usually don’t override.
# File lib/rubigen/options.rb, line 118 118: def add_general_options!(opt) 119: opt.separator 'General Options:' 120: 121: opt.on('-h', '--help', 'Show this help message and quit.') { |v| options[:help] = v } 122: opt.on('-p', '--pretend', 'Run but do not make any changes.') { |v| options[:pretend] = v } 123: opt.on('-f', '--force', 'Overwrite files that already exist.') { options[:collision] = :force } 124: opt.on('-s', '--skip', 'Skip files that already exist.') { options[:collision] = :skip } 125: opt.on('-q', '--quiet', 'Suppress normal output.') { |v| options[:quiet] = v } 126: opt.on('-t', '--backtrace', 'Debugging: show backtrace on errors.') { |v| options[:backtrace] = v } 127: opt.on('-c', '--svn', 'Modify files with subversion. (Note: svn must be in path)') do 128: options[:svn] = `svn status`.inject({}) do |opt, e| 129: opt[e.chomp[7..1]] = true 130: opt 131: end 132: end 133: opt.on('-g', '--git', 'Modify files with git. (Note: git must be in path)') do 134: options[:git] = `git status`.inject({:new => {}, :modified => {}}) do |opt, e| 135: opt[:new][e.chomp[14..1]] = true if e =~ /new file:/ 136: opt[:modified][e.chomp[14..1]] = true if e =~ /modified:/ 137: opt 138: end 139: end 140: end
Override to add your options to the parser:
def add_options!(opt) opt.on('-v', '--verbose') { |value| options[:verbose] = value } end
# File lib/rubigen/options.rb, line 114 114: def add_options!(opt) 115: end
Convenient access to class default options.
# File lib/rubigen/options.rb, line 60 60: def default_options 61: self.class.default_options 62: end
Merge together our instance options. In increasing precedence:
default_options (class default options) options (instance options) runtime_options (provided as argument) mandatory_options (class mandatory options)
# File lib/rubigen/options.rb, line 69 69: def full_options(runtime_options = {}) 70: self.class.full_options(options.merge(runtime_options)) 71: end
Convenient access to class mandatory options.
# File lib/rubigen/options.rb, line 55 55: def mandatory_options 56: self.class.mandatory_options 57: end
Parse arguments into the options hash. Classes may customize parsing behavior by overriding these methods:
#banner Usage: ./script/generate [options] #add_options! Options: some options.. #add_general_options! General Options: general options..
# File lib/rubigen/options.rb, line 80 80: def parse!(args, runtime_options = {}) 81: self.options = {} 82: 83: @option_parser = OptionParser.new do |opt| 84: opt.banner = banner 85: add_options!(opt) 86: add_general_options!(opt) 87: opt.parse!(args) 88: end 89: 90: return args 91: ensure 92: self.options = full_options(runtime_options) 93: end
Raise a usage error. Override usage_message to provide a blurb after the option parser summary.
# File lib/rubigen/options.rb, line 97 97: def usage(message = usage_message) 98: raise UsageError, "#{@option_parser}\n#{message}" 99: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.