Class: Climate::Option
- Inherits:
-
Object
- Object
- Climate::Option
- Defined in:
- lib/climate/option.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add_to(parser) ⇒ Object
- #default ⇒ Object
-
#initialize(name, description, options = {}) ⇒ Option
constructor
A new instance of Option.
- #long ⇒ Object
- #long_usage ⇒ Object
- #optional? ⇒ Boolean
- #parser ⇒ Object
- #required? ⇒ Boolean
- #short ⇒ Object
- #short_usage ⇒ Object
- #spec ⇒ Object
- #type ⇒ Object
- #usage(options = {}) ⇒ Object
Constructor Details
#initialize(name, description, options = {}) ⇒ Option
Returns a new instance of Option.
8 9 10 11 12 |
# File 'lib/climate/option.rb', line 8 def initialize(name, description, ={}) @name = name @description = description @options = end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/climate/option.rb', line 5 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/climate/option.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/climate/option.rb', line 6 def @options end |
Instance Method Details
#add_to(parser) ⇒ Object
50 51 52 |
# File 'lib/climate/option.rb', line 50 def add_to(parser) parser.opt(@name, @description, @options) end |
#default ⇒ Object
17 |
# File 'lib/climate/option.rb', line 17 def default ; spec[:default] ; end |
#long ⇒ Object
15 |
# File 'lib/climate/option.rb', line 15 def long ; spec[:long] ; end |
#long_usage ⇒ Object
28 29 30 |
# File 'lib/climate/option.rb', line 28 def long_usage type == :flag ? "--#{long}" : "--#{long}=<#{type}>" end |
#optional? ⇒ Boolean
19 |
# File 'lib/climate/option.rb', line 19 def optional? ; spec.has_key?(:default) ; end |
#parser ⇒ Object
24 25 26 |
# File 'lib/climate/option.rb', line 24 def parser @parser ||= Trollop::Parser.new.tap {|p| add_to(p) } end |
#required? ⇒ Boolean
20 |
# File 'lib/climate/option.rb', line 20 def required? ; ! optional? ; end |
#short ⇒ Object
16 |
# File 'lib/climate/option.rb', line 16 def short ; spec[:short] ; end |
#short_usage ⇒ Object
32 33 34 |
# File 'lib/climate/option.rb', line 32 def short_usage short && (type == :flag ? "-#{short}" : "-#{short}<#{type}>") end |
#spec ⇒ Object
22 |
# File 'lib/climate/option.rb', line 22 def spec ; @specs ||= parser.specs[@name] ; end |
#type ⇒ Object
14 |
# File 'lib/climate/option.rb', line 14 def type ; spec[:type] ; end |
#usage(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/climate/option.rb', line 36 def usage(={}) help = short_usage || long_usage if [:with_long] && (long_usage != help) help = [help, long_usage].compact.join(.fetch(:separator, '|')) end if optional? && !.fetch(:hide_optional, false) "[#{help}]" else help end end |