Module: Sickle::ClassMethods
- Defined in:
- lib/sickle.rb
Instance Method Summary collapse
- #__commands ⇒ Object
- #__global_options ⇒ Object
- #desc(label) ⇒ Object
- #flag(name) ⇒ Object
- #global_flag(name) ⇒ Object
- #global_option(name, opts = {}) ⇒ Object
- #include_modules(hash) ⇒ Object
- #included(base) ⇒ Object
- #method_added(a) ⇒ Object
- #option(name, opts = {}) ⇒ Object
- #run(argv) ⇒ Object
Instance Method Details
#__commands ⇒ Object
221 222 223 |
# File 'lib/sickle.rb', line 221 def __commands @__commands ||= {} end |
#__global_options ⇒ Object
225 226 227 |
# File 'lib/sickle.rb', line 225 def @__global_options ||= {} end |
#desc(label) ⇒ Object
193 194 195 |
# File 'lib/sickle.rb', line 193 def desc(label) Sickle.push_desc(label) end |
#flag(name) ⇒ Object
209 210 211 |
# File 'lib/sickle.rb', line 209 def flag(name) option(name, :default => false) end |
#global_flag(name) ⇒ Object
201 202 203 |
# File 'lib/sickle.rb', line 201 def global_flag(name) global_option(name, :default => false) end |
#global_option(name, opts = {}) ⇒ Object
197 198 199 |
# File 'lib/sickle.rb', line 197 def global_option(name, opts = {}) [name.to_s] = Option.new(name, opts) end |
#include_modules(hash) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/sickle.rb', line 213 def include_modules(hash) hash.each do |key, value| Sickle.push_namespace(key) send(:include, value) Sickle.pop_namespace end end |
#included(base) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/sickle.rb', line 186 def included(base) __commands.each do |name, command| name = (Sickle.namespace + [name]).join(":") base.__commands[name] = command end end |
#method_added(a) ⇒ Object
268 269 270 271 272 273 |
# File 'lib/sickle.rb', line 268 def method_added(a) meth = instance_method(a) if desc = Sickle.pop_desc __commands[a.to_s] = Command.new(meth, a, desc, Sickle.) end end |
#option(name, opts = {}) ⇒ Object
205 206 207 |
# File 'lib/sickle.rb', line 205 def option(name, opts = {}) Sickle.push_option(name, opts) end |
#run(argv) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/sickle.rb', line 229 def run(argv) # puts "ARGV: #{argv.inspect}" if command_name = argv.shift command_name = command_name.gsub("-", "_") if command = __commands[command_name] all = .values + command..values results = {} args = OptionParser.new do |parser| all.each do |option| option.register(parser, results) end end.parse!(argv) all.each do |o| results[o.name] ||= o.default end # puts "args: #{args.inspect}" # puts "results: #{results.inspect}" obj = self.new obj.instance_variable_set(:@__options, results) command.meth.bind(obj).call(*args) else puts "\e[31mCommand '#{command_name}' not found\e[0m" puts run(["help"]) end else run(["help"]) end end |