Module: Quickl::Command::ClassMethods

Defined in:
lib/quickl/command/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#doc_extractorObject

Extractor for documentation



10
11
12
# File 'lib/quickl/command/class_methods.rb', line 10

def doc_extractor
  @doc_extractor
end

#super_commandObject

The super command, if any



7
8
9
# File 'lib/quickl/command/class_methods.rb', line 7

def super_command
  @super_command
end

Instance Method Details

#command_nameObject

Returns command name



43
44
45
# File 'lib/quickl/command/class_methods.rb', line 43

def command_name
  module2command(self)
end

#documentation(opts = {}) ⇒ Object Also known as: help

Returns command documentation



48
49
50
# File 'lib/quickl/command/class_methods.rb', line 48

def documentation(opts = {})
  doc_extractor.call(self, opts)
end

#handle_error(ex, cmd = self) ⇒ Object

Handles a command error



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/quickl/command/class_methods.rb', line 102

def handle_error(ex, cmd = self)
  if react_to?(ex) && ex.respond_to?(:react!)
    begin
      ex.command = cmd
      ex.react!
    rescue Quickl::Error => ex2
      handle_error(ex2, cmd)
    end
  else
    raise ex
  end
end

#has_sub_commands?Boolean

Returns true if this command has at least one subcommand

Returns:

  • (Boolean)


31
32
33
# File 'lib/quickl/command/class_methods.rb', line 31

def has_sub_commands?
  @subcommands and !@subcommands.empty?
end

#no_react_to(*args) ⇒ Object

Bypass reaction to some exceptions



84
85
86
87
# File 'lib/quickl/command/class_methods.rb', line 84

def no_react_to(*args)
  @no_react_to ||= []
  @no_react_to += args
end

#no_react_to?(ex) ⇒ Boolean

Should I bypass reaction to a given error?

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/quickl/command/class_methods.rb', line 90

def no_react_to?(ex)
  defined?(@no_react_to) && Array(@no_react_to).find{|c|
    ex.is_a?(c)
  }
end

#overviewObject

Returns command overview



68
69
70
71
# File 'lib/quickl/command/class_methods.rb', line 68

def overview
  doc = documentation.split("\n")
  doc.find{|s| !s.strip.empty?} || "no overview available"
end

#program_nameObject

Returns name of the program under execution



38
39
40
# File 'lib/quickl/command/class_methods.rb', line 38

def program_name
  Quickl.program_name
end

#react_to?(ex) ⇒ Boolean

Should I react to a given error?

Returns:

  • (Boolean)


97
98
99
# File 'lib/quickl/command/class_methods.rb', line 97

def react_to?(ex)
  !no_react_to?(ex)
end

#run(argv = [], requester = nil) ⇒ Object

Runs the command



74
75
76
77
78
79
# File 'lib/quickl/command/class_methods.rb', line 74

def run(argv = [], requester = nil)
  cmd = self.new
  cmd.run(argv, requester)
rescue Quickl::Error => ex
  handle_error(ex, cmd)
end

#subcommand_by_name(name) ⇒ Object

Returns a subcommand by its name, or nil



18
19
20
21
22
23
24
25
26
27
# File 'lib/quickl/command/class_methods.rb', line 18

def subcommand_by_name(name)
  return nil unless has_sub_commands?
  look  = name.split(':')
  found = subcommands.find{|cmd| cmd.command_name == look.first}
  if found.nil? or (look.size == 1)
    return found 
  else
    found.subcommand_by_name(look[1..-1].join(':'))
  end
end

#subcommandsObject

Returns the array of defined subcommands



13
14
15
# File 'lib/quickl/command/class_methods.rb', line 13

def subcommands
  @subcommands ||= []
end

#usageObject

Returns command usage



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quickl/command/class_methods.rb', line 54

def usage
  doc = documentation.split("\n")
  doc.each_with_index{|line,i|
    case line
      when /Usage:/ 
        return line.strip
      when /SYNOPSIS/
        return doc[i+1].strip || "no usage available"
    end
  } 
  "no usage available"
end