Class: Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/commander-tool.rb

Defined Under Namespace

Classes: Command, Option

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Commander

Returns a new instance of Commander.



56
57
58
59
# File 'lib/commander-tool.rb', line 56

def initialize
  @commands = {}
  @global_options = []
end

Instance Method Details

#add_command(name, description = "", &block) ⇒ Object



61
62
63
64
65
# File 'lib/commander-tool.rb', line 61

def add_command(name, description = "", &block)
  command = Command.new(name, description, &block)
  @commands[command.name] = command
  command
end

#add_option(switches, description = "") ⇒ Object



67
68
69
70
# File 'lib/commander-tool.rb', line 67

def add_option(switches, description = "")
  @global_options << Option.new(switches, description)
  self
end

#parse(args = ARGV) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/commander-tool.rb', line 72

def parse(args = ARGV)
  global_options, remaining = parse_options(
    args,
    @global_options
  )

  command_name = remaining.shift

  if command_name.nil?
    handle_no_command(global_options)
    return
  end

  command = @commands[command_name]

  unless command
    raise_unknown_command(command_name)
  end

  parse_command(
    command,
    remaining,
    global_options
  )
end