dorian-arguments 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/dorian/arguments.rb +30 -26
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6e446f6aaac5736fad5f8f34cf06f47807106bfcf2c2a6bc9f4c947a5b30b5d
|
4
|
+
data.tar.gz: 3afa1cea746fb8a29c4178d476848e9ea26275d6cee249700effd91c2934091a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aca7cbf836751411ecabf3496a0ec0419367d82293cac10e5d95193b011229a4fe3531d307722d2edfdc6333690038f4258c57579ba1b78bebd80a12b9962941
|
7
|
+
data.tar.gz: b75f7bab12e64f7727727163c11cbc3eb12e6a1e8e24e6b6fef49ec22f14ac721a47838815c3164d2a36a5505991220c080baa64924d69a35890519c63d098b9
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/dorian/arguments.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require "dorian/to_struct"
|
2
2
|
require "bigdecimal"
|
3
|
+
require "bigdecimal/util"
|
3
4
|
require "active_support"
|
4
5
|
require "active_support/core_ext/string/inflections"
|
5
6
|
require "active_support/core_ext/array/wrap"
|
7
|
+
require "active_support/core_ext/enumerable"
|
6
8
|
|
7
9
|
class Dorian
|
8
10
|
class Arguments
|
@@ -81,21 +83,11 @@ class Dorian
|
|
81
83
|
end
|
82
84
|
.reject(&:nil?)
|
83
85
|
|
84
|
-
if type == BOOLEAN
|
85
|
-
values = values.map { |value| BOOLEANS.fetch(value.downcase) }
|
86
|
-
end
|
87
|
-
|
88
|
-
values = values.map { |value| value.to_i } if type == INTEGER
|
89
|
-
|
90
|
-
if type == DECIMAL || type == NUMBER
|
91
|
-
values = values.map { |value| BigDecimal(value) }
|
92
|
-
end
|
93
|
-
|
94
|
-
values = values.first if values.size < 2
|
95
|
-
values || BOOLEANS.fetch(DEFAULTS.fetch(type)) if type == BOOLEAN
|
96
|
-
|
97
86
|
indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) }
|
98
87
|
|
88
|
+
values = [DEFAULTS.fetch(type)] if values.empty?
|
89
|
+
values = values.map { |value| cast(type, value) }
|
90
|
+
values = values.first unless values.many?
|
99
91
|
options[key] = values
|
100
92
|
end
|
101
93
|
|
@@ -103,12 +95,19 @@ class Dorian
|
|
103
95
|
|
104
96
|
arguments -= files
|
105
97
|
|
106
|
-
{
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
98
|
+
{ arguments:, options:, files:, help: }.to_deep_struct
|
99
|
+
end
|
100
|
+
|
101
|
+
def cast(type, value)
|
102
|
+
if type == BOOLEAN
|
103
|
+
BOOLEANS.fetch(value.downcase)
|
104
|
+
elsif type == INTEGER
|
105
|
+
value.to_i
|
106
|
+
elsif type == DECIMAL || type == NUMBER
|
107
|
+
value.to_d
|
108
|
+
else
|
109
|
+
value
|
110
|
+
end
|
112
111
|
end
|
113
112
|
|
114
113
|
def help
|
@@ -119,11 +118,14 @@ class Dorian
|
|
119
118
|
definition.each do |key, value|
|
120
119
|
type, default, aliases = normalize(value)
|
121
120
|
|
122
|
-
keys_message =
|
123
|
-
key
|
124
|
-
|
121
|
+
keys_message =
|
122
|
+
([key] + aliases)
|
123
|
+
.map(&:to_s)
|
124
|
+
.map(&:parameterize)
|
125
|
+
.map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
|
126
|
+
.join("|")
|
125
127
|
|
126
|
-
message += " #{keys_message} #{type.upcase}, default: #{default}\n"
|
128
|
+
message += " #{keys_message} #{type.upcase}, default: #{cast(type, default).inspect}\n"
|
127
129
|
end
|
128
130
|
|
129
131
|
message
|
@@ -133,9 +135,11 @@ class Dorian
|
|
133
135
|
if value.is_a?(Hash)
|
134
136
|
type = value[:type]&.to_s || DEFAULT_TYPE
|
135
137
|
default = value[:default]&.to_s || DEFAULTS.fetch(type)
|
136
|
-
aliases =
|
137
|
-
|
138
|
-
|
138
|
+
aliases =
|
139
|
+
Array.wrap(
|
140
|
+
value[:alias]&.to_s || value[:aliases]&.map(&:to_s) ||
|
141
|
+
DEFAULT_ALIASES
|
142
|
+
)
|
139
143
|
else
|
140
144
|
type = value.to_s
|
141
145
|
default = DEFAULTS.fetch(type)
|