Class: Cl::Arg

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cast

#cast

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

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  type == :array
end

#defaultObject



40
41
42
# File 'lib/cl/arg.rb', line 40

def default
  opts[:default]
end

#default?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cl/arg.rb', line 44

def default?
  opts.key?(:default)
end

#define(const) ⇒ Object



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

def define(const)
  mod = Module.new
  mod.send(:attr_accessor, name)
  mod.class_eval "def #{name}?; #{name}.is_a?(Array) ? !#{name}.empty? : !!#{name} end"
  const.send(:include, mod)
end

#descriptionObject



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

def description
  opts[:description]
end

#enumObject



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

def enum
  Array(opts[:enum])
end

#enum?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cl/arg.rb', line 36

def enum?
  opts.key?(:enum)
end

#known?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/cl/arg.rb', line 48

def known?(value)
  enum.include?(value)
end

#required?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cl/arg.rb', line 52

def required?
  !!opts[:required]
end

#separatorObject



56
57
58
# File 'lib/cl/arg.rb', line 56

def separator
  opts[:sep]
end

#set(cmd, value) ⇒ Object



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

def set(cmd, value)
  value = cast(value)
  unknown(value) if enum? && !known?(value)
  cmd.send(:"#{name}=", value)
end

#splat?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/cl/arg.rb', line 60

def splat?
  !!opts[:splat] && array?
end

#to_sObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/cl/arg.rb', line 68

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



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

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

#unknown(value) ⇒ Object



64
65
66
# File 'lib/cl/arg.rb', line 64

def unknown(value)
  raise UnknownArgumentValue.new(value, enum.join(', '))
end