| Module | Rails::Generator::Options |
| In: |
vendor/rails/railties/lib/rails_generator/options.rb
|
| options | [W] |
# File vendor/rails/railties/lib/rails_generator/options.rb, line 6
6: def self.included(base)
7: base.extend(ClassMethods)
8: class << base
9: if respond_to?(:inherited)
10: alias_method :inherited_without_options, :inherited
11: end
12: alias_method :inherited, :inherited_with_options
13: end
14: end
Adds general options like -h and —quiet. Usually don‘t override.
# File vendor/rails/railties/lib/rails_generator/options.rb, line 119
119: def add_general_options!(opt)
120: opt.separator ''
121: opt.separator 'Rails Info:'
122: opt.on('-v', '--version', 'Show the Rails version number and quit.')
123: opt.on('-h', '--help', 'Show this help message and quit.') { |v| options[:help] = v }
124:
125: opt.separator ''
126: opt.separator 'General Options:'
127:
128: opt.on('-p', '--pretend', 'Run but do not make any changes.') { |v| options[:pretend] = v }
129: opt.on('-f', '--force', 'Overwrite files that already exist.') { options[:collision] = :force }
130: opt.on('-s', '--skip', 'Skip files that already exist.') { options[:collision] = :skip }
131: opt.on('-q', '--quiet', 'Suppress normal output.') { |v| options[:quiet] = v }
132: opt.on('-t', '--backtrace', 'Debugging: show backtrace on errors.') { |v| options[:backtrace] = v }
133: opt.on('-c', '--svn', 'Modify files with subversion. (Note: svn must be in path)') do
134: options[:svn] = `svn status`.inject({}) do |opt, e|
135: opt[e.chomp[7..-1]] = true
136: opt
137: end
138: end
139: 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 vendor/rails/railties/lib/rails_generator/options.rb, line 70
70: def full_options(runtime_options = {})
71: self.class.full_options(options.merge(runtime_options))
72: 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 vendor/rails/railties/lib/rails_generator/options.rb, line 81
81: def parse!(args, runtime_options = {})
82: self.options = {}
83:
84: @option_parser = OptionParser.new do |opt|
85: opt.banner = banner
86: add_options!(opt)
87: add_general_options!(opt)
88: opt.parse!(args)
89: end
90:
91: return args
92: ensure
93: self.options = full_options(runtime_options)
94: end
Raise a usage error. Override usage_message to provide a blurb after the option parser summary.
# File vendor/rails/railties/lib/rails_generator/options.rb, line 98
98: def usage(message = usage_message)
99: raise UsageError, "#{@option_parser}\n#{message}"
100: end