dorian-arguments 0.0.1 → 0.2.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 -0
- data/lib/dorian/arguments.rb +65 -42
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3842736e3a26ecd94df067996bdc259b7538ea017c7e4779789f31e6a49adf70
|
4
|
+
data.tar.gz: c1f363f50efb215e6b209b445c178872178dc5b93aae96cf07bd0547f8c938c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bf3bf93d7445af8e4d53fc1b4d22e08fe6d9ce3d7bf57088ee367abcef21f87b305d30d13fa8dae787fe81abd4e8e3583fcdc1565d94b06ca9bcd382a6d756f
|
7
|
+
data.tar.gz: a65fb51190305fd70ccaf392c2967ce9fffc28cd6071d1d4efb709b57182351771d253aa823b9f94064862d5ee97850a39073acd91bbfd3ae221f3b5948e9961
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/dorian/arguments.rb
CHANGED
@@ -2,6 +2,7 @@ require "dorian/to_struct"
|
|
2
2
|
require "bigdecimal"
|
3
3
|
require "active_support"
|
4
4
|
require "active_support/core_ext/string/inflections"
|
5
|
+
require "active_support/core_ext/array/wrap"
|
5
6
|
|
6
7
|
class Dorian
|
7
8
|
class Arguments
|
@@ -17,13 +18,7 @@ class Dorian
|
|
17
18
|
DEFAULT_ALIASES = []
|
18
19
|
DEFAULT_TYPE = BOOLEAN
|
19
20
|
|
20
|
-
TYPES = [
|
21
|
-
BOOLEAN,
|
22
|
-
STRING,
|
23
|
-
NUMBER,
|
24
|
-
INTEGER,
|
25
|
-
DECIMAL
|
26
|
-
]
|
21
|
+
TYPES = [BOOLEAN, STRING, NUMBER, INTEGER, DECIMAL]
|
27
22
|
|
28
23
|
DEFAULTS = {
|
29
24
|
BOOLEAN => "false",
|
@@ -41,12 +36,7 @@ class Dorian
|
|
41
36
|
DECIMAL => /^[0-9.]+$/i
|
42
37
|
}
|
43
38
|
|
44
|
-
BOOLEANS = {
|
45
|
-
"0" => false,
|
46
|
-
"1" => true,
|
47
|
-
"true" => true,
|
48
|
-
"false" => false
|
49
|
-
}
|
39
|
+
BOOLEANS = { "0" => false, "1" => true, "true" => true, "false" => false }
|
50
40
|
|
51
41
|
def initialize(**definition)
|
52
42
|
@definition = definition
|
@@ -62,46 +52,40 @@ class Dorian
|
|
62
52
|
files = []
|
63
53
|
|
64
54
|
definition.each do |key, value|
|
65
|
-
|
66
|
-
type = value[:type] || DEFAULT_TYPE
|
67
|
-
default = value[:default] || DEFAULTS.fetch(type)
|
68
|
-
aliases = value[:alias] || value[:aliases] || DEFAULT_ALIASES
|
69
|
-
else
|
70
|
-
type = value
|
71
|
-
default = DEFAULTS.fetch(type)
|
72
|
-
aliases = DEFAULT_ALIASES
|
73
|
-
end
|
55
|
+
type, default, aliases = normalize(value)
|
74
56
|
|
75
57
|
keys = ([key] + aliases).map(&:to_s).map(&:parameterize)
|
76
58
|
keys = keys.map { |key| ["--#{key}", "-#{key}"] }.flatten
|
77
59
|
|
78
60
|
indexes = []
|
79
61
|
|
80
|
-
values =
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
62
|
+
values =
|
63
|
+
arguments
|
64
|
+
.map
|
65
|
+
.with_index do |argument, index|
|
66
|
+
if keys.include?(argument.split("=").first)
|
67
|
+
indexes << index
|
68
|
+
|
69
|
+
if argument.include?("=")
|
70
|
+
argument.split("=", 2).last
|
71
|
+
elsif arguments[index + 1].to_s =~ MATCHES.fetch(type)
|
72
|
+
indexes << index + 1
|
73
|
+
|
74
|
+
arguments[index + 1]
|
75
|
+
elsif type == BOOLEAN
|
76
|
+
"true"
|
77
|
+
else
|
78
|
+
default
|
79
|
+
end
|
80
|
+
end
|
94
81
|
end
|
95
|
-
|
96
|
-
end.reject(&:nil?)
|
82
|
+
.reject(&:nil?)
|
97
83
|
|
98
84
|
if type == BOOLEAN
|
99
85
|
values = values.map { |value| BOOLEANS.fetch(value.downcase) }
|
100
86
|
end
|
101
87
|
|
102
|
-
if type == INTEGER
|
103
|
-
values = values.map { |value| value.to_i }
|
104
|
-
end
|
88
|
+
values = values.map { |value| value.to_i } if type == INTEGER
|
105
89
|
|
106
90
|
if type == DECIMAL || type == NUMBER
|
107
91
|
values = values.map { |value| BigDecimal(value) }
|
@@ -119,7 +103,46 @@ class Dorian
|
|
119
103
|
|
120
104
|
arguments -= files
|
121
105
|
|
122
|
-
|
106
|
+
{
|
107
|
+
arguments:,
|
108
|
+
options:,
|
109
|
+
files:,
|
110
|
+
help:
|
111
|
+
}.to_deep_struct
|
112
|
+
end
|
113
|
+
|
114
|
+
def help
|
115
|
+
message = "USAGE: #{$PROGRAM_NAME}\n"
|
116
|
+
|
117
|
+
message += "\n" if definition.any?
|
118
|
+
|
119
|
+
definition.each do |key, value|
|
120
|
+
type, default, aliases = normalize(value)
|
121
|
+
|
122
|
+
keys_message = ([key] + aliases).map(&:to_s).map(&:parameterize).map do |key|
|
123
|
+
key.size == 1 ? "-#{key}" : "--#{key}"
|
124
|
+
end.join("|")
|
125
|
+
|
126
|
+
message += " #{keys_message} #{type.upcase}, default: #{default}\n"
|
127
|
+
end
|
128
|
+
|
129
|
+
message
|
130
|
+
end
|
131
|
+
|
132
|
+
def normalize(value)
|
133
|
+
if value.is_a?(Hash)
|
134
|
+
type = value[:type]&.to_s || DEFAULT_TYPE
|
135
|
+
default = value[:default]&.to_s || DEFAULTS.fetch(type)
|
136
|
+
aliases = Array.wrap(
|
137
|
+
value[:alias]&.to_s || value[:aliases]&.map(&:to_s) || DEFAULT_ALIASES
|
138
|
+
)
|
139
|
+
else
|
140
|
+
type = value.to_s
|
141
|
+
default = DEFAULTS.fetch(type)
|
142
|
+
aliases = DEFAULT_ALIASES
|
143
|
+
end
|
144
|
+
|
145
|
+
[type, default, aliases]
|
123
146
|
end
|
124
147
|
end
|
125
148
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dorian-arguments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dorian Marié
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -58,6 +58,7 @@ executables: []
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
+
- VERSION
|
61
62
|
- lib/dorian-arguments.rb
|
62
63
|
- lib/dorian/arguments.rb
|
63
64
|
homepage: https://github.com/dorianmariecom/dorian-arguments
|