argvise 0.0.7 → 0.0.9
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 +4 -4
- data/.rubocop.yml +30 -0
- data/bin/console.rb +1 -0
- data/bin/irb.rb +10 -0
- data/docs/Readme.md +77 -16
- data/lib/{core.rb → argvise/core.rb} +83 -33
- data/lib/argvise/refinement.rb +37 -0
- data/lib/argvise/version.rb +1 -1
- data/lib/argvise.rb +2 -1
- data/rbi/lib/argvise.rbi +5 -2
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e698d11b5f582bd557fd6f4d1a1d2d9f9cda5512d2ba26af3e59d803016093a8
|
|
4
|
+
data.tar.gz: 182e7a4cad5e576575335d22413eac4868c8ab7f993c17d9f344f898d8e86291
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 24c9621fa200763a833f02bb019026b8a75b49f0948f88cb765ba38196bd5f1c4406313f8f5d837b70da259cf241a248bee41040615bb7ab354b7e7335ebefdd
|
|
7
|
+
data.tar.gz: 44e70c7e0b19e25e180e21851b0582e9e3ca1507aab3de9168abd24dff8f0d785087b5a302294046a5a366e6add6a8b3e98579d2573c7a0ed6b1146810aeaed2
|
data/.rubocop.yml
CHANGED
|
@@ -1,2 +1,32 @@
|
|
|
1
|
+
# ln ~/.config/rubocop/config.yml .rubocop.yml
|
|
1
2
|
AllCops:
|
|
2
3
|
TargetRubyVersion: 3.1
|
|
4
|
+
|
|
5
|
+
Lint/MissingCopEnableDirective:
|
|
6
|
+
Enabled: false
|
|
7
|
+
Lint/RedundantCopDisableDirective:
|
|
8
|
+
Enabled: false
|
|
9
|
+
Lint/EmptyExpression:
|
|
10
|
+
# disabled => allow ()
|
|
11
|
+
Enabled: false
|
|
12
|
+
|
|
13
|
+
# https://docs.rubocop.org/rubocop/cops_style.html
|
|
14
|
+
Style/TrailingCommaInHashLiteral:
|
|
15
|
+
EnforcedStyle: diff_comma
|
|
16
|
+
Style/TrailingCommaInArrayLiteral:
|
|
17
|
+
EnforcedStyle: diff_comma
|
|
18
|
+
Style/Lambda:
|
|
19
|
+
EnforcedStyle: literal
|
|
20
|
+
Style/ModuleFunction:
|
|
21
|
+
EnforcedStyle: extend_self
|
|
22
|
+
Style/BlockDelimiters:
|
|
23
|
+
Enabled: false
|
|
24
|
+
Style/Documentation:
|
|
25
|
+
Enabled: false
|
|
26
|
+
|
|
27
|
+
# https://docs.rubocop.org/rubocop/cops_layout.html
|
|
28
|
+
Layout/CaseIndentation:
|
|
29
|
+
EnforcedStyle: end
|
|
30
|
+
IndentOneStep: true
|
|
31
|
+
Layout/MultilineMethodCallIndentation:
|
|
32
|
+
EnforcedStyle: indented_relative_to_receiver
|
data/bin/console.rb
CHANGED
data/bin/irb.rb
ADDED
data/docs/Readme.md
CHANGED
|
@@ -23,6 +23,9 @@ raw_cmd_hash = {
|
|
|
23
23
|
cargo: (), b: (), r: true, target: "wasm32-wasip2"
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
using Argvise::HashRefin
|
|
27
|
+
# OR: include Argvise::HashMixin
|
|
28
|
+
|
|
26
29
|
raw_cmd_hash
|
|
27
30
|
.to_argv
|
|
28
31
|
# .to_argv({bsd_style: false, kebab_case_flags: true})
|
|
@@ -158,8 +161,8 @@ gem install argvise
|
|
|
158
161
|
require 'argvise'
|
|
159
162
|
|
|
160
163
|
raw_cmd_hash = {
|
|
161
|
-
docker:
|
|
162
|
-
build:
|
|
164
|
+
docker: (), #=> docker
|
|
165
|
+
build: (),
|
|
163
166
|
push: true, #=> --push
|
|
164
167
|
tag: ["ghcr.io/[user]/repo:latest", "ghcr.io/[user]/repo:v0.0.1"], #=> --tag ghcr... --tag ghcr..0.0.1
|
|
165
168
|
platform: "wasi/wasm", #=> --platform wasi/wasm
|
|
@@ -168,7 +171,7 @@ raw_cmd_hash = {
|
|
|
168
171
|
description: "A Docker build example"
|
|
169
172
|
}, # => --label maintainer=user --label description=A..example
|
|
170
173
|
file: "wasi.dockerfile",
|
|
171
|
-
path:
|
|
174
|
+
path: (),
|
|
172
175
|
}
|
|
173
176
|
|
|
174
177
|
Argvise.build(raw_cmd_hash)
|
|
@@ -181,18 +184,59 @@ Argvise.build(raw_cmd_hash)
|
|
|
181
184
|
# ]
|
|
182
185
|
```
|
|
183
186
|
|
|
184
|
-
###
|
|
187
|
+
### Shortcut
|
|
188
|
+
|
|
189
|
+
#### Mixin
|
|
185
190
|
|
|
186
191
|
```ruby
|
|
187
|
-
|
|
188
|
-
|
|
192
|
+
require 'argvise'
|
|
193
|
+
|
|
194
|
+
module A
|
|
195
|
+
module_function
|
|
196
|
+
include Argvise::HashMixin
|
|
197
|
+
|
|
198
|
+
def demo
|
|
199
|
+
{ path: '/path/to/dir' }.to_argv.then { p it }
|
|
200
|
+
#=> ["--path", "/path/to/dir"]
|
|
201
|
+
|
|
202
|
+
{ path: '/path/to/dir' }.to_argv_bsd.then { p it }
|
|
203
|
+
#=> ["-path", "/path/to/dir"]
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
A.demo
|
|
208
|
+
Hash.method_defined?(:to_argv) # => true
|
|
209
|
+
{}.respond_to?(:to_argv) #=> true
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### Refinement
|
|
213
|
+
|
|
214
|
+
```ruby
|
|
215
|
+
require 'argvise'
|
|
216
|
+
class A
|
|
217
|
+
using Argvise::HashRefin
|
|
218
|
+
|
|
219
|
+
def self.demo
|
|
220
|
+
{ target: "wasm32-wasip2" }.to_argv.then { p it }
|
|
221
|
+
# => ["--target", "wasm32-wasip2"]
|
|
222
|
+
|
|
223
|
+
{ target: "wasm32-wasip2" }.to_argv_bsd.then { p it }
|
|
224
|
+
# => ["-target", "wasm32-wasip2"]
|
|
225
|
+
|
|
226
|
+
{}.respond_to?(:to_argv).then { p it } #=> true
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
A.demo
|
|
231
|
+
Hash.method_defined?(:to_argv) # => false
|
|
232
|
+
{}.respond_to?(:to_argv) #=> false
|
|
189
233
|
```
|
|
190
234
|
|
|
191
235
|
### Configurable builder
|
|
192
236
|
|
|
193
237
|
> Required
|
|
194
238
|
>
|
|
195
|
-
> - argvise: >= v0.0.
|
|
239
|
+
> - argvise: >= v0.0.9
|
|
196
240
|
> - ruby: >= v3.1.0
|
|
197
241
|
|
|
198
242
|
```ruby
|
|
@@ -214,6 +258,7 @@ raw_cmd = {
|
|
|
214
258
|
|
|
215
259
|
p '----------------'
|
|
216
260
|
p "GNU-style + kebab case flags=false"
|
|
261
|
+
# argvise: >= v0.0.4
|
|
217
262
|
raw_cmd
|
|
218
263
|
.then(&Argvise.new_proc)
|
|
219
264
|
.with_bsd_style(false)
|
|
@@ -224,7 +269,12 @@ raw_cmd
|
|
|
224
269
|
#=> ["compiler", "build", "--pack_type", "tar+zstd", "--push", "-v", "-f", "p2", "--tag", "v0.0.1", "--tag", "beta", "--platform", "wasi/wasm", "--label", "maintainer=user", "--label", "description=Demo", "/path/to/dir"]
|
|
225
270
|
|
|
226
271
|
p '----------------'
|
|
272
|
+
|
|
273
|
+
# argvise: >= v0.0.9
|
|
274
|
+
using Argvise::HashRefin
|
|
275
|
+
|
|
227
276
|
p 'GNU-style + kebab-case-flags=true'
|
|
277
|
+
# argvise: >= v0.0.6
|
|
228
278
|
raw_cmd
|
|
229
279
|
.to_argv
|
|
230
280
|
.display
|
|
@@ -233,14 +283,13 @@ raw_cmd
|
|
|
233
283
|
|
|
234
284
|
p '----------------'
|
|
235
285
|
p 'BSD-style + kebab-case-flags=true'
|
|
286
|
+
# argvise: >= v0.0.9
|
|
236
287
|
raw_cmd
|
|
237
|
-
.
|
|
238
|
-
.with_bsd_style
|
|
239
|
-
.with_kebab_case_flags
|
|
240
|
-
.build
|
|
288
|
+
.to_argv_bsd
|
|
241
289
|
.display
|
|
242
290
|
|
|
243
291
|
#=> ["compiler", "build", "-pack-type", "tar+zstd", "-push", "-v", "-f", "p2", "-tag", "v0.0.1", "-tag", "beta", "-platform", "wasi/wasm", "-label", "maintainer=user", "-label", "description=Demo", "/path/to/dir"]
|
|
292
|
+
p '----------------'
|
|
244
293
|
```
|
|
245
294
|
|
|
246
295
|
## Data Type
|
|
@@ -299,15 +348,27 @@ raw_cmd
|
|
|
299
348
|
|
|
300
349
|
- `{ cargo: () }` => `["cargo"]`
|
|
301
350
|
- `{ cargo: nil, b: nil }` => `["cargo", "b"]`
|
|
302
|
-
- `{ "-fv":
|
|
351
|
+
- `{ "-fv": () }` => `["-fv"]`
|
|
303
352
|
|
|
304
353
|
## Changelog
|
|
305
354
|
|
|
306
355
|
### v0.0.6 (2025-11-05)
|
|
307
356
|
|
|
308
|
-
Breaking
|
|
357
|
+
Breaking Change:
|
|
309
358
|
|
|
310
359
|
- `cmd_hash |> hash_to_argv` => `cmd_hash.to_argv(opts)`
|
|
311
|
-
-
|
|
312
|
-
|
|
313
|
-
|
|
360
|
+
- Previous: `{a: true}.then(&hash_to_argv)`
|
|
361
|
+
- Current: `{a: true}.to_argv`
|
|
362
|
+
|
|
363
|
+
### v0.0.9 (2025-11-30)
|
|
364
|
+
|
|
365
|
+
- add refinements for Hash
|
|
366
|
+
- add `Hash#to_argv_bsd`
|
|
367
|
+
|
|
368
|
+
Breaking Change:
|
|
369
|
+
- Mitigated side effects introduced by monkey patching.
|
|
370
|
+
- Previous:
|
|
371
|
+
- Simply calling `require 'argvise'` was enough to import `Hash#to_argv`;
|
|
372
|
+
- no explicit `include` or `using` was required.
|
|
373
|
+
- Current:
|
|
374
|
+
- We must manually `include Argvise::HashMixin` or `using Argvise::HashRefin` to import `Hash#to_argv`.
|
|
@@ -216,21 +216,21 @@ class Argvise
|
|
|
216
216
|
# Generates the corresponding argument array based on the value type
|
|
217
217
|
def generate_args(flag, value)
|
|
218
218
|
case value
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
219
|
+
when true
|
|
220
|
+
[flag]
|
|
221
|
+
when Array
|
|
222
|
+
expand_array(flag, value)
|
|
223
|
+
when Hash
|
|
224
|
+
expand_hash(flag, value)
|
|
225
|
+
else
|
|
226
|
+
# e.g., {tag: 'uuu'} => ["--tag", "uuu"]
|
|
227
|
+
[flag, value.to_s]
|
|
228
228
|
end
|
|
229
229
|
end
|
|
230
230
|
|
|
231
|
-
#
|
|
232
|
-
#
|
|
233
|
-
#
|
|
231
|
+
# {tag: ["v1", "v2"]}
|
|
232
|
+
# => (flag: "--tag", array: ['v1', 'v2'])
|
|
233
|
+
# => ["--tag", "v1", "--tag", "v2"]
|
|
234
234
|
def expand_array(flag, array)
|
|
235
235
|
# FP style: array.flat_map { |v| [flag, v.to_s] }
|
|
236
236
|
array.each_with_object([]) do |v, memo|
|
|
@@ -241,9 +241,9 @@ class Argvise
|
|
|
241
241
|
|
|
242
242
|
# Processes hash values (generates key=value format)
|
|
243
243
|
#
|
|
244
|
-
#
|
|
245
|
-
#
|
|
246
|
-
#
|
|
244
|
+
# {label: { env: "test", key: "value" }}
|
|
245
|
+
# => (flag: "--label", hash)
|
|
246
|
+
# => ["--label", "env=test", "--label", "key=value"]
|
|
247
247
|
def expand_hash(flag, hash)
|
|
248
248
|
# hash.flat_map { |k, v| [flag, "#{k}=#{v}"] }
|
|
249
249
|
hash.each_with_object([]) do |(k, v), memo|
|
|
@@ -253,30 +253,80 @@ class Argvise
|
|
|
253
253
|
end
|
|
254
254
|
end
|
|
255
255
|
|
|
256
|
-
class
|
|
257
|
-
|
|
256
|
+
class Argvise
|
|
257
|
+
module HashExt # rubocop:disable Style/Documentation
|
|
258
|
+
# Converts a hash map into GNU-style command-line arguments.
|
|
259
|
+
#
|
|
260
|
+
# == Example:
|
|
261
|
+
#
|
|
262
|
+
# require 'argvise'
|
|
263
|
+
# using Argvise::HashRefin
|
|
264
|
+
#
|
|
265
|
+
# { v: true, path: '/path/to/dir' }.to_argv
|
|
266
|
+
# #=> ["-v", "--path", "/path/to/dir"]
|
|
267
|
+
#
|
|
268
|
+
# == params:
|
|
269
|
+
#
|
|
270
|
+
# - opts: See also [Argvise::new]
|
|
271
|
+
#
|
|
272
|
+
# == raw_cmd_hash.to_argv is equivalent to:
|
|
273
|
+
#
|
|
274
|
+
# raw_cmd_hash
|
|
275
|
+
# .then(&Argvise.new_proc)
|
|
276
|
+
# .with_bsd_style(false)
|
|
277
|
+
# .with_kebab_case_flags(true)
|
|
278
|
+
# .build
|
|
279
|
+
#
|
|
280
|
+
# ---
|
|
281
|
+
# sig { params(opts: T.nilable(Hash)).returns(T::Array[String]) }
|
|
282
|
+
def to_argv(opts = nil)
|
|
283
|
+
Argvise.build(self, opts:)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Converts a hash map into BSD-style command-line arguments.
|
|
287
|
+
#
|
|
288
|
+
# == Example:
|
|
289
|
+
#
|
|
290
|
+
# require 'argvise'
|
|
291
|
+
# using Argvise::HashRefin
|
|
292
|
+
#
|
|
293
|
+
# { path: '/path/to/dir' }.to_argv_bsd
|
|
294
|
+
# #=> ["-path", "/path/to/dir"]
|
|
295
|
+
#
|
|
296
|
+
def to_argv_bsd(options = {})
|
|
297
|
+
# if options is not Hash Type => {}
|
|
298
|
+
options = {} unless options.is_a?(::Hash)
|
|
299
|
+
|
|
300
|
+
opts = options.merge({ bsd_style: true })
|
|
301
|
+
Argvise.build(self, opts:)
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Converts a hash map into command-line arguments.
|
|
258
306
|
#
|
|
259
307
|
# == Example:
|
|
260
308
|
#
|
|
261
|
-
#
|
|
262
|
-
# #=> ["-v", "--path", "/path/to/dir"]
|
|
263
|
-
#
|
|
264
|
-
# == params:
|
|
309
|
+
# require 'argvise'
|
|
265
310
|
#
|
|
266
|
-
#
|
|
311
|
+
# module A
|
|
312
|
+
# module_function
|
|
313
|
+
# include Argvise::HashMixin
|
|
267
314
|
#
|
|
268
|
-
#
|
|
315
|
+
# def demo
|
|
316
|
+
# puts({ path: '/path/to/dir' }.to_argv)
|
|
317
|
+
# #=> ["--path", "/path/to/dir"]
|
|
269
318
|
#
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
#
|
|
273
|
-
#
|
|
274
|
-
# .build
|
|
319
|
+
# puts({ path: '/path/to/dir' }.to_argv_bsd)
|
|
320
|
+
# #=> ["-path", "/path/to/dir"]
|
|
321
|
+
# end
|
|
322
|
+
# end
|
|
275
323
|
#
|
|
276
|
-
#
|
|
277
|
-
#
|
|
278
|
-
#
|
|
279
|
-
|
|
280
|
-
|
|
324
|
+
# A.demo
|
|
325
|
+
# Hash.method_defined?(:to_argv) # => true
|
|
326
|
+
# {}.respond_to?(:to_argv) #=> true
|
|
327
|
+
module HashMixin
|
|
328
|
+
def self.included(_host)
|
|
329
|
+
::Hash.include(HashExt)
|
|
330
|
+
end
|
|
281
331
|
end
|
|
282
332
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# To maintain `mruby` compatibility, define `private_constant` and
|
|
5
|
+
# `refinements` in this file rather than in **core.rb**.
|
|
6
|
+
|
|
7
|
+
class Argvise
|
|
8
|
+
private_constant :HashExt
|
|
9
|
+
|
|
10
|
+
# Refinements: Hash#to_argv, Hash#to_argv_bsd
|
|
11
|
+
#
|
|
12
|
+
# = Example
|
|
13
|
+
#
|
|
14
|
+
# require 'argvise'
|
|
15
|
+
# class A
|
|
16
|
+
# using Argvise::HashRefin
|
|
17
|
+
# def self.demo
|
|
18
|
+
# puts({ target: "wasm32-wasip2" }.to_argv)
|
|
19
|
+
# # => ["--target", "wasm32-wasip2"]
|
|
20
|
+
#
|
|
21
|
+
# puts({ target: "wasm32-wasip2" }.to_argv_bsd)
|
|
22
|
+
# # => ["-target", "wasm32-wasip2"]
|
|
23
|
+
#
|
|
24
|
+
# {}.respond_to?(:to_argv) #=> true
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# A.demo
|
|
29
|
+
# Hash.method_defined?(:to_argv) # => false
|
|
30
|
+
# {}.respond_to?(:to_argv) #=> false
|
|
31
|
+
#
|
|
32
|
+
module HashRefin
|
|
33
|
+
refine ::Hash do
|
|
34
|
+
import_methods HashExt
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
data/lib/argvise/version.rb
CHANGED
data/lib/argvise.rb
CHANGED
data/rbi/lib/argvise.rbi
CHANGED
|
@@ -6,7 +6,7 @@ class Argvise
|
|
|
6
6
|
class << self
|
|
7
7
|
sig do
|
|
8
8
|
params(
|
|
9
|
-
raw_cmd_hash: Hash,
|
|
9
|
+
raw_cmd_hash: T.any(Hash, HashExt),
|
|
10
10
|
opts: T.nilable(T::Hash[Symbol, T::Boolean])
|
|
11
11
|
).returns(T::Array[String])
|
|
12
12
|
end
|
|
@@ -15,7 +15,7 @@ class Argvise
|
|
|
15
15
|
|
|
16
16
|
sig do
|
|
17
17
|
params(
|
|
18
|
-
raw_cmd_hash: Hash,
|
|
18
|
+
raw_cmd_hash: T.any(Hash, HashExt),
|
|
19
19
|
opts: T.nilable(T::Hash[Symbol, T::Boolean])
|
|
20
20
|
).void
|
|
21
21
|
end
|
|
@@ -34,4 +34,7 @@ end
|
|
|
34
34
|
class ::Hash
|
|
35
35
|
sig { params(opts: T.nilable(Hash)).returns(T::Array[String]) }
|
|
36
36
|
def to_argv(opts = nil); end
|
|
37
|
+
|
|
38
|
+
sig { params(options: Hash).returns(T::Array[String]) }
|
|
39
|
+
def to_argv_bsd(options = {}); end
|
|
37
40
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: argvise
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- 2moe
|
|
@@ -21,10 +21,12 @@ files:
|
|
|
21
21
|
- License
|
|
22
22
|
- bin/build.rb
|
|
23
23
|
- bin/console.rb
|
|
24
|
+
- bin/irb.rb
|
|
24
25
|
- docs/Readme.md
|
|
25
26
|
- lib/argvise.rb
|
|
27
|
+
- lib/argvise/core.rb
|
|
28
|
+
- lib/argvise/refinement.rb
|
|
26
29
|
- lib/argvise/version.rb
|
|
27
|
-
- lib/core.rb
|
|
28
30
|
- rbi/lib/argvise.rbi
|
|
29
31
|
homepage: https://github.com/2moe/argvise-gem
|
|
30
32
|
licenses:
|