Class: Benry::CLI::ActionInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/benry/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_name, name, desc, option_schemas, action_class, action_method) ⇒ ActionInfo

Returns a new instance of ActionInfo.



322
323
324
325
326
327
328
329
# File 'lib/benry/cli.rb', line 322

def initialize(full_name, name, desc, option_schemas, action_class, action_method)
  @full_name      = full_name
  @name           = name
  @desc           = desc
  @option_schemas = option_schemas
  @action_class   = action_class
  @action_method  = action_method
end

Instance Attribute Details

#action_classObject (readonly)

Returns the value of attribute action_class.



331
332
333
# File 'lib/benry/cli.rb', line 331

def action_class
  @action_class
end

#action_methodObject (readonly)

Returns the value of attribute action_method.



331
332
333
# File 'lib/benry/cli.rb', line 331

def action_method
  @action_method
end

#descObject (readonly)

Returns the value of attribute desc.



331
332
333
# File 'lib/benry/cli.rb', line 331

def desc
  @desc
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



331
332
333
# File 'lib/benry/cli.rb', line 331

def full_name
  @full_name
end

#nameObject (readonly)

Returns the value of attribute name.



331
332
333
# File 'lib/benry/cli.rb', line 331

def name
  @name
end

#option_schemasObject (readonly)

Returns the value of attribute option_schemas.



331
332
333
# File 'lib/benry/cli.rb', line 331

def option_schemas
  @option_schemas
end

Instance Method Details

#==(other) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/benry/cli.rb', line 333

def ==(other)
  return (
    self.class == other.class                   \
    && @full_name      == other.full_name       \
    && @name           == other.name            \
    && @desc           == other.desc            \
    && @option_schemas == other.option_schemas  \
    && @action_class   == other.action_class    \
    && @action_method  == other.action_method
  )
end

#help_message(command) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/benry/cli.rb', line 345

def help_message(command)
  #; [!hjq5l] builds help message.
  meth = @action_class.new.method(@action_method)
  argstr = ""
  meth.parameters.each do |kind, name|
    #; [!7qmnz] replaces '_' in arg names with '-'.
    #; [!s6p09] converts arg name 'file_or_dir' into 'file|dir'.
    name_str = name.to_s.gsub('_or_', '|').gsub('_', '-')
    case kind
    when :req ; argstr << " <#{name_str}>"
    when :opt ; argstr << " [<#{name_str}>]"
    when :rest; argstr << " [<#{name_str}>...]"
    end
  end
  #; [!6m50d] don't show non-described options.
  pairs = @option_schemas.collect {|opt| [opt.option_string, opt.desc] }
  pairs = pairs.select {|optstr, desc| desc }
  #
  width = pairs.collect {|pair| pair[0].length }.max || 0
  width = [width, 20].max
  width = [width, 35].min
  #
  msg = ""
  #msg << "#{command} #{@full_name}  --  #{@desc}\n"
  msg << "#{@desc}\n"
  msg << "\n"
  msg << "Usage:\n"
  msg << "  #{command} #{@full_name} [<options>]#{argstr}\n"
  msg << "\n"            unless pairs.empty?
  msg << "Options:\n"    unless pairs.empty?
  pairs.each do |option_string, desc|
    msg << "  %-#{width}s : %s\n" % [option_string, desc]
  end
  return msg
end