Class: Migr8::Util::CommandOptionDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defstr) ⇒ CommandOptionDefinition

Returns a new instance of CommandOptionDefinition.



1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
# File 'lib/migr8.rb', line 1966

def initialize(defstr)
  case defstr
  when /\A *--(\w[-\w]*)(?:\[=(.+?)\]|=(\S.*?))?(?:\s+\#(\w+))?\s*:(?:\s+(.*)?)?\z/
    short, long, arg, name, desc = nil, $1, ($2 || $3), $4, $5
    arg_required = $2 ? nil : $3 ? true : false
  when /\A *-(\w),\s*--(\w[-\w]*)(?:\[=(.+?)\]|=(\S.*?))?(?:\s+\#(\w+))?\s*:(?:\s+(.*)?)?\z/
    short, long, arg, name, desc = $1, $2, ($3 || $4), $5, $6
    arg_required = $3 ? nil : $4 ? true : false
  when /\A *-(\w)(?:\[(.+?)\]|\s+([^\#\s].*?))?(?:\s+\#(\w+))?\s*:(?:\s+(.*)?)?\z/
    short, long, arg, name, desc = $1, nil, ($2 || $3), $4, $5
    arg_required = $2 ? nil : $3 ? true : false
  else
    raise CommandOptionDefinitionError.new("'#{defstr}': invalid definition.")
  end
  name ||= (long || short)
  #
  @short = _strip(short)
  @long  = _strip(long)
  @arg   = _strip(arg)
  @name  = _strip(name)
  @desc  = _strip(desc)
  @arg_required = arg_required
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def arg
  @arg
end

#arg_requiredObject

Returns the value of attribute arg_required.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def arg_required
  @arg_required
end

#descObject

Returns the value of attribute desc.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def desc
  @desc
end

#longObject

Returns the value of attribute long.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def long
  @long
end

#nameObject

Returns the value of attribute name.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def name
  @name
end

#shortObject

Returns the value of attribute short.



1964
1965
1966
# File 'lib/migr8.rb', line 1964

def short
  @short
end

Instance Method Details

#usage(width = 20) ⇒ Object



1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
# File 'lib/migr8.rb', line 1990

def usage(width=20)
  argreq = @arg_required
  if @short && @long
    s = "-#{@short}, --#{@long}"           if argreq == false
    s = "-#{@short}, --#{@long}=#{@arg}"   if argreq == true
    s = "-#{@short}, --#{@long}[=#{@arg}]" if argreq == nil
  elsif @long
    s = "--#{@long}"           if argreq == false
    s = "--#{@long}=#{@arg}"   if argreq == true
    s = "--#{@long}[=#{@arg}]" if argreq == nil
  elsif @short
    s = "-#{@short}"           if argreq == false
    s = "-#{@short} #{@arg}"   if argreq == true
    s = "-#{@short}[#{@arg}]"  if argreq == nil
  end
  #; [!xd9do] returns option usage with specified width.
  return "%-#{width}s: %s" % [s, @desc]
end