class Benry::CLI::ActionInfo

Attributes

action_class[R]
action_method[R]
desc[R]
full_name[R]
name[R]
option_schemas[R]

Public Class Methods

new(full_name, name, desc, option_schemas, action_class, action_method) click to toggle source
# File lib/benry/cli.rb, line 321
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

Public Instance Methods

==(other) click to toggle source
# File lib/benry/cli.rb, line 332
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) click to toggle source
# File lib/benry/cli.rb, line 344
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