argvise 0.0.0 → 0.0.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: 2ffc3b336215c132bac812e0a592a576f8c052e061363cfa337b876fd8d8ba5c
4
- data.tar.gz: 2b13e0ffb309f4473249775fbe44161289573395eb3fc2df8b10e1ffa49b2620
3
+ metadata.gz: 8558fd9dd7cf152d33787d2c15c101a18e28d3064e2ecfe54c77d4d488e16119
4
+ data.tar.gz: 1ef9387ef135826ee91e6409b1c585ba92ee7411487a1fc6dec25a47e7268718
5
5
  SHA512:
6
- metadata.gz: b0f93d1d83200a891f6ca6d70227931f15fd605d474ca108bc6e402e1fafe45d3b164f3e0b669eb42511ef81a61c0fe4b33bf80675291c36e47a0300aefbce95
7
- data.tar.gz: 075aa56b3faa272177d7536cd6753ed5e8e95684510906bde68083008fac4c956ad80b69c6e8486b20e0e41b445edeace7eb7206195524225b9512ff770bce96
6
+ metadata.gz: d39ff4e6428417cd801f08f077a9f92b742d85bb91a2fd15cfed5bead4b432d4a999a592f93f7339334ec11b5ec4ec5d88fc719a126afc4341cf28424a2d94c0
7
+ data.tar.gz: cec50ddc41188e914a4967ff97fe914e84ad620f7033823d322d25284d9f7933cf9e1f7f2186f06dfc57f3c92221d0e54828a9739c7681a3eba8f119f792062e
data/docs/Readme.md ADDED
@@ -0,0 +1,119 @@
1
+ # Argvise
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/argvise.svg?icon=si%3Arubygems)](https://rubygems.org/gems/argvise)
4
+
5
+ A Ruby gem for converting hash structures into command-line argument arrays.
6
+
7
+ > **Note:** This is *not* a command-line parser — quite the opposite. Argvise helps you **build** CLI commands programmatically.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'argvise'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundler install
21
+ ```
22
+
23
+ Or install it directly:
24
+
25
+ ```bash
26
+ gem install argvise
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Basic Conversion
32
+
33
+ ```ruby
34
+ require 'argvise'
35
+
36
+ options = {
37
+ docker: nil, #=> docker
38
+ build: nil,
39
+ push: true, #=> --push
40
+ tag: ["ghcr.io/[user]/repo:latest", "ghcr.io/[user]/repo:v0.0.1"], #=> --tag ghcr... --tag ghcr..0.0.1
41
+ platform: "wasi/wasm", #=> --platform wasi/wasm
42
+ label: {
43
+ maintainer: "user",
44
+ description: "A Docker build example"
45
+ }, # => --label maintainer=user --label description=A..example
46
+ file: "wasi.dockerfile",
47
+ path: nil,
48
+ }
49
+
50
+ Argvise.build(options)
51
+ # => [
52
+ # "docker", "build", "--push",
53
+ # "--tag", "ghcr.io/[user]/repo:latest", "--tag", "ghcr.io/[user]/repo:v0.0.1",
54
+ # "--platform", "wasi/wasm",
55
+ # "--label", "maintainer=user", "--label", "description=A Docker build example",
56
+ # "--file", "wasi.dockerfile", "path"
57
+ # ]
58
+ ```
59
+
60
+ ### Lambda Shortcut
61
+
62
+ ```ruby
63
+ { v: true, dir: '/path/to/dir' }.then(&hash_to_args)
64
+ # => ["-v", "--dir", "/path/to/dir"]
65
+ ```
66
+
67
+ ## Supported Data Structures
68
+
69
+ ### 1. Simple Flags
70
+
71
+ ```ruby
72
+ { verbose: true }.then(&hash_to_args) # => ["--verbose"]
73
+ ```
74
+
75
+ ### 2. Boolean Values
76
+
77
+ ```ruby
78
+ { silent: false }.then(&hash_to_args) # => []
79
+ ```
80
+
81
+ ### 3. Array Values
82
+
83
+ ```ruby
84
+ { tag: %w[a b] }.then(&hash_to_args)
85
+ # => ["--tag", "a", "--tag", "b"]
86
+ ```
87
+
88
+ ### 4. Hash Values
89
+
90
+ ```ruby
91
+ { label: { env: 'test', k2: 'v2' } }.then(&hash_to_args)
92
+ # => ["--label", "env=test", "--label", "k2=v2"]
93
+ ```
94
+
95
+ ## API Documentation
96
+
97
+ ### `Argvise.build(options)`
98
+
99
+ Main method to convert hash to command-line arguments array
100
+
101
+ **Parameters:**
102
+
103
+ - `options` (Hash) - The hash to be converted
104
+
105
+ **Returns:**
106
+
107
+ - Array<String> generated command-line arguments
108
+
109
+ ### Conversion Rules
110
+
111
+ | Hash Format | Result Example |
112
+ | ------------------------- | ---------------------------------- |
113
+ | `{ key: nil }` | `["key"]` |
114
+ | `{ key: false }` | `[]` |
115
+ | `{ key: true }` | `["--key"]` |
116
+ | `{ k: true }` | `["-k"]` |
117
+ | `{ key: "value" }` | `["--key", "value"]` |
118
+ | `{ key: ["a", "b"] }` | `["--key", "a", "--key", "b"]` |
119
+ | `{ key: { a: 1, b: 2 } }` | `["--key", "a=1", "--key", "b=2"]` |
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Argvise
4
+ VERSION = '0.0.2'
5
+ end
data/lib/argvise.rb CHANGED
@@ -1,102 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Style/Lambda, Lint/MissingCopEnableDirective
4
- # ------------------
5
- # Converts hash to command array
6
- #
7
- # Example
8
- # {cargo: nil, build: nil, v: true, target: "wasm32-wasip2"}.then { Argvise.build _1 }
9
- # # Output => ["cargo", "build", "-v", "--target", "wasm32-wasip2"]
10
- #
11
- # Supported data structures:
12
- # - Simple flags: `{ verbose: true }` => "--verbose"
13
- # - Boolean values: `{ v: false }` => no argument generated
14
- # - Array values: `{ tag: %w[a b] }` => `["--tag", "a", "--tag", "b"]`
15
- # - Hash values: `{ label: { env: 'test' } }` => `["--label", "env=test"]`
16
- class Argvise
17
- # Converts a hash into a command-line argument array
18
- #
19
- # @param options [Hash] The hash to be converted
20
- # @return [Array<String>] The generated array of command-line arguments
21
- def self.build(options)
22
- new.build(options)
23
- end
24
-
25
- def build(options)
26
- # options.each_pair.flat_map { |k, v| process_pair(k.to_s, v) }
27
- options.each_with_object([]) do |(k, v), memo|
28
- memo.concat(process_pair(k.to_s, v))
29
- end
30
- end
31
-
32
- private
33
-
34
- # Processes a single key-value pair and generates the corresponding argument fragment
35
- def process_pair(key, value)
36
- # e.g., {cargo: nil, build: nil} => ["cargo", "build"]
37
- return [key] if value.nil?
38
- # e.g., {install: false} => []
39
- return [] unless value
40
-
41
- flag = build_flag(key)
42
- generate_args(flag, value)
43
- end
44
-
45
- # Builds the command-line flag prefix (automatically detects short or long options)
46
- #
47
- # short key, e.g., {v: true} => "-v"
48
- # long key, e.g., {verbose: true} => "--verbose"
49
- def build_flag(key)
50
- prefix = key.length == 1 ? '-' : '--'
51
- "#{prefix}#{key.tr('_', '-')}"
52
- end
53
-
54
- # Generates the corresponding argument array based on the value type
55
- def generate_args(flag, value)
56
- case value
57
- when true
58
- [flag]
59
- when Array
60
- expand_array(flag, value)
61
- when Hash
62
- expand_hash(flag, value)
63
- else
64
- # e.g., {tag: 'uuu'} => ["--tag", "uuu"]
65
- [flag, value.to_s]
66
- end
67
- end
68
-
69
- # {tag: ["v1", "v2"]}
70
- # => (flag: "--tag", array: ['v1', 'v2'])
71
- # => ["--tag", "v1", "--tag", "v2"]
72
- def expand_array(flag, array)
73
- # FP style: array.flat_map { |v| [flag, v.to_s] }
74
- array.each_with_object([]) do |v, memo|
75
- memo << flag
76
- memo << v.to_s
77
- end
78
- end
79
-
80
- # Processes hash values (generates key=value format)
81
- #
82
- # {label: { env: "test", key: "value" }}
83
- # => (flag: "--label", hash)
84
- # => ["--label", "env=test", "--label", "key=value"]
85
- def expand_hash(flag, hash)
86
- # hash.flat_map { |k, v| [flag, "#{k}=#{v}"] }
87
- hash.each_with_object([]) do |(k, v), memo|
88
- memo << flag
89
- memo << "#{k}=#{v}"
90
- end
91
- end
92
- end
93
-
94
- # A convenient lambda method: converts a hash into command-line arguments
95
- #
96
- # Example:
97
- # { v: true, path: '/path/to/dir' }.then(&hash_to_args) # => ["-v", "--path", "/path/to/dir"]
98
- def hash_to_args
99
- ->(options) do
100
- Argvise.build(options)
101
- end
102
- end
3
+ require_relative 'argvise/version'
4
+ require_relative 'core'
data/lib/core.rb ADDED
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Style/Lambda, Lint/MissingCopEnableDirective
4
+ # ------------------
5
+ # Converts hash to command array
6
+ #
7
+ # Example
8
+ # {cargo: nil, build: nil, v: true, target: "wasm32-wasip2"}.then { Argvise.build _1 }
9
+ # # Output => ["cargo", "build", "-v", "--target", "wasm32-wasip2"]
10
+ #
11
+ # Supported data structures:
12
+ # - Simple flags: `{ verbose: true }` => "--verbose"
13
+ # - Boolean values: `{ v: false }` => no argument generated
14
+ # - Array values: `{ tag: %w[a b] }` => `["--tag", "a", "--tag", "b"]`
15
+ # - Hash values: `{ label: { env: 'test' } }` => `["--label", "env=test"]`
16
+ class Argvise
17
+ # Converts a hash into a command-line argument array
18
+ #
19
+ # @param options [Hash] The hash to be converted
20
+ # @return [Array<String>] The generated array of command-line arguments
21
+ #
22
+ # sig { params(options: Hash).returns(T::Array[String]) }
23
+ def self.build(options)
24
+ new.build(options)
25
+ end
26
+
27
+ def build(options)
28
+ # options.each_pair.flat_map { |k, v| process_pair(k.to_s, v) }
29
+ options.each_with_object([]) do |(k, v), memo|
30
+ memo.concat(process_pair(k.to_s, v))
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ # Processes a single key-value pair and generates the corresponding argument fragment
37
+ def process_pair(key, value)
38
+ # e.g., {cargo: nil, build: nil} => ["cargo", "build"]
39
+ return [key] if value.nil?
40
+ # e.g., {install: false} => []
41
+ return [] unless value
42
+
43
+ flag = build_flag(key)
44
+ generate_args(flag, value)
45
+ end
46
+
47
+ # Builds the command-line flag prefix (automatically detects short or long options)
48
+ #
49
+ # short key, e.g., {v: true} => "-v"
50
+ # long key, e.g., {verbose: true} => "--verbose"
51
+ def build_flag(key)
52
+ prefix = key.length == 1 ? '-' : '--'
53
+ "#{prefix}#{key.tr('_', '-')}"
54
+ end
55
+
56
+ # Generates the corresponding argument array based on the value type
57
+ def generate_args(flag, value)
58
+ case value
59
+ when true
60
+ [flag]
61
+ when Array
62
+ expand_array(flag, value)
63
+ when Hash
64
+ expand_hash(flag, value)
65
+ else
66
+ # e.g., {tag: 'uuu'} => ["--tag", "uuu"]
67
+ [flag, value.to_s]
68
+ end
69
+ end
70
+
71
+ # {tag: ["v1", "v2"]}
72
+ # => (flag: "--tag", array: ['v1', 'v2'])
73
+ # => ["--tag", "v1", "--tag", "v2"]
74
+ def expand_array(flag, array)
75
+ # FP style: array.flat_map { |v| [flag, v.to_s] }
76
+ array.each_with_object([]) do |v, memo|
77
+ memo << flag
78
+ memo << v.to_s
79
+ end
80
+ end
81
+
82
+ # Processes hash values (generates key=value format)
83
+ #
84
+ # {label: { env: "test", key: "value" }}
85
+ # => (flag: "--label", hash)
86
+ # => ["--label", "env=test", "--label", "key=value"]
87
+ def expand_hash(flag, hash)
88
+ # hash.flat_map { |k, v| [flag, "#{k}=#{v}"] }
89
+ hash.each_with_object([]) do |(k, v), memo|
90
+ memo << flag
91
+ memo << "#{k}=#{v}"
92
+ end
93
+ end
94
+ end
95
+
96
+ # A convenient lambda method: converts a hash into command-line arguments
97
+ #
98
+ # Example:
99
+ # { v: true, path: '/path/to/dir' }.then(&hash_to_args) # => ["-v", "--path", "/path/to/dir"]
100
+ #
101
+ # sig { returns(T.proc.params(options: Hash).returns(T::Array[String])) }
102
+ def hash_to_args
103
+ ->(options) do
104
+ Argvise.build(options)
105
+ end
106
+ end
@@ -0,0 +1,12 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ class Argvise
5
+ class << self
6
+ sig { params(options: Hash).returns(T::Array[String]) }
7
+ def build(options); end
8
+ end
9
+ end
10
+
11
+ sig { returns(T.proc.params(options: Hash).returns(T::Array[String])) }
12
+ def hash_to_args; 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.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - 2moe
@@ -18,7 +18,11 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - License
21
+ - docs/Readme.md
21
22
  - lib/argvise.rb
23
+ - lib/argvise/version.rb
24
+ - lib/core.rb
25
+ - rbi/lib/argvise.rbi
22
26
  homepage: https://github.com/2moe/argvise-gem
23
27
  licenses:
24
28
  - Apache-2.0