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 =
[]
- DEFAULT_TYPE =
BOOLEAN- TYPES =
[BOOLEAN, STRING, NUMBER, INTEGER, DECIMAL]
- DEFAULTS =
{ BOOLEAN => "false", STRING => "", NUMBER => "0", INTEGER => "0", DECIMAL => "0" }
- MATCHES =
{ BOOLEAN => /^(true|false)$/i, STRING => /^.+$/i, NUMBER => /^[0-9.]+$/i, INTEGER => /^[0-9]+$/i, DECIMAL => /^[0-9.]+$/i }
- BOOLEANS =
{ "true" => true, "false" => false }
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.
- #normalize(value) ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(**definition) ⇒ Arguments
Returns a new instance of Arguments.
43 44 45 |
# File 'lib/dorian/arguments.rb', line 43 def initialize(**definition) @definition = definition end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
11 12 13 |
# File 'lib/dorian/arguments.rb', line 11 def definition @definition end |
Class Method Details
.parse ⇒ Object
47 48 49 |
# File 'lib/dorian/arguments.rb', line 47 def self.parse(...) new(...).parse end |
Instance Method Details
#cast(type, value) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dorian/arguments.rb', line 101 def cast(type, value) if type == BOOLEAN BOOLEANS.fetch(value.downcase) elsif type == INTEGER value.to_i elsif type == DECIMAL || type == NUMBER value.to_d else value end end |
#help ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dorian/arguments.rb', line 113 def help = "USAGE: #{$PROGRAM_NAME}\n" += "\n" if definition.any? definition.each do |key, value| type, default, aliases = normalize(value) = ([key] + aliases) .map(&:to_s) .map(&:parameterize) .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" } .join("|") += " #{keys_message} #{type.upcase}, default: #{cast(type, default).inspect}\n" end end |
#normalize(value) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dorian/arguments.rb', line 135 def normalize(value) if value.is_a?(Hash) type = value[:type]&.to_s || DEFAULT_TYPE default = value[:default]&.to_s || DEFAULTS.fetch(type) aliases = Array.wrap( value[:alias]&.to_s || value[:aliases]&.map(&:to_s) || DEFAULT_ALIASES ) else type = value.to_s default = DEFAULTS.fetch(type) aliases = DEFAULT_ALIASES end [type, default, aliases] end |
#parse ⇒ Object
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 97 98 99 |
# File 'lib/dorian/arguments.rb', line 51 def parse arguments = ARGV = {} files = [] definition.each do |key, value| type, default, aliases = normalize(value) keys = ([key] + aliases).map(&:to_s).map(&:parameterize) 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 =~ MATCHES.fetch(type) indexes << index + 1 arguments[index + 1] elsif type == BOOLEAN "true" else default end end end .reject(&:nil?) indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) } values = [DEFAULTS.fetch(type)] if values.empty? values = values.map { |value| cast(type, value) } values = values.first unless values.many? [key] = values end files = arguments.select { |argument| File.exist?(argument) } arguments -= files { arguments:, options:, files:, help: }.to_deep_struct end |