Class: Ordu
- Inherits:
-
OptionParser
- Object
- OptionParser
- Ordu
- Defined in:
- lib/ordu.rb
Overview
block specified in the class definition).
Class Method Summary collapse
-
.parse!(args) ⇒ Object
Parses a new instance.
Instance Method Summary collapse
-
#command_summary ⇒ Object
A string which summarizes the commands - shown in the help.
-
#initialize(options: nil, commands: nil, action: nil) ⇒ Ordu
constructor
Make a new Ordu instance.
-
#parse!(args) ⇒ Object
Parse options in order, and call commands for non-option arguments (if the command exists).
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 32 |
# File 'lib/ordu.rb', line 23 def initialize(options: nil, commands: nil, action: nil) super() ||= self.class. commands ||= self.class.commands action ||= self.class.action .each { |o| option!(*o) } commands.each { |c| command!(*c) } on_tail(command_summary) if command_summary @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.
56 57 58 |
# File 'lib/ordu.rb', line 56 def self.parse!(args) new.parse!(args) end |
Instance Method Details
#command_summary ⇒ Object
A string which summarizes the commands - shown in the help
45 46 47 48 49 50 51 |
# File 'lib/ordu.rb', line 45 def command_summary return if commands.empty? w = commands.keys.map(&:length).max lines = ['Available commands:'] lines += commands.map { |k, v| " %-#{w}s %s" % [k, v.desc] } lines.join("\n") end |
#parse!(args) ⇒ Object
Parse options in order, and call commands for non-option arguments (if the command exists). This is how programs are exected.
36 37 38 39 40 41 42 |
# File 'lib/ordu.rb', line 36 def parse!(args) args = order!(args) unless args.empty? return commands[args.shift].parse!(args) if commands.key?(args.first) end run(*args) end |