toolrack 0.22.1 → 0.23.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bf5660073953ff0e581655963232f58762e39d46ccd1383885958da071d76e6
4
- data.tar.gz: 6f94d5fe9de1c054d386639e192a60a0d075c34daf46c477936a4fccb7c52a56
3
+ metadata.gz: 7829bd272823dbd7bdf754bca5be1287b074486f99a33ee15bd2607cb391a2f5
4
+ data.tar.gz: ea82bc8cca37d760813fb276e7540df2d80b11624c8e9760d6936cd4c86bf347
5
5
  SHA512:
6
- metadata.gz: f218994963cbdf44dc154b02ebdf4fa011c0f4f2819ab96143d8e14e383aa8688cf775a3db67896110020157253a33ee419e56a6be2649e76c169813af3ce1c4
7
- data.tar.gz: 6acabb8fd0426fde3acaa42b3e1f55c27402480e6dad210ee21ff0f5a9426e07a377b23aeb005da851788a6bec7a1eb057253947d4db2dec76df3121d58c8896
6
+ metadata.gz: 2dc8a2048cb53ea6d6ed2bacaf3cbd7e2a1c8ea8a725c9657b5dd69fdcb49d9f4b6e3aeea12a36772ba54449d3b4cd47bf5be376d77e1b1fb6091ad5f68261d2
7
+ data.tar.gz: 1ceb5fd6b3bdd488b1961bff52c7cce4d4e68f31f842f7029f4eeb73452d52c19a20b772d7ed1a8a11fbb6ffc83dd0474943131d304991e905ac87d97e95a2e1
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- toolrack (0.22.1)
4
+ toolrack (0.23.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- colorize (0.8.1)
9
+ colorize (1.1.0)
10
10
  devops_assist (0.3.11)
11
11
  git_cli
12
12
  git_cli_prompt (~> 0.3.3)
@@ -15,8 +15,7 @@ GEM
15
15
  toolrack
16
16
  tty-prompt
17
17
  diff-lcs (1.5.0)
18
- git_cli (0.11.2)
19
- gvcs
18
+ git_cli (0.12.0)
20
19
  ptools (~> 1.4.0)
21
20
  teLogger
22
21
  toolrack
@@ -24,13 +23,13 @@ GEM
24
23
  teLogger
25
24
  toolrack
26
25
  tty-prompt
27
- gvcs (0.1.1)
28
- minitest (5.18.0)
26
+ gvcs (0.1.2)
27
+ minitest (5.19.0)
29
28
  pastel (0.8.0)
30
29
  tty-color (~> 0.5)
31
30
  ptools (1.4.3)
32
31
  rake (12.3.3)
33
- release-gem (0.1.12)
32
+ release-gem (0.1.25)
34
33
  colorize
35
34
  git_cli
36
35
  gvcs
@@ -42,15 +41,15 @@ GEM
42
41
  rspec-core (~> 3.12.0)
43
42
  rspec-expectations (~> 3.12.0)
44
43
  rspec-mocks (~> 3.12.0)
45
- rspec-core (3.12.1)
44
+ rspec-core (3.12.2)
46
45
  rspec-support (~> 3.12.0)
47
- rspec-expectations (3.12.2)
46
+ rspec-expectations (3.12.3)
48
47
  diff-lcs (>= 1.2.0, < 2.0)
49
48
  rspec-support (~> 3.12.0)
50
- rspec-mocks (3.12.5)
49
+ rspec-mocks (3.12.6)
51
50
  diff-lcs (>= 1.2.0, < 2.0)
52
51
  rspec-support (~> 3.12.0)
53
- rspec-support (3.12.0)
52
+ rspec-support (3.12.1)
54
53
  teLogger (0.2.2)
55
54
  tty-color (0.6.0)
56
55
  tty-command (0.10.1)
@@ -0,0 +1,254 @@
1
+
2
+
3
+ module Antrapol
4
+ module ToolRack
5
+
6
+ module ArgUtils
7
+ include Antrapol::ToolRack::ConditionUtils
8
+
9
+ class ArgParsingException < StandardError; end
10
+ class RequiredFieldEmpty < ArgParsingException; end
11
+ class DuplicatedField < ArgParsingException; end
12
+ class InvalidKey < ArgParsingException; end
13
+
14
+ module ClassMethods
15
+ include Antrapol::ToolRack::ConditionUtils
16
+
17
+ def arg_spec(&block)
18
+ class_eval(&block)
19
+ end
20
+
21
+ def opt(key, desc, opts = {}, &block)
22
+ raise RequiredFieldEmpty, "Key field cannot be empty" if is_empty?(key)
23
+ raise DuplicatedField, "Given key '#{key}' already exist" if options.keys.include?(key)
24
+ raise RequiredFieldEmpty, "Block is require" if not block
25
+
26
+ options[key] = { key: key, desc: desc, options: opts, callback: block }
27
+ required << key if opts[:required] == true
28
+ end
29
+
30
+ def opt_alias(existing, new)
31
+ raise InvalidKey, "Existing key '#{existing}'to map to new key '#{new}' not found" if not options.keys.include?(existing)
32
+ aliases[new] = existing
33
+ end
34
+
35
+ def is_all_required_satisfy?(arr)
36
+ if arr.is_a?(Array)
37
+ res = true
38
+ required.each do |f|
39
+ res = arr.include?(f)
40
+ if not res
41
+ al = aliases.invert[f]
42
+ res = arr.include?(al)
43
+ raise RequiredFieldEmpty, "Required parameter '#{f}' is not given" if not res
44
+ end
45
+ end
46
+ res
47
+
48
+ else
49
+ raise ArgParsingException, "Given array to check for required fields is not an array. Got #{arr}"
50
+ if arr.is_a?(ArgParsingException)
51
+ STDERR.puts "Error throwing into the method was : #{arr.message}"
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ def callback(evt, opts = {}, &block)
58
+ raise ArgParsingException, "Event should not be nil" if evt.nil?
59
+ callbacks[evt] = { opts: opts, cb: block }
60
+ end
61
+
62
+ def required
63
+ if @_req.nil?
64
+ @_req = []
65
+ end
66
+ @_req
67
+ end
68
+
69
+ def options
70
+ if @_options.nil?
71
+ @_options = {}
72
+ end
73
+ @_options
74
+ end
75
+
76
+ def aliases
77
+ if @_aliases.nil?
78
+ @_aliases = {}
79
+ end
80
+ @_aliases
81
+ end
82
+
83
+ def callbacks
84
+ if @_cb.nil?
85
+ @_cb = {}
86
+ end
87
+ @_cb
88
+ end
89
+
90
+ #def value_separator=(val)
91
+ # logger.debug "Setting value separator #{val}"
92
+ # @_valSep = val
93
+ #end
94
+ def set_value_separator(val)
95
+ @_valSep = val
96
+ end
97
+
98
+ def value_separator
99
+ if @_valSep.nil?
100
+ @_valSep = " "
101
+ end
102
+ @_valSep
103
+ end
104
+
105
+ def logger
106
+ Antrapol::ToolRack.logger(:c_arg_utils)
107
+ end
108
+
109
+ end # module ClassMethods
110
+ def self.included(klass)
111
+ klass.extend(ClassMethods)
112
+ end
113
+
114
+ def parse_argv(argv, &block)
115
+ cb = self.class.callbacks[:pre_processing]
116
+ if not_empty?(cb) and cb[:cb]
117
+ #logger.debug "Calling pre-processing for class #{self}"
118
+ @parse_argv_block = block
119
+ # here will engage the DSL method of the included class
120
+ update_argv, val = instance_exec(argv, &cb[:cb])
121
+ #logger.debug "Preprocessing return update flag : #{update_argv} and list #{val} for class #{self}"
122
+ argv = val if update_argv
123
+ end
124
+
125
+ # split the key and value if given
126
+ # > app -n:john
127
+ if self.class.value_separator == " " and argv.length > 0
128
+ parse_argv_space(argv)
129
+ else
130
+ parse_argv_key_value_mixed(argv)
131
+ end
132
+
133
+ cbp = self.class.callbacks[:post_processing]
134
+ if not_empty?(cbp) and cbp[:cb]
135
+ #logger.debug "Post processing got #{argv}"
136
+ instance_exec(argv, &cbp[:cb])
137
+ end
138
+ end
139
+
140
+ private
141
+ def parse_argv_space(argv)
142
+ logger.debug "got argv (space) : #{argv}"
143
+ cls = self.class
144
+ clear = cls.is_all_required_satisfy?(argv)
145
+ logger.debug "All required fields are there? : #{clear}"
146
+ if clear
147
+
148
+ go = true
149
+ i = 0
150
+ while i < argv.length
151
+ a = argv[i]
152
+ logger.debug "Processing : #{a}"
153
+
154
+ key = a
155
+ conf = cls.options[key]
156
+ if is_empty?(conf)
157
+ al = cls.aliases[key]
158
+ conf = cls.options[al] if not_empty?(al)
159
+ end
160
+
161
+ if is_empty?(conf)
162
+ logger.warn "Given key to select ('#{key}') has not defined before"
163
+ # ignore if the given parameter not recognized
164
+ i += 1
165
+ next
166
+ end
167
+
168
+ if not conf[:callback].nil?
169
+ # expecting parameter
170
+ paramsCount = conf[:callback].arity
171
+ if paramsCount > 0
172
+ # look ahead token to load the parameters
173
+ val = []
174
+ (0...paramsCount).each do |ii|
175
+ val << argv[i+1+ii]
176
+ end
177
+ val.delete_if { |e| e.nil? }
178
+ logger.debug "Parameter for callback : #{val}"
179
+ raise RequiredFieldEmpty, "Key '#{a}' requires #{paramsCount} parameter(s) but got #{val.length}" if paramsCount != val.length
180
+ instance_exec(*val, &conf[:callback])
181
+ i += paramsCount+1
182
+ else
183
+ instance_eval(&conf[:callback])
184
+ i += 1
185
+ end
186
+
187
+ else
188
+ i += 1
189
+
190
+ end
191
+
192
+ end # argv.each
193
+
194
+ end # if all required satisfy
195
+
196
+ end
197
+
198
+ def parse_argv_key_value_mixed(argv)
199
+ logger.debug "got argv : #{argv}"
200
+ cls = self.class
201
+ if cls.is_all_required_satisfy?(argv)
202
+
203
+ argv.each do |a|
204
+ logger.debug "Processing : #{a}"
205
+
206
+ key = a
207
+ val = []
208
+ #p a
209
+ #p cls.value_separator
210
+ #p a =~ /#{cls.value_separator}/
211
+ if (a =~ /#{cls.value_separator}/) != nil
212
+ keys = a.split(cls.value_separator)
213
+ key = keys.first
214
+ val = keys[1..-1]
215
+ end
216
+
217
+ logger.debug "After separation : #{key}"
218
+
219
+ conf = cls.options[key]
220
+ if is_empty?(conf)
221
+ al = cls.aliases[key]
222
+ conf = cls.options[al] if not_empty?(al)
223
+ end
224
+
225
+ if is_empty?(conf)
226
+ logger.warn "Given key to select ('#{key}') has not defined before"
227
+ # ignore if the given parameter not recognized
228
+ next
229
+ end
230
+
231
+ if not conf[:callback].nil?
232
+ # expecting parameter
233
+ paramsCount = conf[:callback].arity
234
+ if paramsCount > 0
235
+ raise RequiredFieldEmpty, "Key '#{conf[:key]}' requires #{paramsCount} value(s) to be given but got #{val.length}." if val.length != paramsCount
236
+ instance_exec(*val, &conf[:callback])
237
+ else
238
+ instance_eval(&conf[:callback])
239
+ end
240
+ end
241
+
242
+ end # argv.each
243
+
244
+ end # if all required satisfy
245
+ end
246
+
247
+ def logger
248
+ Antrapol::ToolRack.logger(:arg_utils)
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+ end
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.22.1"
3
+ VERSION = "0.23.2"
4
4
  end
5
5
  end
6
6
 
data/lib/toolrack.rb CHANGED
@@ -19,11 +19,40 @@ require_relative 'toolrack/version_utils'
19
19
  require_relative 'toolrack/file_utils'
20
20
  require_relative 'toolrack/block_params_utils'
21
21
  require_relative 'toolrack/terminal_utils'
22
+ require_relative 'toolrack/arg_utils'
23
+
22
24
 
23
25
  module Antrapol
24
26
  module ToolRack
27
+ include ConditionUtils
28
+
25
29
  class Error < StandardError; end
26
30
  # Your code goes here...
31
+ #
32
+ def self.logger(tag = nil, &block)
33
+ if @_logger.nil?
34
+ @_logger = TeLogger::Tlogger.new
35
+ end
36
+
37
+ if block
38
+ if not_empty?(tag)
39
+ @_logger.with_tag(tag, &block)
40
+ else
41
+ @_logger.with_tag(@_logger.tag, &block)
42
+ end
43
+ else
44
+ if is_empty?(tag)
45
+ @_logger.tag = :tr
46
+ @_logger
47
+ else
48
+ # no block but tag is given? hmm
49
+ @_logger.tag = tag
50
+ @_logger
51
+ end
52
+ end
53
+
54
+ end
55
+
27
56
  end
28
57
  end
29
58
 
@@ -68,3 +97,5 @@ TR::BlockParamsUtils = ToolRack::BlockParamsUtils
68
97
 
69
98
  TR::TerminalUtils = ToolRack::TerminalUtils
70
99
 
100
+ TR::ArgUtils = ToolRack::ArgUtils
101
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.1
4
+ version: 0.23.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devops_assist
@@ -72,6 +72,7 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - lib/toolrack.rb
75
+ - lib/toolrack/arg_utils.rb
75
76
  - lib/toolrack/block_params_utils.rb
76
77
  - lib/toolrack/cli_utils.rb
77
78
  - lib/toolrack/condition_utils.rb
@@ -113,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
114
  - !ruby/object:Gem::Version
114
115
  version: '0'
115
116
  requirements: []
116
- rubygems_version: 3.4.10
117
+ rubygems_version: 3.4.18
117
118
  signing_key:
118
119
  specification_version: 4
119
120
  summary: Collection of simple utilities but I find it increase clarity