class Benry::CLI::OptionSchema

Attributes

argflag[R]
argname[R]
callback[R]
desc[R]
long[R]
name[R]
short[R]

Public Class Methods

new(name, short, long, argname, argflag, desc, &callback) click to toggle source
# File lib/benry/cli.rb, line 26
def initialize(name, short, long, argname, argflag, desc, &callback)
  @name     = name
  @short    = short
  @long     = long
  @argname  = argname
  @argflag  = argflag   # :required, :optional, or nil
  @desc     = desc
  @callback = callback
end
parse(defstr, desc, name: nil, &callback) click to toggle source
# File lib/benry/cli.rb, line 62
def self.parse(defstr, desc, name: nil, &callback)
  #; [!cy1ux] regards canonical name of '-f NAME #file' as 'file'.
  defstr = defstr.strip()
  defstr = defstr.sub(/\s+\#(\w+)\z/, '')
  name ||= $1
  #; [!fdh36] can parse '-v, --version' (short + long).
  #; [!jkmee] can parse '-v' (short)
  #; [!uc2en] can parse '--version' (long).
  #; [!sy157] can parse '-f, --file=FILE' (short + long + required-arg).
  #; [!wrjqa] can parse '-f FILE' (short + required-arg).
  #; [!ip99s] can parse '--file=FILE' (long + required-arg).
  #; [!9pmv8] can parse '-i, --indent[=N]' (short + long + optional-arg).
  #; [!ooo42] can parse '-i[N]' (short + optional-arg).
  #; [!o93c7] can parse '--indent[=N]' (long + optional-arg).
  #; [!gzuhx] can parse string with extra spaces.
  case defstr
  when /\A-(\w),\s*--(\w[-\w]*)(?:=(\S+)|\[=(\S+)\])?\z/ ; arr = [$1,  $2,  $3, $4]
  when /\A-(\w)(?:\s+(\S+)|\[(\S+)\])?\z/                ; arr = [$1,  nil, $2, $3]
  when /\A--(\w[-\w]*)(?:=(\S+)|\[=(\S+)\])?\z/          ; arr = [nil, $1,  $2, $3]
  else
    #; [!1769n] raises error when invalid format.
    raise OptionDefinitionError.new("'#{defstr}': failed to parse option definition.")
  end
  short, long, arg_required, arg_optional = arr
  #; [!j2wgf] raises error when '-i [N]' specified.
  defstr !~ /\A-\w\s+\[/  or
    raise OptionDefinitionError.new("'#{defstr}': failed to parse option definition"+\
                                    " due to extra space before '['"+\
                                    " (should be '#{defstr.sub(/\s+/, '')}').")
  #; [!6f4xx] uses long name or short name as option name when option name is not specfied.
  name = name || long || short
  #
  argname = arg_required || arg_optional
  argflag = arg_required ? :required \
          : arg_optional ? :optional : nil
  return self.new(name.to_s, short, long, argname, argflag, desc, &callback)
end

Public Instance Methods

==(other) click to toggle source
# File lib/benry/cli.rb, line 38
def ==(other)
  return (
    self.class  == other.class    \
    && @short   == other.short    \
    && @long    == other.long     \
    && @argname == other.argname  \
    && @argflag == other.argflag  \
    && @desc    == other.desc     \
    && @callback == other.callback
  )
end
arg_nothing?() click to toggle source
# File lib/benry/cli.rb, line 58
def arg_nothing?
  return @argflag == nil
end
arg_optional?() click to toggle source
# File lib/benry/cli.rb, line 54
def arg_optional?
  return @argflag == :optional
end
arg_required?() click to toggle source
# File lib/benry/cli.rb, line 50
def arg_required?
  return @argflag == :required
end
option_string() click to toggle source
# File lib/benry/cli.rb, line 100
def option_string
  #; [!pdaz3] builds option definition string.
  s = ""
  case
  when @short && @long ; s << "-#{@short}, --#{@long}"
  when @short          ; s << "-#{@short}"
  when           @long ; s << "    --#{@long}"
  else
    raise "unreachable"
  end
  #
  case
  when arg_required? ; s << (@long ? "=#{@argname}"   : " #{@argname}")
  when arg_optional? ; s << (@long ? "[=#{@argname}]" : "[#{@argname}]")
  else               ; nil
  end
  #
  return s
end