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 =
[].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

Class Method Summary collapse

Instance Method Summary collapse

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

#definitionObject (readonly)

Returns the value of attribute definition.



8
9
10
# File 'lib/dorian/arguments.rb', line 8

def definition
  @definition
end

Class Method Details

.parseObject



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

#helpObject



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
  message = "USAGE: #{File.basename($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)


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

#parseObject



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
  options = {}

  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)
    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