Class: Dorian::Arguments

Inherits:
Object
  • Object
show all
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]
DEFAULT =
nil
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**definition) ⇒ Arguments

Returns a new instance of Arguments.



37
38
39
# File 'lib/dorian/arguments.rb', line 37

def initialize(**definition)
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



11
12
13
# File 'lib/dorian/arguments.rb', line 11

def definition
  @definition
end

Class Method Details

.parseObject



41
42
43
# File 'lib/dorian/arguments.rb', line 41

def self.parse(...)
  new(...).parse
end

Instance Method Details

#cast(type, value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dorian/arguments.rb', line 95

def cast(type, value)
  if value.nil?
    nil
  elsif type == BOOLEAN
    BOOLEANS.fetch(value.downcase)
  elsif type == INTEGER
    value.to_i
  elsif type == DECIMAL || type == NUMBER
    value.to_d
  else
    value
  end
end

#helpObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dorian/arguments.rb', line 109

def help
  message = "USAGE: #{$PROGRAM_NAME}\n"

  message += "\n" if definition.any?

  definition.each do |key, value|
    type, default, aliases = normalize(value)

    keys_message =
      ([key] + aliases)
        .map(&:to_s)
        .map(&:parameterize)
        .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
        .join("|")

    message +=
      "  #{keys_message} #{type.upcase}, default: #{cast(type, default).inspect}\n"
  end

  message
end

#normalize(value) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dorian/arguments.rb', line 131

def normalize(value)
  if value.is_a?(Hash)
    type = value[:type]&.to_s || DEFAULT_TYPE
    default = value[:default]&.to_s || DEFAULT
    aliases =
      Array.wrap(
        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

#parseObject



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
# File 'lib/dorian/arguments.rb', line 45

def parse
  arguments = ARGV
  options = {}
  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 = [default] if values.empty?
    values = values.map { |value| cast(type, value) }
    values = values.first unless values.many?
    options[key] = values
  end

  files = arguments.select { |argument| File.exist?(argument) }

  arguments -= files

  { arguments:, options:, files:, help: }.to_deep_struct
end