dorian-arguments 0.0.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -0
  3. data/lib/dorian/arguments.rb +80 -53
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 951dc350b8ef2808b4d70b61ad7febaf0ebee1cd72d86407e5546d6635178bee
4
- data.tar.gz: 19eab1e2e8aa96e4f5bc00cd416d52c7bcc17b108b05f22c2b75d09477c82c4d
3
+ metadata.gz: e6e446f6aaac5736fad5f8f34cf06f47807106bfcf2c2a6bc9f4c947a5b30b5d
4
+ data.tar.gz: 3afa1cea746fb8a29c4178d476848e9ea26275d6cee249700effd91c2934091a
5
5
  SHA512:
6
- metadata.gz: 3a070d3878e7823609ce007d8f53e7b2efde25a4780a1665aec50b625ae1ef6bb42efa8830d7785c783e396cac5a888b72da68e71d828eee82103594bd44fb33
7
- data.tar.gz: 5df10a59d4f6ff04bcc287cbdaa28fc35f90498c4d6a817cb40ead81efa2e69ed8acd50e751be2374a4372d82f4e23c520e28c4ea3403de4c39b9fee37087e35
6
+ metadata.gz: aca7cbf836751411ecabf3496a0ec0419367d82293cac10e5d95193b011229a4fe3531d307722d2edfdc6333690038f4258c57579ba1b78bebd80a12b9962941
7
+ data.tar.gz: b75f7bab12e64f7727727163c11cbc3eb12e6a1e8e24e6b6fef49ec22f14ac721a47838815c3164d2a36a5505991220c080baa64924d69a35890519c63d098b9
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -1,7 +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"
6
+ require "active_support/core_ext/array/wrap"
7
+ require "active_support/core_ext/enumerable"
5
8
 
6
9
  class Dorian
7
10
  class Arguments
@@ -17,13 +20,7 @@ class Dorian
17
20
  DEFAULT_ALIASES = []
18
21
  DEFAULT_TYPE = BOOLEAN
19
22
 
20
- TYPES = [
21
- BOOLEAN,
22
- STRING,
23
- NUMBER,
24
- INTEGER,
25
- DECIMAL
26
- ]
23
+ TYPES = [BOOLEAN, STRING, NUMBER, INTEGER, DECIMAL]
27
24
 
28
25
  DEFAULTS = {
29
26
  BOOLEAN => "false",
@@ -41,12 +38,7 @@ class Dorian
41
38
  DECIMAL => /^[0-9.]+$/i
42
39
  }
43
40
 
44
- BOOLEANS = {
45
- "0" => false,
46
- "1" => true,
47
- "true" => true,
48
- "false" => false
49
- }
41
+ BOOLEANS = { "0" => false, "1" => true, "true" => true, "false" => false }
50
42
 
51
43
  def initialize(**definition)
52
44
  @definition = definition
@@ -62,64 +54,99 @@ class Dorian
62
54
  files = []
63
55
 
64
56
  definition.each do |key, value|
65
- if value.is_a?(Hash)
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
57
+ type, default, aliases = normalize(value)
74
58
 
75
59
  keys = ([key] + aliases).map(&:to_s).map(&:parameterize)
76
60
  keys = keys.map { |key| ["--#{key}", "-#{key}"] }.flatten
77
61
 
78
62
  indexes = []
79
63
 
80
- values = arguments.map.with_index do |argument, index|
81
- if keys.include?(argument.split("=").first)
82
- indexes << index
64
+ values =
65
+ arguments
66
+ .map
67
+ .with_index do |argument, index|
68
+ if keys.include?(argument.split("=").first)
69
+ indexes << index
70
+
71
+ if argument.include?("=")
72
+ argument.split("=", 2).last
73
+ elsif arguments[index + 1].to_s =~ MATCHES.fetch(type)
74
+ indexes << index + 1
75
+
76
+ arguments[index + 1]
77
+ elsif type == BOOLEAN
78
+ "true"
79
+ else
80
+ default
81
+ end
82
+ end
83
+ end
84
+ .reject(&:nil?)
83
85
 
84
- if argument.include?("=")
85
- argument.split("=", 2).last
86
- elsif arguments[index + 1].to_s =~ MATCHES.fetch(type)
87
- indexes << index + 1
86
+ indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) }
88
87
 
89
- arguments[index + 1]
90
- elsif type == BOOLEAN
91
- "true"
92
- else
93
- default
94
- end
95
- end
96
- end.reject(&:nil?)
88
+ values = [DEFAULTS.fetch(type)] if values.empty?
89
+ values = values.map { |value| cast(type, value) }
90
+ values = values.first unless values.many?
91
+ options[key] = values
92
+ end
97
93
 
98
- if type == BOOLEAN
99
- values = values.map { |value| BOOLEANS.fetch(value.downcase) }
100
- end
94
+ files = arguments.select { |argument| File.exist?(argument) }
101
95
 
102
- if type == INTEGER
103
- values = values.map { |value| value.to_i }
104
- end
96
+ arguments -= files
105
97
 
106
- if type == DECIMAL || type == NUMBER
107
- values = values.map { |value| BigDecimal(value) }
108
- end
98
+ { arguments:, options:, files:, help: }.to_deep_struct
99
+ end
109
100
 
110
- values = values.first if values.size < 2
111
- values || BOOLEANS.fetch(DEFAULTS.fetch(type)) if type == BOOLEAN
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
111
+ end
112
112
 
113
- indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) }
113
+ def help
114
+ message = "USAGE: #{$PROGRAM_NAME}\n"
114
115
 
115
- options[key] = values
116
+ message += "\n" if definition.any?
117
+
118
+ definition.each do |key, value|
119
+ type, default, aliases = normalize(value)
120
+
121
+ keys_message =
122
+ ([key] + aliases)
123
+ .map(&:to_s)
124
+ .map(&:parameterize)
125
+ .map { |key| key.size == 1 ? "-#{key}" : "--#{key}" }
126
+ .join("|")
127
+
128
+ message += " #{keys_message} #{type.upcase}, default: #{cast(type, default).inspect}\n"
116
129
  end
117
130
 
118
- files = arguments.select { |argument| File.exist?(argument) }
131
+ message
132
+ end
119
133
 
120
- arguments -= files
134
+ def normalize(value)
135
+ if value.is_a?(Hash)
136
+ type = value[:type]&.to_s || DEFAULT_TYPE
137
+ default = value[:default]&.to_s || DEFAULTS.fetch(type)
138
+ aliases =
139
+ Array.wrap(
140
+ value[:alias]&.to_s || value[:aliases]&.map(&:to_s) ||
141
+ DEFAULT_ALIASES
142
+ )
143
+ else
144
+ type = value.to_s
145
+ default = DEFAULTS.fetch(type)
146
+ aliases = DEFAULT_ALIASES
147
+ end
121
148
 
122
- [arguments, options.to_struct, files]
149
+ [type, default, aliases]
123
150
  end
124
151
  end
125
152
  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.1
4
+ version: 0.3.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-19 00:00:00.000000000 Z
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