Class: Ergane::OptionDefinition
- Defined in:
- lib/ergane/option_definition.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #attach(parser, store) ⇒ Object
- #boolean? ⇒ Boolean
- #default_value ⇒ Object
-
#initialize(name, type = nil, short: nil, description: nil, default: nil, required: false, optional: false) ⇒ OptionDefinition
constructor
A new instance of OptionDefinition.
- #long_flag ⇒ Object
- #short_flag ⇒ Object
- #signature ⇒ Object
Constructor Details
#initialize(name, type = nil, short: nil, description: nil, default: nil, required: false, optional: false) ⇒ OptionDefinition
Returns a new instance of OptionDefinition.
7 8 9 10 11 12 13 14 15 |
# File 'lib/ergane/option_definition.rb', line 7 def initialize(name, type = nil, short: nil, description: nil, default: nil, required: false, optional: false) @name = name.to_sym @type = type @short = short&.to_s @description = description @default = default @required = required @optional = optional end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def name @name end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def optional @optional end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def required @required end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def short @short end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/ergane/option_definition.rb', line 5 def type @type end |
Instance Method Details
#attach(parser, store) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ergane/option_definition.rb', line 41 def attach(parser, store) args = [short_flag, long_flag].compact args << type if type && !boolean? args << description if description parser.on(*args) { |value| store[name] = value } end |
#boolean? ⇒ Boolean
17 18 19 |
# File 'lib/ergane/option_definition.rb', line 17 def boolean? type.nil? || type == TrueClass || type == FalseClass end |
#default_value ⇒ Object
21 22 23 |
# File 'lib/ergane/option_definition.rb', line 21 def default_value boolean? ? (default.nil? ? false : default) : default end |
#long_flag ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/ergane/option_definition.rb', line 25 def long_flag flag = "--#{name.to_s.tr('_', '-')}" unless boolean? flag += optional ? "=[VALUE]" : "=VALUE" end flag end |
#short_flag ⇒ Object
33 34 35 |
# File 'lib/ergane/option_definition.rb', line 33 def short_flag "-#{short}" if short end |
#signature ⇒ Object
37 38 39 |
# File 'lib/ergane/option_definition.rb', line 37 def signature [("#{short_flag}," if short), long_flag].compact.join(" ") end |