Class: WIP::Runner::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/runner/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Command

Returns a new instance of Command.



48
49
50
51
# File 'lib/wip/runner/command.rb', line 48

def initialize(io)
  @io     = io
  @parser = WIP::Runner::Parser.new(@io, self.class)
end

Instance Attribute Details

#parserObject (readonly)

TODO(?)… :arguments, :options



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

def parser
  @parser
end

Class Method Details

.argument(name, definition) ⇒ Object



26
27
28
# File 'lib/wip/runner/command.rb', line 26

def argument(name, definition)
  arguments[name] = Options.new(definition)
end

.arguments(value = nil) ⇒ Object



30
31
32
# File 'lib/wip/runner/command.rb', line 30

def arguments(value = nil)
  @arguments ||= {}
end

.commandsObject



22
23
24
# File 'lib/wip/runner/command.rb', line 22

def commands
  @commands ||= Commands.within(self)
end

.options(&block) ⇒ Object

TODO: test multi-options



35
36
37
38
39
# File 'lib/wip/runner/command.rb', line 35

def options(&block)
  @options ||= []
  @options << block if block_given?
  @options
end

.overview(value = nil) ⇒ Object



17
18
19
20
# File 'lib/wip/runner/command.rb', line 17

def overview(value = nil)
  @overview = value unless value.nil?
  @overview
end

.signatureObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/wip/runner/command.rb', line 5

def signature
  @signature ||= begin
    ns    = Commands.name
    parts = self.name
      .sub(/^.*(CLI|Commands)::/, '')
      .split('::')
    parts.map { |part| part.gsub(/([A-Z])/, "-\\1").sub(/^-/, '') }
      .join(' ')
      .downcase
  end
end

.workflow(&block) ⇒ Object



41
42
43
# File 'lib/wip/runner/command.rb', line 41

def workflow(&block)
  Workflow.define(&block)
end

Instance Method Details

#run(argv = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wip/runner/command.rb', line 53

def run(argv = [])
  parser.run(argv) do |command, arguments, options|
    if command.nil?
      if options.help
        parser.help
        return
      end

      validate!(arguments)
      execute(arguments, options)
    else
      # TODO: add a spec for the help path.
      command.match(/^-/) ? parser.help : delegate(command, argv)
    end
  end
rescue OptionParser::InvalidOption, InvalidArguments, InvalidCommand => e
  print_error(e)
end