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.



32
33
34
# File 'lib/dorian/arguments.rb', line 32

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

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



6
7
8
# File 'lib/dorian/arguments.rb', line 6

def definition
  @definition
end

Class Method Details

.parseObject



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

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

Instance Method Details

#cast(type, value) ⇒ Object



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

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



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

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 { |key| parameterize(key) }
        .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
        .join("|")

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

  message
end

#many?(array) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/dorian/arguments.rb', line 130

def many?(array)
  array.size > 1
end

#normalize(value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dorian/arguments.rb', line 143

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



134
135
136
137
138
139
140
141
# File 'lib/dorian/arguments.rb', line 134

def parameterize(string, separator: "-")
  string
    .to_s
    .encode("ASCII", invalid: :replace, undef: :replace, replace: "")
    .downcase
    .gsub(/[^a-z0-9]+/, separator)
    .gsub(/^#{separator}+|#{separator}+$/, "")
end

#parseObject



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

def parse
  arguments = ARGV
  options = {}
  files = []

  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 =~ 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 many?(values)
    options[key] = values
  end

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

  arguments -= files

  options = Struct.new(*options.keys).new(*options.values)

  Struct.new(:arguments, :options, :files, :help).new(
    arguments, options, files, help
  )
end