Class: Cl::Arg

Inherits:
Struct
  • Object
show all
Includes:
Cast
Defined in:
lib/cl/arg.rb

Constant Summary

Constants included from Cast

Cast::FALSE, Cast::TRUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cast

#cast, #split

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name


4
5
6
# File 'lib/cl/arg.rb', line 4

def name
  @name
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts


4
5
6
# File 'lib/cl/arg.rb', line 4

def opts
  @opts
end

Instance Method Details

#define(const) ⇒ Object


7
8
9
# File 'lib/cl/arg.rb', line 7

def define(const)
  const.send(:attr_accessor, name)
end

#descriptionObject


19
20
21
# File 'lib/cl/arg.rb', line 19

def description
  opts[:description]
end

#required?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/cl/arg.rb', line 31

def required?
  !!opts[:required]
end

#separatorObject


23
24
25
# File 'lib/cl/arg.rb', line 23

def separator
  opts[:sep]
end

#set(cmd, value) ⇒ Object


11
12
13
# File 'lib/cl/arg.rb', line 11

def set(cmd, value)
  cmd.send(:"#{name}=", cast(value))
end

#splat?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/cl/arg.rb', line 27

def splat?
  opts[:splat] && type == :array
end

#to_sObject


35
36
37
38
39
40
41
42
43
44
# File 'lib/cl/arg.rb', line 35

def to_s
  str = name
  case type
  when :array          then str = "#{str}.."
  when :boolean, :bool then str = "#{str}:bool"
  when :integer, :int  then str = "#{str}:int"
  when :float          then str = "#{str}:float"
  end
  required? ? str : "[#{str}]"
end

#typeObject


15
16
17
# File 'lib/cl/arg.rb', line 15

def type
  opts[:type] || :string
end