Class: Cl::Arg
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#opts ⇒ Object
Returns the value of attribute opts.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #default ⇒ Object
- #default? ⇒ Boolean
- #define(const) ⇒ Object
- #description ⇒ Object
- #enum ⇒ Object
- #enum? ⇒ Boolean
- #known?(value) ⇒ Boolean
- #required? ⇒ Boolean
- #separator ⇒ Object
- #set(cmd, value) ⇒ Object
- #splat? ⇒ Boolean
- #to_s ⇒ Object
- #type ⇒ Object
- #unknown(value) ⇒ Object
Methods included from Cast
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name
4 5 6 |
# File 'lib/cl/arg.rb', line 4 def name @name end |
#opts ⇒ Object
Returns the value of attribute opts
4 5 6 |
# File 'lib/cl/arg.rb', line 4 def opts @opts end |
Instance Method Details
#array? ⇒ Boolean
24 25 26 |
# File 'lib/cl/arg.rb', line 24 def array? type == :array end |
#default ⇒ Object
40 41 42 |
# File 'lib/cl/arg.rb', line 40 def default opts[:default] end |
#default? ⇒ 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 |
#description ⇒ Object
28 29 30 |
# File 'lib/cl/arg.rb', line 28 def description opts[:description] end |
#enum ⇒ Object
32 33 34 |
# File 'lib/cl/arg.rb', line 32 def enum Array(opts[:enum]) end |
#enum? ⇒ Boolean
36 37 38 |
# File 'lib/cl/arg.rb', line 36 def enum? opts.key?(:enum) end |
#known?(value) ⇒ Boolean
48 49 50 |
# File 'lib/cl/arg.rb', line 48 def known?(value) enum.include?(value) end |
#required? ⇒ Boolean
52 53 54 |
# File 'lib/cl/arg.rb', line 52 def required? !!opts[:required] end |
#separator ⇒ Object
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
60 61 62 |
# File 'lib/cl/arg.rb', line 60 def splat? !!opts[:splat] && array? end |
#to_s ⇒ Object
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 |
#type ⇒ Object
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 |