Class: Awshucks::Command

Inherits:
Object show all
Defined in:
lib/awshucks/command.rb

Overview

wraps up command parsing functions. Command class has class-level accessor for registering commands.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.commandObject

Returns the value of attribute command.



50
51
52
# File 'lib/awshucks/command.rb', line 50

def command
  @command
end

.descriptionObject

Returns the value of attribute description.



52
53
54
# File 'lib/awshucks/command.rb', line 52

def description
  @description
end

.usageObject

Returns the value of attribute usage.



51
52
53
# File 'lib/awshucks/command.rb', line 51

def usage
  @usage
end

Class Method Details

.commandsObject



34
35
36
37
38
39
40
# File 'lib/awshucks/command.rb', line 34

def commands
  @commands ||= returning({}) do |commands|
    inherited_commands.each do |command|
      commands[command.command] = command
    end
  end
end

.execute(args, config) ⇒ Object



46
47
48
# File 'lib/awshucks/command.rb', line 46

def execute(args, config)
  new.execute(args, config)
end

.inherited(klass) ⇒ Object



20
21
22
23
# File 'lib/awshucks/command.rb', line 20

def inherited(klass)
  # save it for later, klass doesn't get fully defined until after this callback
  inherited_commands << klass
end

.option_stringObject



42
43
44
# File 'lib/awshucks/command.rb', line 42

def option_string
  new.option_parser.to_s # ehhh not a huge fan of this...
end

.parse_and_execute(args, config) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/awshucks/command.rb', line 25

def parse_and_execute(args, config)
  key = args.shift
  if key && commands[key] # be explicit about nil, no invalid command inheritance hijinks allowed
    commands[key].execute(args, config)
  else
    raise UnknownCommandError, key
  end
end

Instance Method Details

#execute(args, config) ⇒ Object

instance methods ####

Raises:



66
67
68
# File 'lib/awshucks/command.rb', line 66

def execute(args, config)
  raise CommandError, "must implement the execute method!"
end

#option_parserObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/awshucks/command.rb', line 70

def option_parser
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = self.class.usage
    opts.separator self.class.description
    opts.separator ""
    if respond_to?(:custom_options)
      opts.separator "Options:"
      custom_options(opts)
    else
      opts.separator "No options."
    end
  end
end