Class: Dorian::Arguments
- Inherits:
-
Object
- Object
- Dorian::Arguments
- Defined in:
- lib/dorian/arguments.rb
Constant Summary collapse
- BOOLEAN =
"boolean"- STRING =
"string"- NUMBER =
"number"- INTEGER =
"integer"- DECIMAL =
"decimal"- DEFAULT_MULTIPLE =
false- DEFAULT_ALIASES =
[].freeze
- DEFAULT_TYPE =
BOOLEAN- TYPES =
[BOOLEAN, STRING, NUMBER, INTEGER, DECIMAL].freeze
- DEFAULT =
nil- MATCHES =
{ BOOLEAN => /^(true|false)$/i, STRING => /^.+$/i, NUMBER => /^[0-9.]+$/i, INTEGER => /^[0-9]+$/i, DECIMAL => /^[0-9.]+$/i }.freeze
- BOOLEANS =
{ "true" => true, "false" => false }.freeze
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Class Method Summary collapse
Instance Method Summary collapse
- #cast(type, value) ⇒ Object
- #help ⇒ Object
-
#initialize(**definition) ⇒ Arguments
constructor
A new instance of Arguments.
- #many?(array) ⇒ Boolean
- #normalize(value) ⇒ Object
- #parameterize(string, separator: "-") ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(**definition) ⇒ Arguments
Returns a new instance of Arguments.
34 35 36 |
# File 'lib/dorian/arguments.rb', line 34 def initialize(**definition) @definition = definition end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
8 9 10 |
# File 'lib/dorian/arguments.rb', line 8 def definition @definition end |
Class Method Details
.parse ⇒ Object
38 39 40 |
# File 'lib/dorian/arguments.rb', line 38 def self.parse(...) new(...).parse end |
Instance Method Details
#cast(type, value) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dorian/arguments.rb', line 98 def cast(type, value) if value.nil? nil elsif type == BOOLEAN BOOLEANS.fetch(value.downcase) elsif type == INTEGER value.to_i elsif [DECIMAL, NUMBER].include?(type) value.to_d else value end end |
#help ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dorian/arguments.rb', line 112 def help = "USAGE: #{File.basename($PROGRAM_NAME)}\n" += "\n" if definition.any? definition.each do |key, value| type, default, aliases = normalize(value) = ([key] + aliases) .map(&:to_s) .map { |key| parameterize(key) } .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" } .join("|") += " #{} #{type.upcase}, default: #{cast(type, default).inspect}\n" end end |
#many?(array) ⇒ Boolean
134 135 136 |
# File 'lib/dorian/arguments.rb', line 134 def many?(array) array.size > 1 end |
#normalize(value) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/dorian/arguments.rb', line 147 def normalize(value) if value.is_a?(Hash) type = value[:type]&.to_s || DEFAULT_TYPE default = value[:default]&.to_s || DEFAULT aliases = Array( value[:alias]&.to_s || value[:aliases]&.map(&:to_s) || DEFAULT_ALIASES ) else type = value.to_s default = DEFAULT aliases = DEFAULT_ALIASES end [type, default, aliases] end |
#parameterize(string, separator: "-") ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/dorian/arguments.rb', line 138 def parameterize(string, separator: "-") string .to_s .encode("ASCII", invalid: :replace, undef: :replace, replace: "") .downcase .gsub(/[^a-z0-9]+/, separator) .gsub(/^#{separator}+|#{separator}+$/, "") end |
#parse ⇒ Object
42 43 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 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dorian/arguments.rb', line 42 def parse arguments = ARGV = {} definition.each do |key, value| type, default, aliases = normalize(value) keys = ([key] + aliases).map(&:to_s).map { |key| parameterize(key) } keys = keys.map { |key| ["--#{key}", "-#{key}"] }.flatten indexes = [] values = arguments .map .with_index do |argument, index| if keys.include?(argument.split("=").first) indexes << index if argument.include?("=") argument.split("=", 2).last elsif arguments[index + 1].to_s&.match?(MATCHES.fetch(type)) indexes << (index + 1) arguments[index + 1] elsif type == BOOLEAN "true" else default end end end .compact indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) } values = [default] if values.empty? values = values.map { |value| cast(type, value) } values = values.first unless many?(values) [key] = values end files = arguments.select { |argument| File.exist?(argument) } arguments -= files = Struct.new(*.keys).new(*.values) Struct.new(:arguments, :options, :files, :help).new( arguments, , files, help ) end |