Class: Climate::Option
- Inherits:
-
Object
- Object
- Climate::Option
- Includes:
- Described
- Defined in:
- lib/climate/parser.rb
Overview
Wraps the properties supplied to Trollop::Parser#opt for some OO over engineered sweetness
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes included from Described
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
- #multi? ⇒ Boolean
- #optional? ⇒ Boolean
- #parser ⇒ Object
- #required? ⇒ Boolean
- #short ⇒ Object
- #short_usage ⇒ Object
- #spec ⇒ Object
- #type ⇒ Object
- #usage(options = {}) ⇒ Object
Methods included from Described
Constructor Details
#initialize(name, description, options = {}) ⇒ Option
Returns a new instance of Option.
23 24 25 26 |
# File 'lib/climate/parser.rb', line 23 def initialize(name, description, ={}) @options = super end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/climate/parser.rb', line 21 def @options end |
Instance Method Details
#add_to(parser) ⇒ Object
67 68 69 |
# File 'lib/climate/parser.rb', line 67 def add_to(parser) parser.opt(@name, @description, @options) end |
#default ⇒ Object
31 |
# File 'lib/climate/parser.rb', line 31 def default ; spec[:default] ; end |
#long ⇒ Object
29 |
# File 'lib/climate/parser.rb', line 29 def long ; spec[:long] ; end |
#long_usage ⇒ Object
43 44 45 |
# File 'lib/climate/parser.rb', line 43 def long_usage type == :flag ? "--#{long}" : "--#{long}=<#{type}>" end |
#multi? ⇒ Boolean
35 |
# File 'lib/climate/parser.rb', line 35 def multi? ; spec[:multi] ; end |
#optional? ⇒ Boolean
33 |
# File 'lib/climate/parser.rb', line 33 def optional? ; spec.has_key?(:default) ; end |
#parser ⇒ Object
39 40 41 |
# File 'lib/climate/parser.rb', line 39 def parser @parser ||= Trollop::Parser.new.tap {|p| add_to(p) } end |
#required? ⇒ Boolean
34 |
# File 'lib/climate/parser.rb', line 34 def required? ; ! optional? ; end |
#short ⇒ Object
30 |
# File 'lib/climate/parser.rb', line 30 def short ; spec[:short] ; end |
#short_usage ⇒ Object
47 48 49 |
# File 'lib/climate/parser.rb', line 47 def short_usage short && (type == :flag ? "-#{short}" : "-#{short} <#{name}>") end |
#spec ⇒ Object
37 |
# File 'lib/climate/parser.rb', line 37 def spec ; @specs ||= parser.specs[@name] ; end |
#type ⇒ Object
28 |
# File 'lib/climate/parser.rb', line 28 def type ; spec[:type] ; end |
#usage(options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/climate/parser.rb', line 51 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}]" elsif multi? && !.fetch(:hide_optional, false) "[#{help}...]" else help end end |