rake_options 0.1.1 → 0.1.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: f139479b6f1399fc8d375f81f8b0d55c0ee617f38a029754e9ae1a70099015bb
4
- data.tar.gz: 744d888d606e102fd3b2e442c89596ac347c8397c30004808ee4d7d6b8416464
3
+ metadata.gz: fb24381876d415a7632d456afd8cace19474689f511ec98df623e65967ab1fe1
4
+ data.tar.gz: 22e3ef52a8f49fc4287a5f5b89ce302a7df74103aca8debcda6a0175452bc3f0
5
5
  SHA512:
6
- metadata.gz: 27a9ba60af3907fb6b346fe00f386e02ed976ec3055b330a68848e42a911183b493fdd51f4a1584df90545bdb569339b5899a2da4ffe0be94beaaba96b0a2713
7
- data.tar.gz: 9c3f3056b9abfc8ed41e40991751c95a04c6e56d6d3cb3f4f4c76071930d4d87d5535ad4d1f9c31921acac6d12d14ed1d9ccef6d161b8a94c97f7d78b0475c1d
6
+ metadata.gz: 87e1e28bd9dcf2ee316cbc839d00133d12d0098759f0fdb5e19ccf7ebaecf7299b9b4edf5afaa8fc65dc239fa875747f8e0e28904a66548d52d35c08d4948cbe
7
+ data.tar.gz: e20bfdf5cb10e26ba2cc9eae12184fa23ca9934980b5b31c96efa35a4f4a09d9d99f32437e393789fc315a6d36f9163dae65a970100646f88b00760f061d0483
data/README.md CHANGED
@@ -31,21 +31,24 @@ require 'rake_options'
31
31
 
32
32
  desc "Build with custom options"
33
33
  task :build do
34
- config = {
35
- "with-mysql-lib" => "--with-mysql-lib $path",
36
- "enable-feature" => "--enable-feature $name"
37
- }
34
+ # Simple array configuration: [flag_name, type]
35
+ config = [
36
+ ["with-mysql-lib", :string],
37
+ ["enable-feature", :string],
38
+ ["port", :integer]
39
+ ]
38
40
 
39
41
  options = RakeOptions.command_line_args(config)
40
42
 
41
43
  puts "MySQL lib path: #{options['with-mysql-lib']}"
42
44
  puts "Feature: #{options['enable-feature']}"
45
+ puts "Port: #{options['port']}" # Automatically cast to integer
43
46
  end
44
47
  ```
45
48
 
46
49
  Run with:
47
50
  ```bash
48
- rake build -- --with-mysql-lib=/usr/local/mysql/lib --enable-feature=caching
51
+ rake build -- --with-mysql-lib=/usr/local/mysql/lib --enable-feature=caching --port=3000
49
52
  ```
50
53
 
51
54
  For values with spaces, use quotes:
@@ -53,6 +56,33 @@ For values with spaces, use quotes:
53
56
  rake build -- --with-mysql-lib="/path/with spaces/lib"
54
57
  ```
55
58
 
59
+ ### Type Casting
60
+
61
+ RakeOptions automatically casts values to the specified type:
62
+
63
+ ```ruby
64
+ config = [
65
+ ["port", :integer],
66
+ ["enabled", :boolean],
67
+ ["ratio", :float],
68
+ ["name", :string]
69
+ ]
70
+
71
+ options = RakeOptions.command_line_args(config)
72
+
73
+ # Values are automatically cast:
74
+ options['port'] # => 3000 (Integer)
75
+ options['enabled'] # => true (Boolean)
76
+ options['ratio'] # => 1.5 (Float)
77
+ options['name'] # => "myapp" (String)
78
+ ```
79
+
80
+ Supported types:
81
+ - `:string` - String values (default)
82
+ - `:integer` or `:int` - Integer values
83
+ - `:float` - Float values
84
+ - `:boolean` or `:bool` - Boolean values (true/false)
85
+
56
86
  ### Bracket-Style Arguments
57
87
 
58
88
  ```ruby
@@ -60,10 +90,10 @@ require 'rake_options'
60
90
 
61
91
  desc "Deploy with bracket notation"
62
92
  task :deploy do
63
- config = {
64
- "environment" => "[environment=$env]",
65
- "region" => "[region=$region]"
66
- }
93
+ config = [
94
+ ["environment", :string],
95
+ ["region", :string]
96
+ ]
67
97
 
68
98
  options = RakeOptions.command_line_args(config, notation: :bracket)
69
99
 
@@ -76,17 +106,6 @@ Run with:
76
106
  rake deploy [environment=production] [region=us-west-2]
77
107
  ```
78
108
 
79
- ### Multiple Variables in One Template
80
-
81
- ```ruby
82
- config = {
83
- "database" => "--config $file --env $environment"
84
- }
85
-
86
- options = RakeOptions.command_line_args(config)
87
- # Extracts both file and environment from: --config=database.yml --env=production
88
- ```
89
-
90
109
  ### Automatic Help Documentation
91
110
 
92
111
  ```ruby
@@ -96,18 +115,20 @@ readme_content = <<~HELP
96
115
  Build Task Help
97
116
 
98
117
  Available options:
99
- --with-mysql-lib PATH Specify MySQL library path
100
- --enable-feature NAME Enable a specific feature
118
+ --with-mysql-lib=PATH Specify MySQL library path (string)
119
+ --enable-feature=NAME Enable a specific feature (string)
120
+ --port=NUMBER Port number (integer)
101
121
  HELP
102
122
 
103
123
  RakeOptions.initialize_readme(readme_content)
104
124
 
105
125
  desc "Build with help support"
106
126
  task :build do
107
- config = {
108
- "with-mysql-lib" => "--with-mysql-lib $path",
109
- "enable-feature" => "--enable-feature $name"
110
- }
127
+ config = [
128
+ ["with-mysql-lib", :string],
129
+ ["enable-feature", :string],
130
+ ["port", :integer]
131
+ ]
111
132
 
112
133
  options = RakeOptions.command_line_args(config)
113
134
  # Your build logic here
@@ -133,29 +154,32 @@ options[:with_mysql_lib]
133
154
 
134
155
  ## Configuration Format
135
156
 
136
- The configuration hash maps symbolic names to template patterns:
157
+ The configuration is a simple array of tuples:
137
158
 
138
159
  ```ruby
139
- {
140
- "key-name" => "template pattern with $variables"
141
- }
160
+ [
161
+ ["flag-name", :type],
162
+ ["another-flag", :type]
163
+ ]
142
164
  ```
143
165
 
144
- - **Key**: The name you'll use to access the parsed value
145
- - **Value**: A template string that defines how to extract the argument
146
- - **Variables**: Prefixed with `$` to indicate where values should be extracted
166
+ - **flag-name**: The CLI flag name (will become `--flag-name`)
167
+ - **type**: The data type (`:string`, `:integer`, `:float`, `:boolean`)
147
168
 
148
- ### Template Examples
169
+ ### Examples
149
170
 
150
171
  ```ruby
151
- # Single variable
152
- "mysql-path" => "--with-mysql-lib $path"
172
+ # String values
173
+ ["mysql-path", :string]
174
+
175
+ # Integer values
176
+ ["port", :integer]
153
177
 
154
- # Multiple variables
155
- "database" => "--config $file --env $environment"
178
+ # Boolean flags
179
+ ["enabled", :boolean]
156
180
 
157
- # Bracket notation
158
- "region" => "[region=$value]"
181
+ # Float values
182
+ ["ratio", :float]
159
183
  ```
160
184
 
161
185
  ## Notation Styles
@@ -236,17 +260,17 @@ rake task -- --help
236
260
  rake task -- --path="/path/with spaces"
237
261
  ```
238
262
 
239
- ### Template not matching
263
+ ### Flag name mismatch
240
264
 
241
- **Problem**: Template variables aren't extracting values.
265
+ **Problem**: Arguments aren't being recognized.
242
266
 
243
- **Solution**: Ensure your template matches the command line format exactly:
267
+ **Solution**: Ensure the flag name in ARGV matches the config:
244
268
  ```ruby
245
- # Template
246
- "option" => "--option $value"
269
+ # Config
270
+ ["my-option", :string]
247
271
 
248
- # Command line must match (note the = sign)
249
- rake task -- --option=myvalue
272
+ # Command line must match exactly
273
+ rake task -- --my-option=myvalue
250
274
  ```
251
275
 
252
276
  ## Requirements
@@ -5,12 +5,14 @@ require_relative "template_engine"
5
5
  module RakeOptions
6
6
  class CLIParser
7
7
  def initialize(config)
8
- @config = config
8
+ @config = normalize_config(config)
9
9
  @parsed_templates = {}
10
10
 
11
11
  # Pre-parse all templates
12
- @config.each do |key, template_str|
13
- @parsed_templates[key] = TemplateEngine.parse_template(template_str)
12
+ @config.each do |key, type|
13
+ # Auto-construct the full template from the key
14
+ full_template = "--#{key} $value"
15
+ @parsed_templates[key] = TemplateEngine.parse_template(full_template)
14
16
  end
15
17
  end
16
18
 
@@ -20,18 +22,13 @@ module RakeOptions
20
22
  def parse(argv = ARGV)
21
23
  result = {}
22
24
 
23
- @config.each do |key, _template_str|
25
+ @config.each do |key, type|
24
26
  parsed_template = @parsed_templates[key]
25
27
  extracted = TemplateEngine.extract_values(argv, parsed_template)
26
28
 
27
29
  if extracted && !extracted.empty?
28
- # For single variable templates, store the value directly
29
- if extracted.size == 1
30
- result[key] = extracted.values.first
31
- else
32
- # For multiple variables, store the hash
33
- result[key] = extracted
34
- end
30
+ raw_value = extracted.values.first
31
+ result[key] = cast_value(raw_value, type)
35
32
  else
36
33
  result[key] = nil
37
34
  end
@@ -39,5 +36,40 @@ module RakeOptions
39
36
 
40
37
  result
41
38
  end
39
+
40
+ private
41
+
42
+ # Normalize config to hash format
43
+ # @param config [Array, Hash] Configuration
44
+ # @return [Hash] Normalized configuration
45
+ def normalize_config(config)
46
+ if config.is_a?(Array)
47
+ # Convert array of tuples to hash
48
+ config.to_h
49
+ else
50
+ config
51
+ end
52
+ end
53
+
54
+ # Cast value to specified type
55
+ # @param value [String] Raw string value
56
+ # @param type [Symbol] Target type
57
+ # @return [Object] Casted value
58
+ def cast_value(value, type)
59
+ return nil if value.nil?
60
+
61
+ case type
62
+ when :string
63
+ value.to_s
64
+ when :integer, :int
65
+ value.to_i
66
+ when :float
67
+ value.to_f
68
+ when :boolean, :bool
69
+ value.downcase == 'true' || value == '1'
70
+ else
71
+ value
72
+ end
73
+ end
42
74
  end
43
75
  end
@@ -3,7 +3,7 @@
3
3
  module RakeOptions
4
4
  class HelpGenerator
5
5
  def initialize(config, readme_content = nil)
6
- @config = config
6
+ @config = normalize_config(config)
7
7
  @readme_content = readme_content
8
8
  end
9
9
 
@@ -26,13 +26,24 @@ module RakeOptions
26
26
  end
27
27
  end
28
28
 
29
+ # Normalize config to hash format
30
+ # @param config [Array, Hash] Configuration
31
+ # @return [Hash] Normalized configuration
32
+ def normalize_config(config)
33
+ if config.is_a?(Array)
34
+ config.to_h
35
+ else
36
+ config
37
+ end
38
+ end
39
+
29
40
  # Auto-generate help from config
30
41
  # @return [String] Auto-generated help text
31
42
  def auto_generate_help
32
43
  help_text = "Available Options:\n\n"
33
44
 
34
- @config.each do |key, template|
35
- help_text += format_option(key, template)
45
+ @config.each do |key, type|
46
+ help_text += format_option(key, type)
36
47
  end
37
48
 
38
49
  help_text
@@ -40,10 +51,10 @@ module RakeOptions
40
51
 
41
52
  # Format individual option for display
42
53
  # @param key [String] Option key
43
- # @param template [String] Template string
54
+ # @param type [Symbol] Value type
44
55
  # @return [String] Formatted option line
45
- def format_option(key, template)
46
- " #{template.ljust(40)} (#{key})\n"
56
+ def format_option(key, type)
57
+ " --#{key}=VALUE".ljust(42) + "(#{type})\n"
47
58
  end
48
59
  end
49
60
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RakeOptions
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -5,11 +5,11 @@ require "spec_helper"
5
5
  RSpec.describe "RakeOptions Integration Tests" do
6
6
  describe "Full CLI parsing workflow" do
7
7
  let(:config) do
8
- {
9
- "with-mysql-lib" => "--with-mysql-lib $path",
10
- "enable-feature" => "--enable-feature $name",
11
- "port" => "--port $number"
12
- }
8
+ [
9
+ ["with-mysql-lib", :string],
10
+ ["enable-feature", :string],
11
+ ["port", :integer]
12
+ ]
13
13
  end
14
14
 
15
15
  it "parses multiple CLI arguments in one invocation" do
@@ -19,7 +19,7 @@ RSpec.describe "RakeOptions Integration Tests" do
19
19
 
20
20
  expect(result["with-mysql-lib"]).to eq("/usr/local/lib")
21
21
  expect(result["enable-feature"]).to eq("caching")
22
- expect(result["port"]).to eq("3000")
22
+ expect(result["port"]).to eq(3000)
23
23
  end
24
24
 
25
25
  it "handles partial arguments gracefully" do
@@ -134,20 +134,18 @@ RSpec.describe "RakeOptions Integration Tests" do
134
134
 
135
135
  describe "Mixed scenarios" do
136
136
  let(:config) do
137
- {
138
- "database" => "--config $file --env $environment",
139
- "verbose" => "--verbose $level"
140
- }
137
+ [
138
+ ["database", :string],
139
+ ["verbose", :string]
140
+ ]
141
141
  end
142
142
 
143
- it "handles templates with multiple variables" do
144
- stub_const("ARGV", ["--config=database.yml", "--env=production", "--verbose=debug"])
143
+ it "handles multiple arguments" do
144
+ stub_const("ARGV", ["--database=mydb", "--verbose=debug"])
145
145
 
146
146
  result = RakeOptions.command_line_args(config, notation: :cli)
147
147
 
148
- expect(result["database"]).to be_a(Hash)
149
- expect(result["database"]["file"]).to eq("database.yml")
150
- expect(result["database"]["environment"]).to eq("production")
148
+ expect(result["database"]).to eq("mydb")
151
149
  expect(result["verbose"]).to eq("debug")
152
150
  end
153
151
 
@@ -172,9 +170,7 @@ RSpec.describe "RakeOptions Integration Tests" do
172
170
 
173
171
  describe "Error scenarios" do
174
172
  let(:config) do
175
- {
176
- "option" => "--option $value"
177
- }
173
+ [["option", :string]]
178
174
  end
179
175
 
180
176
  it "raises InvalidNotationError for unsupported notation" do
@@ -195,16 +191,16 @@ RSpec.describe "RakeOptions Integration Tests" do
195
191
  describe "Real-world usage scenarios" do
196
192
  context "deployment task" do
197
193
  let(:deploy_config) do
198
- {
199
- "environment" => "--env $environment",
200
- "region" => "--region $region",
201
- "version" => "--version $ver",
202
- "dry-run" => "--dry-run $flag"
203
- }
194
+ [
195
+ ["environment", :string],
196
+ ["region", :string],
197
+ ["version", :string],
198
+ ["dry-run", :boolean]
199
+ ]
204
200
  end
205
201
 
206
202
  it "handles typical deployment command" do
207
- stub_const("ARGV", ["--env=production", "--region=us-east-1", "--version=v1.2.3"])
203
+ stub_const("ARGV", ["--environment=production", "--region=us-east-1", "--version=v1.2.3"])
208
204
 
209
205
  result = RakeOptions.command_line_args(deploy_config)
210
206
 
@@ -217,17 +213,17 @@ RSpec.describe "RakeOptions Integration Tests" do
217
213
 
218
214
  context "build task" do
219
215
  let(:build_config) do
220
- {
221
- "with-mysql" => "--with-mysql-lib $path",
222
- "with-ssl" => "--with-ssl-lib $path",
223
- "prefix" => "--prefix $path"
224
- }
216
+ [
217
+ ["with-mysql", :string],
218
+ ["with-ssl", :string],
219
+ ["prefix", :string]
220
+ ]
225
221
  end
226
222
 
227
223
  it "handles build configuration" do
228
224
  stub_const("ARGV", [
229
- "--with-mysql-lib=/usr/local/mysql/lib",
230
- "--with-ssl-lib=/usr/local/ssl/lib",
225
+ "--with-mysql=/usr/local/mysql/lib",
226
+ "--with-ssl=/usr/local/ssl/lib",
231
227
  "--prefix=/opt/myapp"
232
228
  ])
233
229
 
@@ -7,9 +7,7 @@ RSpec.describe RakeOptions::CLIParser do
7
7
  describe "#parse" do
8
8
  context "with single flag" do
9
9
  let(:config) do
10
- {
11
- "with-mysql-lib" => "--with-mysql-lib $path"
12
- }
10
+ [["with-mysql-lib", :string]]
13
11
  end
14
12
  let(:parser) { described_class.new(config) }
15
13
 
@@ -30,10 +28,7 @@ RSpec.describe RakeOptions::CLIParser do
30
28
 
31
29
  context "with multiple flags" do
32
30
  let(:config) do
33
- {
34
- "with-mysql-lib" => "--with-mysql-lib $path",
35
- "enable-feature" => "--enable-feature $name"
36
- }
31
+ [["with-mysql-lib", :string], ["enable-feature", :string]]
37
32
  end
38
33
  let(:parser) { described_class.new(config) }
39
34
 
@@ -56,9 +51,7 @@ RSpec.describe RakeOptions::CLIParser do
56
51
 
57
52
  context "with quoted values" do
58
53
  let(:config) do
59
- {
60
- "message" => "--message $text"
61
- }
54
+ [["message", :string]]
62
55
  end
63
56
  let(:parser) { described_class.new(config) }
64
57
 
@@ -72,9 +65,7 @@ RSpec.describe RakeOptions::CLIParser do
72
65
 
73
66
  context "with unknown flags" do
74
67
  let(:config) do
75
- {
76
- "known-flag" => "--known-flag $value"
77
- }
68
+ [["known-flag", :string]]
78
69
  end
79
70
  let(:parser) { described_class.new(config) }
80
71
 
@@ -87,21 +78,34 @@ RSpec.describe RakeOptions::CLIParser do
87
78
  end
88
79
  end
89
80
 
90
- context "with multiple variables in template" do
81
+ context "with type casting" do
91
82
  let(:config) do
92
- {
93
- "database" => "--config $file --env $environment"
94
- }
83
+ [["port", :integer], ["enabled", :boolean], ["ratio", :float]]
95
84
  end
96
85
  let(:parser) { described_class.new(config) }
97
86
 
98
- it "extracts multiple variables" do
99
- argv = ["--config=database.yml", "--env=production"]
87
+ it "casts integer values" do
88
+ argv = ["--port=3000"]
100
89
  result = parser.parse(argv)
101
90
 
102
- expect(result["database"]).to be_a(Hash)
103
- expect(result["database"]["file"]).to eq("database.yml")
104
- expect(result["database"]["environment"]).to eq("production")
91
+ expect(result["port"]).to eq(3000)
92
+ expect(result["port"]).to be_a(Integer)
93
+ end
94
+
95
+ it "casts boolean values" do
96
+ argv = ["--enabled=true"]
97
+ result = parser.parse(argv)
98
+
99
+ expect(result["enabled"]).to eq(true)
100
+ expect(result["enabled"]).to be_a(TrueClass)
101
+ end
102
+
103
+ it "casts float values" do
104
+ argv = ["--ratio=1.5"]
105
+ result = parser.parse(argv)
106
+
107
+ expect(result["ratio"]).to eq(1.5)
108
+ expect(result["ratio"]).to be_a(Float)
105
109
  end
106
110
  end
107
111
  end
@@ -6,10 +6,7 @@ require "rake_options/help_generator"
6
6
  RSpec.describe RakeOptions::HelpGenerator do
7
7
  describe "#display_and_exit" do
8
8
  let(:config) do
9
- {
10
- "with-mysql-lib" => "--with-mysql-lib $path",
11
- "enable-feature" => "--enable-feature $name"
12
- }
9
+ [["with-mysql-lib", :string], ["enable-feature", :string]]
13
10
  end
14
11
 
15
12
  context "with custom README" do
@@ -46,10 +43,7 @@ RSpec.describe RakeOptions::HelpGenerator do
46
43
 
47
44
  describe "#generate_help_text" do
48
45
  let(:config) do
49
- {
50
- "option1" => "--option1 $value",
51
- "option2" => "--option2 $value"
52
- }
46
+ [["option1", :string], ["option2", :string]]
53
47
  end
54
48
 
55
49
  context "with custom README" do
@@ -75,12 +69,12 @@ RSpec.describe RakeOptions::HelpGenerator do
75
69
  end
76
70
 
77
71
  describe "#format_option" do
78
- let(:generator) { described_class.new({}) }
72
+ let(:generator) { described_class.new([]) }
79
73
 
80
- it "formats option with key and template" do
81
- formatted = generator.send(:format_option, "my-option", "--my-option $value")
82
- expect(formatted).to include("--my-option $value")
83
- expect(formatted).to include("my-option")
74
+ it "formats option with key and type" do
75
+ formatted = generator.send(:format_option, "my-option", :string)
76
+ expect(formatted).to include("--my-option")
77
+ expect(formatted).to include("string")
84
78
  end
85
79
  end
86
80
  end
@@ -4,10 +4,7 @@ require "spec_helper"
4
4
 
5
5
  RSpec.describe RakeOptions do
6
6
  let(:config) do
7
- {
8
- "with-mysql-lib" => "--with-mysql-lib $path",
9
- "enable-feature" => "--enable-feature $name"
10
- }
7
+ [["with-mysql-lib", :string], ["enable-feature", :string]]
11
8
  end
12
9
 
13
10
  describe ".command_line_args" do
@@ -66,6 +63,10 @@ RSpec.describe RakeOptions do
66
63
  end
67
64
 
68
65
  describe ".initialize_readme" do
66
+ let(:simple_config) do
67
+ [["option", :string]]
68
+ end
69
+
69
70
  it "stores readme content" do
70
71
  readme = "Custom help text"
71
72
  described_class.initialize_readme(readme)
@@ -78,7 +79,7 @@ RSpec.describe RakeOptions do
78
79
  described_class.initialize_readme(readme)
79
80
  stub_const("ARGV", ["--help"])
80
81
 
81
- expect { described_class.command_line_args(config) }
82
+ expect { described_class.command_line_args(simple_config) }
82
83
  .to output(/My Custom Help/).to_stdout
83
84
  .and raise_error(SystemExit)
84
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_options
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randy Villanueva