Class: Opts::CommandParser
- Inherits:
-
Object
- Object
- Opts::CommandParser
- Defined in:
- lib/opts/command_parser.rb
Instance Method Summary collapse
- #call(env, args) ⇒ Object
- #cmd(name, app = nil, *args, &block) ⇒ Object
-
#initialize(default_app = nil, &block) ⇒ CommandParser
constructor
A new instance of CommandParser.
Constructor Details
#initialize(default_app = nil, &block) ⇒ CommandParser
Returns a new instance of CommandParser.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/opts/command_parser.rb', line 3 def initialize(default_app=nil, &block) @default_app = default_app @commands = {} if block and block.arity == -1 instance_eval(&block) elsif block_given? yield(self) end end |
Instance Method Details
#call(env, args) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/opts/command_parser.rb', line 23 def call(env, args) cmd_name = args.first.to_s cmd = @commands[cmd_name] raise Opts::UnknownCommandError, "Unknown command #{cmd_name}" unless cmd (env[:commands] ||= []) << cmd_name cmd[:app].call(env, args[1..-1]) end |
#cmd(name, app = nil, *args, &block) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/opts/command_parser.rb', line 14 def cmd(name, app=nil, *args, &block) if Class === app app = app.new(*args, &block) end @commands[name.to_s] = { :app => app || block } end |