Class: Ordu

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/ordu.rb

Overview

block specified in the class definition).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: nil, commands: nil, action: nil) ⇒ Ordu

Make a new Ordu instance. It will be set up with the options, commands and action specified in its class definition.



23
24
25
26
27
28
29
30
31
# File 'lib/ordu.rb', line 23

def initialize(options: nil, commands: nil, action: nil)
  super()
  options ||= self.class.options
  commands ||= self.class.commands
  action ||= self.class.action
  options.each { |o| option!(*o) }
  commands.each { |c| command!(*c) }
  @action = action
end

Class Method Details

.parse!(args) ⇒ Object

Parses a new instance. Note that this must be the top-level, since you can’t supply a name. This is convenient for starting the parser from scripts.



48
49
50
# File 'lib/ordu.rb', line 48

def self.parse!(args)
  new.parse!(args)
end

Instance Method Details

#parse!(args) ⇒ Object

Parse options in order, and call commands for non-option arguments (if the command exists). This is how programs are exected.



35
36
37
38
39
40
41
42
43
# File 'lib/ordu.rb', line 35

def parse!(args)
  args = order!(args)
  unless args.empty?
    if commands.key?(args.first)
      return commands[args.shift].parse!(args) 
    end
  end
  run(*args)
end