Class: Option
- Inherits:
-
Object
- Object
- Option
- Defined in:
- lib/nub/commander.rb
Overview
Command option encapsulation
Instance Attribute Summary collapse
-
#allowed ⇒ Object
Returns the value of attribute allowed.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#required ⇒ Object
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
-
#initialize(key, desc, type: nil, required: false, allowed: {}) ⇒ Option
constructor
Create a new option instance.
-
#to_s(level: 0) ⇒ Object
Return a human readable string of this object.
-
#to_sym ⇒ Object
Get a symbol representing the command.
Constructor Details
#initialize(key, desc, type: nil, required: false, allowed: {}) ⇒ Option
Create a new option instance
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nub/commander.rb', line 44 def initialize(key, desc, type:nil, required:false, allowed:{}) @hint = nil @long = nil @short = nil @desc = desc @allowed = allowed || {} @required = required || false Log.die("allowed should be a hash of values to descriptions") if allowed.class != Hash Log.die("required should be a boolean value") if ![TrueClass, FalseClass].include?(required.class) # Parse the key into its components (short hand, long hand, and hint) #https://bneijt.nl/pr/ruby-regular-expressions/ # Valid forms to look for with chars [a-zA-Z0-9-_=|] # --help, --help=HINT, -h|--help, -h|--help=HINT Log.die("invalid option key #{key}") if key && (key.count('=') > 1 or key.count('|') > 1 or !key[/[^\w\-=|]/].nil? or key[/(^--[a-zA-Z0-9\-_]+$)|(^--[a-zA-Z\-_]+=\w+$)|(^-[a-zA-Z]\|--[a-zA-Z0-9\-_]+$)|(^-[a-zA-Z]\|--[a-zA-Z0-9\-_]+=\w+$)/].nil?) @key = key if key @hint = key[/.*=(.*)$/, 1] @short = key[/^(-\w).*$/, 1] @long = key[/(--[\w\-]+)(=.+)*$/, 1] end # Convert true/false to TrueClass/FalseClass type = TrueClass if type.class == TrueClass type = FalseClass if type.class == FalseClass # Validate and set type, allow Flag defaults to be true or false Log.die("invalid option type #{type}") if ![String, Integer, Array, TrueClass, FalseClass, nil].any?{|x| type == x} Log.die("option type must be set") if @hint && !type @type = String if !key && !type @type = FalseClass if key and !type @type = type if type # Validate hint is given for non flags Log.die("option hint must be set") if @key && !@hint && @type != FalseClass && @type != TrueClass # Validate allowed if @allowed.any? allowed_type = @allowed.first.first.class Log.die("mixed allowed types") if @allowed.any?{|k,v| k.class != allowed_type} end end |
Instance Attribute Details
#allowed ⇒ Object
Returns the value of attribute allowed.
35 36 37 |
# File 'lib/nub/commander.rb', line 35 def allowed @allowed end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
33 34 35 |
# File 'lib/nub/commander.rb', line 33 def desc @desc end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
32 33 34 |
# File 'lib/nub/commander.rb', line 32 def hint @hint end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
29 30 31 |
# File 'lib/nub/commander.rb', line 29 def key @key end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
31 32 33 |
# File 'lib/nub/commander.rb', line 31 def long @long end |
#required ⇒ Object
Returns the value of attribute required.
36 37 38 |
# File 'lib/nub/commander.rb', line 36 def required @required end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
30 31 32 |
# File 'lib/nub/commander.rb', line 30 def short @short end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
34 35 36 |
# File 'lib/nub/commander.rb', line 34 def type @type end |
Instance Method Details
#to_s(level: 0) ⇒ Object
Return a human readable string of this object
96 97 98 |
# File 'lib/nub/commander.rb', line 96 def to_s(level:0) return "#{" " * level * 2}Option => key:#{@key}, desc:'#{@desc}', type:#{@type}, allowed:#{@allowed}, required:#{@required}" end |
#to_sym ⇒ Object
Get a symbol representing the command
90 91 92 |
# File 'lib/nub/commander.rb', line 90 def to_sym return @long[2..-1].gsub("-", "_").to_sym end |