Methods
- add_general_options!
- add_options!
- banner
- default_options
- full_options
- included
- mandatory_options
- options
- parse!
- usage
- usage_message
Classes and Modules
Module Rails::Generator::Options::ClassMethodsAttributes
| [W] | options |
Public Class methods
[ show source ]
# File 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
Public Instance methods
Each instance has an options hash that‘s populated by parse.
[ show source ]
# File railties/lib/rails_generator/options.rb, line 49
49: def options
50: @options ||= {}
51: end
Protected Instance methods
Adds general options like -h and —quiet. Usually don‘t override.
[ show source ]
# File 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] = {}
135: `svn status`.each_line do |line|
136: options[:svn][line.chomp[7..-1]] = true
137: end
138: end
139: opt.on('-g', '--git', 'Modify files with git. (Note: git must be in path)') do
140: options[:git] = {:new => {}, :modified => {}}
141: `git status`.each_line do |line|
142: options[:git][:new][line.chomp[14..-1]] = true if line =~ /new file:/
143: options[:git][:modified][line.chomp[14..-1]] = true if line =~ /modified:/
144: end
145: end
146: end
Override to add your options to the parser:
def add_options!(opt)
opt.on('-v', '--verbose') { |value| options[:verbose] = value }
end
[ show source ]
# File railties/lib/rails_generator/options.rb, line 115
115: def add_options!(opt)
116: end
[ show source ]
# File railties/lib/rails_generator/options.rb, line 107
107: def banner
108: "Usage: #{$0} [options]"
109: end
Convenient access to class default options.
[ show source ]
# File railties/lib/rails_generator/options.rb, line 61
61: def default_options
62: self.class.default_options
63: 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)
[ show source ]
# File 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
Convenient access to class mandatory options.
[ show source ]
# File railties/lib/rails_generator/options.rb, line 56
56: def mandatory_options
57: self.class.mandatory_options
58: 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..
[ show source ]
# File 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.
[ show source ]
# File railties/lib/rails_generator/options.rb, line 98
98: def usage(message = usage_message)
99: raise UsageError, "#{@option_parser}\n#{message}"
100: end
[ show source ]
# File railties/lib/rails_generator/options.rb, line 102
102: def usage_message
103: ''
104: end