Class: Minitar::CLI::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/minitar/cli/commander.rb

Overview

The command dispatcher for Minitar::CLI. This will be replaced in a future version by one of the better-executed CLI application frameworks like GLI, after Ruby 1.8 and 1.9 support have been dropped.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ioe) ⇒ Commander

Returns a new instance of Commander.



11
12
13
14
15
# File 'lib/minitar/cli/commander.rb', line 11

def initialize(ioe)
  @ioe = default_ioe(ioe)
  @commands = {}
  @default_command = nil
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/minitar/cli/commander.rb', line 7

def commands
  @commands
end

#default_commandObject

Returns the value of attribute default_command.



8
9
10
# File 'lib/minitar/cli/commander.rb', line 8

def default_command
  @default_command
end

#ioeObject (readonly)

Returns the value of attribute ioe.



9
10
11
# File 'lib/minitar/cli/commander.rb', line 9

def ioe
  @ioe
end

Instance Method Details

#command(command) ⇒ Object Also known as: []



49
50
51
52
53
54
55
# File 'lib/minitar/cli/commander.rb', line 49

def command(command)
  if command?(command)
    commands[command]
  else
    default_command
  end
end

#command?(command) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/minitar/cli/commander.rb', line 45

def command?(command)
  commands.key?(command)
end

#default_ioe(ioe = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/minitar/cli/commander.rb', line 58

def default_ioe(ioe = {})
  ioe[:input] ||= $stdin
  ioe[:output] ||= $stdout
  ioe[:error] ||= $stderr
  ioe
end

#register(command) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minitar/cli/commander.rb', line 17

def register(command)
  command = command.new(self) if command.is_a?(Class)
  raise CommandAlreadyExists if command.commander != self
  raise CommandAlreadyExists if command?(command.name)

  commands[command.name] = command

  # rubocop:disable Style/GuardClause
  if command.respond_to?(:altname)
    commands[command.altname] = command unless command?(command.altname)
  end
  # rubocop:enable Style/GuardClause
end