Module: UltraCommandLine::Manager::Commands

Includes:
CmdLineArgs
Included in:
Base, Processors
Defined in:
lib/ultra_command_line/manager/commands.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CmdLineArgs

#cmd_line_args, #cmd_line_args=

Instance Attribute Details

#commandsObject



8
9
10
# File 'lib/ultra_command_line/manager/commands.rb', line 8

def commands
  @commands ||= []
end

Instance Method Details

#aliases_consistent?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ultra_command_line/manager/commands.rb', line 19

def aliases_consistent?
  # including aliases
  full_names_list = []
  commands.each do |command|
    if command.root_command?
      return false unless command.name.empty? && command.aliases == [command.name]
    else
      command.aliases.each do |alias_name|
        return false if full_names_list.include? alias_name.to_s
        full_names_list << alias_name.to_s
      end

    end
  end
  true
end

#cmd_line_args_for_command(command) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/ultra_command_line/manager/commands.rb', line 50

def cmd_line_args_for_command(command)
  if command.root_command?
    cmd_line_args
  else
    clfc = cmd_line_args.dup
    clfc.shift
    clfc
  end
end

#command(cmd_line_args = self.cmd_line_args) ⇒ Object



36
37
38
39
# File 'lib/ultra_command_line/manager/commands.rb', line 36

def command(cmd_line_args = self.cmd_line_args)
  command_alias = cmd_line_args.empty? ? '' : cmd_line_args.first
  command_by_alias(command_alias) || root_command
end

#command_by_alias(command_alias) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ultra_command_line/manager/commands.rb', line 41

def command_by_alias(command_alias)
  raise UltraCommandLine::Error, 'command names/aliases are not consistent !' unless aliases_consistent?
  candidates = commands.select do |command|
    command.aliases.map(&:to_s).include? command_alias.to_s
  end
  # raise UltraCommandLine::Error, "There is no command named '#{command_alias}'!" unless candidates.siez == 1
  candidates.size == 1 ? candidates.first : nil
end

#root_commandObject



12
13
14
15
16
17
# File 'lib/ultra_command_line/manager/commands.rb', line 12

def root_command
  return nil if commands.empty?
  l = commands.select {|c| c.root_command? && c.name.empty? }
  raise UltraCommandLine::Error, 'Wrong commands definition !' unless l.count == 1
  l.first
end