Class: Option

Inherits:
Object
  • Object
show all
Defined in:
lib/nub/commander.rb

Overview

Command option encapsulation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, desc, type: nil, required: false, allowed: {}) ⇒ Option

Create a new option instance

Parameters:

  • key (String)

    option short hand, long hand and hint e.g. -s|–skip=COMPONENTS

  • desc (String)

    the option’s description

  • type (Type) (defaults to: nil)

    the option’s type

  • required (Bool) (defaults to: false)

    require the option if true else optional

  • allowed (Hash) (defaults to: {})

    hash of allowed strings to descriptions maps



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

#allowedObject

Returns the value of attribute allowed.



35
36
37
# File 'lib/nub/commander.rb', line 35

def allowed
  @allowed
end

#descObject (readonly)

Returns the value of attribute desc.



33
34
35
# File 'lib/nub/commander.rb', line 33

def desc
  @desc
end

#hintObject (readonly)

Returns the value of attribute hint.



32
33
34
# File 'lib/nub/commander.rb', line 32

def hint
  @hint
end

#keyObject (readonly)

Returns the value of attribute key.



29
30
31
# File 'lib/nub/commander.rb', line 29

def key
  @key
end

#longObject (readonly)

Returns the value of attribute long.



31
32
33
# File 'lib/nub/commander.rb', line 31

def long
  @long
end

#requiredObject

Returns the value of attribute required.



36
37
38
# File 'lib/nub/commander.rb', line 36

def required
  @required
end

#shortObject (readonly)

Returns the value of attribute short.



30
31
32
# File 'lib/nub/commander.rb', line 30

def short
  @short
end

#typeObject (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

Parameters:

  • level (Integer) (defaults to: 0)

    level to indent



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_symObject

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