Class: Clin::Option
- Inherits:
-
Object
- Object
- Clin::Option
- Defined in:
- lib/clin/option.rb
Overview
Option container.
Instance Attribute Summary collapse
-
#argument ⇒ String
readonly
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil.
-
#block ⇒ Object
Returns the value of attribute block.
-
#description ⇒ Object
Returns the value of attribute description.
-
#long ⇒ String
readonly
Get the long option If @long is nil it will use #default_long If @long is false it will return nil.
-
#name ⇒ Object
Returns the value of attribute name.
-
#optional_argument ⇒ Object
Returns the value of attribute optional_argument.
-
#short ⇒ String
readonly
Get the short option If @short is nil it will use #default_short If @short is false it will return nil.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #default_argument ⇒ Object
- #default_long ⇒ Object
- #default_short ⇒ Object
-
#initialize(name, description, short: nil, long: nil, argument: nil, optional_argument: false, &block) ⇒ Option
constructor
A new instance of Option.
- #on(value, out) ⇒ Object
- #option_parser_arguments ⇒ Object
-
#register(opts, out) ⇒ Object
Register the option to the Option Parser.
Constructor Details
#initialize(name, description, short: nil, long: nil, argument: nil, optional_argument: false, &block) ⇒ Option
Returns a new instance of Option.
8 9 10 11 12 13 14 15 16 |
# File 'lib/clin/option.rb', line 8 def initialize(name, description, short: nil, long: nil, argument: nil, optional_argument: false, &block) @name = name @description = description @short = short @long = long @optional_argument = optional_argument @argument = argument @block = block end |
Instance Attribute Details
#argument ⇒ String (readonly)
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil
67 68 69 |
# File 'lib/clin/option.rb', line 67 def argument @argument end |
#block ⇒ Object
Returns the value of attribute block.
5 6 7 |
# File 'lib/clin/option.rb', line 5 def block @block end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/clin/option.rb', line 5 def description @description end |
#long ⇒ String (readonly)
Get the long option If @long is nil it will use #default_long If @long is false it will return nil
58 59 60 |
# File 'lib/clin/option.rb', line 58 def long @long end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/clin/option.rb', line 5 def name @name end |
#optional_argument ⇒ Object
Returns the value of attribute optional_argument.
5 6 7 |
# File 'lib/clin/option.rb', line 5 def optional_argument @optional_argument end |
#short ⇒ String (readonly)
Get the short option If @short is nil it will use #default_short If @short is false it will return nil
49 50 51 |
# File 'lib/clin/option.rb', line 49 def short @short end |
Instance Method Details
#==(other) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/clin/option.rb', line 81 def ==(other) return false unless other.is_a? Clin::Option @name == other.name && @description == other.description && short == other.short && long == other.long && argument == other.argument && @optional_argument == other.optional_argument && @block == other.block end |
#default_argument ⇒ Object
41 42 43 |
# File 'lib/clin/option.rb', line 41 def default_argument name.to_s.upcase end |
#default_long ⇒ Object
37 38 39 |
# File 'lib/clin/option.rb', line 37 def default_long "--#{name.downcase}" end |
#default_short ⇒ Object
33 34 35 |
# File 'lib/clin/option.rb', line 33 def default_short "-#{name[0].downcase}" end |
#on(value, out) ⇒ Object
77 78 79 |
# File 'lib/clin/option.rb', line 77 def on(value, out) out[@name] = value end |
#option_parser_arguments ⇒ Object
72 73 74 75 |
# File 'lib/clin/option.rb', line 72 def option_parser_arguments args = [short, long_argument, description] args.compact end |
#register(opts, out) ⇒ Object
Register the option to the Option Parser
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/clin/option.rb', line 21 def register(opts, out) if @block.nil? opts.on(*option_parser_arguments) do |value| on(value, out) end else opts.on(*option_parser_arguments) do |value| block.call(opts, out, value) end end end |