Class: Mothership::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, description = nil) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mothership/command.rb', line 11

def initialize(context, description = nil)
  @context = context
  @description = description
  @aliases = []

  # inputs accepted by command
  @inputs = {}

  # inputs that act as arguments
  @arguments = []

  # flag -> input (e.g. --name -> :name)
  @flags = {}

  # various callbacks
  @before = []
  @after = []
  @around = []
  @filters = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



9
10
11
# File 'lib/mothership/command.rb', line 9

def after
  @after
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#aroundObject (readonly)

Returns the value of attribute around.



9
10
11
# File 'lib/mothership/command.rb', line 9

def around
  @around
end

#beforeObject (readonly)

Returns the value of attribute before.



9
10
11
# File 'lib/mothership/command.rb', line 9

def before
  @before
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/mothership/command.rb', line 5

def description
  @description
end

#filtersObject (readonly)

Returns the value of attribute filters.



9
10
11
# File 'lib/mothership/command.rb', line 9

def filters
  @filters
end

#flagsObject (readonly)

Returns the value of attribute flags.



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

def flags
  @flags
end

#inputsObject (readonly)

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#interactionsObject

Returns the value of attribute interactions.



5
6
7
# File 'lib/mothership/command.rb', line 5

def interactions
  @interactions
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/mothership/command.rb', line 5

def name
  @name
end

Instance Method Details

#add_input(name, options = {}, &interact) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mothership/command.rb', line 93

def add_input(name, options = {}, &interact)
  options[:interact] = interact if interact
  options[:description] = options.delete(:desc) if options.key?(:desc)

  options[:type] ||=
    case options[:default]
    when true, false
      :boolean
    when Integer
      :integer
    when Float
      :floating
    end

  unless options[:hidden]
    @flags["--#{name.to_s.gsub("_", "-")}"] = name

    if options[:singular]
      @flags["--#{options[:singular]}"] = name
    end

    if aliases = options[:aliases] || options[:alias]
      Array(aliases).each do |a|
        @flags[a] = name
      end
    end
  end

  # :argument => true means accept as single argument
  # :argument => :foo is shorthand for :argument => {:type => :foo}
  if opts = options[:argument]
    type =
      case opts
      when true
        :normal
      when Symbol
        opts
      when Hash
        opts[:type]
      end

    options[:argument] = type

    @arguments <<
      { :name => name,
        :type => type,
        :value => options[:value]
      }
  end

  @inputs[name] = options
end

#display_nameObject



36
37
38
# File 'lib/mothership/command.rb', line 36

def display_name
  @name.to_s.gsub("_", "-")
end

#inspectObject



32
33
34
# File 'lib/mothership/command.rb', line 32

def inspect
  "\#<Command '#{@name}'>"
end

#invoke(inputs = {}, given = {}, global = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mothership/command.rb', line 59

def invoke(inputs = {}, given = {}, global = {})
  @before.each { |f, c| c.new.instance_exec(&f) }

  name = @name

  ctx = @context.new(self)
  ctx.extend @interactions if @interactions

  ctx.input = Inputs.new(self, ctx, inputs, given, global)


  action = proc do |*given_inputs|
    ctx.input = given_inputs.first || ctx.input
    ctx.run(name)
  end

  cmd = self
  @around.each do |a, c|
    before = action

    sub = c.new(cmd, ctx.input)
    action = proc do |*given_inputs|
      ctx.input = given_inputs.first || ctx.input
      sub.instance_exec(before, ctx.input, &a)
    end
  end

  res = ctx.instance_exec(ctx.input, &action)

  @after.each { |f, c| c.new.instance_exec(&f) }

  res
end

#usageObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mothership/command.rb', line 40

def usage
  args = [display_name]
  @arguments.each do |a|
    name = (a[:value] || a[:name]).to_s.upcase
    input = @inputs[a[:name]]

    if a[:type] == :splat
      args << "#{name}..."
    elsif a[:type] == :optional || input.key?(:default) || \
            input.key?(:interact)
      args << "[#{name}]"
    else
      args << name
    end
  end

  args.join(" ")
end