Class: Flagabowski::Option
- Inherits:
-
Object
- Object
- Flagabowski::Option
- Defined in:
- lib/flagabowski.rb
Instance Method Summary collapse
- #cast ⇒ Object
- #default ⇒ Object
- #desc ⇒ Object
-
#initialize(opts) ⇒ Option
constructor
A new instance of Option.
- #long ⇒ Object
- #long_arg ⇒ Object
- #name ⇒ Object
- #satisfied! ⇒ Object
- #satisfied? ⇒ Boolean
- #short ⇒ Object
- #short_arg ⇒ Object
- #short_try_again ⇒ Object
- #unsatisfied? ⇒ Boolean
- #validate!(thing) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Option
Returns a new instance of Option.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/flagabowski.rb', line 6 def initialize(opts) @name = opts[:name] @default = opts[:default] || nil @desc = opts[:desc] || "Lazy option authors have failed you." @long = opts[:long] @opts = opts #for #cast @required = opts[:required] || false @satisfied = false @short = opts[:short] @valid = opts[:valid] end |
Instance Method Details
#cast ⇒ Object
18 19 20 |
# File 'lib/flagabowski.rb', line 18 def cast @cast ||= @opts[:cast] || @default.class end |
#default ⇒ Object
22 23 24 |
# File 'lib/flagabowski.rb', line 22 def default @default end |
#desc ⇒ Object
26 27 28 |
# File 'lib/flagabowski.rb', line 26 def desc @desc end |
#long ⇒ Object
30 31 32 |
# File 'lib/flagabowski.rb', line 30 def long @long || name end |
#long_arg ⇒ Object
34 35 36 |
# File 'lib/flagabowski.rb', line 34 def long_arg %Q{--#{long}#{cast_arg}} end |
#name ⇒ Object
38 39 40 |
# File 'lib/flagabowski.rb', line 38 def name @name.to_s end |
#satisfied! ⇒ Object
42 43 44 |
# File 'lib/flagabowski.rb', line 42 def satisfied! @satisfied = true end |
#satisfied? ⇒ Boolean
46 47 48 |
# File 'lib/flagabowski.rb', line 46 def satisfied? @satisfied end |
#short ⇒ Object
54 55 56 |
# File 'lib/flagabowski.rb', line 54 def short @short ||= namechars.first end |
#short_arg ⇒ Object
58 59 60 |
# File 'lib/flagabowski.rb', line 58 def short_arg %Q{-#{short}#{cast_arg}} end |
#short_try_again ⇒ Object
62 63 64 65 |
# File 'lib/flagabowski.rb', line 62 def short_try_again # perfect is the enemy of good @short = namechars[namechars.index(short)+1] end |
#unsatisfied? ⇒ Boolean
50 51 52 |
# File 'lib/flagabowski.rb', line 50 def unsatisfied? @required ? ! @satisfied : false end |
#validate!(thing) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/flagabowski.rb', line 67 def validate!(thing) return true if @valid.nil? case @valid when Array @valid.include?(thing) when Regexp @valid.match?(thing) else false end end |