Module: Is::Command::Mixin

Included in:
Is::Command, Application
Defined in:
lib/is/command.rb

Constant Summary collapse

@@messages =
{
  :commands => 'Commands',
  :actions => 'Actions',
  :aliases => 'Aliases',
  :commons => 'Common options',
  :options => 'Options'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionArray<String>? (readonly)

Returns:

  • (Array<String>, nil)

35
36
37
# File 'lib/is/command.rb', line 35

def description
  @description
end

#nameSymbol? (readonly)

Returns:

  • (Symbol, nil)

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

def name
  @name
end

Class Method Details

.messages(values = {}) ⇒ Hash

Parameters:

  • values (Hash) (defaults to: {})

Returns:

  • (Hash)

25
26
27
# File 'lib/is/command.rb', line 25

def messages values = {}
  @@messages.merge! values
end

Instance Method Details

#[](name) ⇒ Command, ...

Parameters:

  • name (Symbol, String)

Returns:

  • (Command, Hash, Array, String, Symbol, nil)

65
66
67
# File 'lib/is/command.rb', line 65

def [] name
  @actions[name.intern]
end

#action(name = nil, *description) {|*args| ... } ⇒ Hash, Proc (private)

Parameters:

  • name (Symbol, String, nil) (defaults to: nil)
  • description (Array<String>)

Yields:

  • (*args)

Returns:

  • (Hash, Proc)

50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/is/command.rb', line 50

def action name = nil, *description, &block
  @actions ||= {}
  if name
    @actions[name.intern] = {
      :name => name.intern,
      :description => description,
      :block => block
    }
  else
    @block = block
  end
end

#command(name, *description) {|*args| ... } ⇒ Command (private)

Parameters:

  • name (Symbol, String)
  • description (Array<String>)

Yields:

  • (*args)

Returns:


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

def command name, *description, &block
  @actions ||= {}
  @actions[name.intern] = Command.new self, name, *description, &block
end

#commonsArray<Hash> (protected)

Returns:

  • (Array<Hash>)

106
107
108
109
110
111
112
113
# File 'lib/is/command.rb', line 106

def commons
  @commons ||= []
  if @up.respond_to? :commons
    @up.commons + @commons
  else
    @commons
  end
end

#default {|*args| ... } ⇒ Proc (private)

Yields:

  • (*args)

Returns:

  • (Proc)

93
94
95
# File 'lib/is/command.rb', line 93

def default &block
  action &block
end

#each(*args) {|name, action| ... }

This method returns an undefined value.

Parameters:

  • args (Hash, Array<Symbol>, nil)

Yields:


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/is/command.rb', line 72

def each *args, &block
  if args.size == 0
    c = a = s = true
  elsif Hash === args[0]
    h = args[0]
    c = h[:command]
    a = h[:action]
    s = h[:synonym] || h[:alias]
  else
    c = args.include? :command
    a = args.include? :action
    s = args.include?(:synonym) || args.include?(:alias)
  end
  @actions.select do |k, v|
    (c && Command === v) || (a && Hash === v) ||
      (s && (Array === v || String === v || Symbol === v))
  end.each &block
end

#fullnameString?

Returns:

  • (String, nil)

190
191
192
193
194
195
196
# File 'lib/is/command.rb', line 190

def fullname
  if @up && @up.respond_to?(:fullname)
    @up.fullname + ' ' + @name.to_s
  else
    @name.to_s
  end
end

#help(opts = {}) ⇒ String

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :highlight (Boolean, String, :auto)
  • :color (Boolean, String, :auto)
  • :marker (String)

Returns:

  • (String)

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/is/command.rb', line 203

def help opts = {}
  h = opts[:highlight] || opts[:color]
  hl = opts[:marker] || "\e[1m"
  hr = ''
  case h
  when true, :true, :TRUE, :yes, :YES
    hr = "\e[0m;"
  when false, :false, :FALSE, :no, :NO
    hl = ''
  when nil, :auto, :AUTO
    if $stdout.stat.chardev?
      hr = "\e[0m"
    else
      hl = ''
    end
  else
    hl = h
    hr = "\e[0m"
  end
  cc = {}
  aa = {}
  ss = {}
  @actions.each do |k, v|
    case v
    when Command
      cc[k] = v
    when Hash
      aa[k] = v
    else
      ss[k] = v
    end
  end
  mm = opts[:nocommons] ? @commons : commons or []
  kk = @keys || []
  result = ''
  result << "#{hl}#{fullname}#{hr}\n"
  result << "\t#{@description.join("\n\t")}\n"
  result << "\n"
  if ! cc.empty?
    result << "#{hl}#{@@messages[:commands]}:#{hr}\n"
    cc.each do |k, v|
      result << "\t#{k}\n"
    end
    result << "\n"
  end
  if ! aa.empty?
    result << "#{hl}#{@@messages[:actions]}:#{hr}\n"
    aa.each do |k, v|
      result << "\t#{k}\n"
    end
    result << "\n"
  end
  if ! ss.empty?
    result << "#{hl}#{@@messages[:aliases]}:#{hr}\n"
    aa.each do |k, v|
      if Array === v
        value = v.join ' '
      else
        value = v.to_s
      end
      result << "\t#{k} = #{value}\n"
    end
    result << "\n"
  end
  if ! mm.empty?
    result << "#{hl}#{@@messages[:commons]}:#{hr}\n"
    o = OptionParser.new
    mm.each do |k|
      o.on *k[:defs]
    end
    result << "#{o.summarize.join('')}\n"
    #result << "\n"
  end
  if ! kk.empty?
    result << "#{hl}#{@@messages[:options]}:#{hr}\n"
    o = OptionParser.new
    kk.each do |k|
      o.on *k[:defs]
    end
    result << "#{o.summarize.join('')}\n"
    #result << "\n"
  end
  aa.each do |k, v|
    result << "#{hl}#{fullname} #{k}#{hr}\n"
    result << "\t#{v[:description].join("\n\t")}\n"
    result << "\n"
  end
  cc.each do |k, v|
    result << v.help(:nocommons => true)
  end
  # result << "\n"
  GC.start
  result
end

#key(*defs) {|value| ... } ⇒ Hash (private)

Parameters:

  • defs (Array<String, Class>)

Yields:

  • (value)

Returns:

  • (Hash)

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/is/command.rb', line 118

def key *defs, &block
  @keys ||= []
  @commons ||= []
  if Hash === defs[-1]
    opts = defs.pop
  else
    opts = {}
  end
  k = { :defs => defs, :block => block }
  if opts[:common]
    @commons << k
  else
    @keys << k
  end
  k
end

#parse(*args) ⇒ Array<String> (private)

Parameters:

  • args (Array<String>)

Returns:

  • (Array<String>)

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/is/command.rb', line 137

def parse *args
  @options ||= {}
  @keys ||= []
  @commons ||= []
  context = self
  opts = OptionParser.new do |o|
    (commons + @keys).each do |k|
      defs = k[:defs]
      block = proc do |value|
        context.instance_exec value, &k[:block]
      end
      o.on *defs, &block
    end
  end
  opts.order args
end

#process(*args) ⇒ Object?

Parameters:

  • args (Array<String>)

Returns:

  • (Object, nil)

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/is/command.rb', line 156

def process *args
  @actions ||= {}
  if Hash === args[-1]
    @options = args.pop
  else
    @options = {}
  end
  args = parse *args
  if args[0]
    a = @actions[args[0].intern]
    case a
    when Command
      args.shift
      return a.process *args, @options
    when Hash
      args.shift
      args = parse *args
      return instance_exec *args, &a[:block]
    when Array
      args.shift
      return process *a, *args, @options
    when String, Symbol
      args.shift
      return process a.to_s, *args, @options
    end
  end
  if @block
    return instance_exec *args, &@block
  else
    raise 'Invalid command line.'
  end
end

#synonym(name, source) ⇒ Array<String>, ... (private)

Parameters:

  • name (Symbol, String)
  • source (Array<String>, String, Symbol)

Returns:

  • (Array<String>, String, Symbol)

100
101
102
103
# File 'lib/is/command.rb', line 100

def synonym name, source
  @actions ||= {}
  @actions[name.intern] = source
end

#to_procProc

Returns:

  • (Proc)

299
300
301
# File 'lib/is/command.rb', line 299

def to_proc
  method(:process).to_proc
end