Class: Samovar::Option
- Inherits:
-
Object
- Object
- Samovar::Option
- Defined in:
- lib/samovar/option.rb
Overview
Represents a single command-line option.
An option is a flag-based argument that can have various forms (short, long, with or without values).
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
An optional block to transform the parsed value.
-
#default ⇒ Object
The default value if the option is not provided.
-
#description ⇒ Object
readonly
A description of the option for help output.
-
#flags ⇒ Object
The flags for this option.
-
#key ⇒ Object
The key to use for storing the value.
-
#required ⇒ Object
Whether the option is required.
-
#type ⇒ Object
readonly
The type to coerce the value to.
-
#value ⇒ Object
A fixed value to use regardless of user input.
Instance Method Summary collapse
-
#coerce(result) ⇒ Object
Coerce and transform the result.
-
#coerce_type(result) ⇒ Object
Coerce the result to the specified type.
-
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option
constructor
Initialize a new option.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse this option from the input.
-
#to_a ⇒ Object
Generate an array representation for usage output.
-
#to_s ⇒ Object
Generate a string representation for usage output.
Constructor Details
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option
Initialize a new option.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/samovar/option.rb', line 24 def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) @flags = Flags.new(flags) @description = description if key @key = key else @key = @flags.first.key end @default = default # If the value is given, it overrides the user specified input. @value = value @value ||= true if @flags.boolean? @type = type @required = required @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
An optional block to transform the parsed value.
83 84 85 |
# File 'lib/samovar/option.rb', line 83 def block @block end |
#default ⇒ Object
The default value if the option is not provided.
63 64 65 |
# File 'lib/samovar/option.rb', line 63 def default @default end |
#description ⇒ Object (readonly)
A description of the option for help output.
53 54 55 |
# File 'lib/samovar/option.rb', line 53 def description @description end |
#flags ⇒ Object
The flags for this option.
48 49 50 |
# File 'lib/samovar/option.rb', line 48 def flags @flags end |
#key ⇒ Object
The key to use for storing the value.
58 59 60 |
# File 'lib/samovar/option.rb', line 58 def key @key end |
#required ⇒ Object
Whether the option is required.
78 79 80 |
# File 'lib/samovar/option.rb', line 78 def required @required end |
#type ⇒ Object (readonly)
The type to coerce the value to.
73 74 75 |
# File 'lib/samovar/option.rb', line 73 def type @type end |
#value ⇒ Object
A fixed value to use regardless of user input.
68 69 70 |
# File 'lib/samovar/option.rb', line 68 def value @value end |
Instance Method Details
#coerce(result) ⇒ Object
Coerce and transform the result.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/samovar/option.rb', line 107 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end |
#coerce_type(result) ⇒ Object
Coerce the result to the specified type.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/samovar/option.rb', line 89 def coerce_type(result) if @type == Integer Integer(result) elsif @type == Float Float(result) elsif @type == Symbol result.to_sym elsif @type.respond_to? :call @type.call(result) elsif @type.respond_to? :new @type.new(result) end end |
#parse(input, parent = nil, default = nil) ⇒ Object
Parse this option from the input.
125 126 127 128 129 130 131 |
# File 'lib/samovar/option.rb', line 125 def parse(input, parent = nil, default = nil) result = @flags.parse(input) if result != nil @value.nil? ? coerce(result) : @value end end |
#to_a ⇒ Object
Generate an array representation for usage output.
143 144 145 146 147 148 149 150 151 |
# File 'lib/samovar/option.rb', line 143 def to_a if @default [@flags, @description, "(default: #{@default})"] elsif @required [@flags, @description, "(required)"] else [@flags, @description] end end |
#to_s ⇒ Object
Generate a string representation for usage output.
136 137 138 |
# File 'lib/samovar/option.rb', line 136 def to_s @flags end |