thor-interactive 0.1.0.pre.3 → 0.1.0.pre.4

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: '049af6f0fedd7bda65c5845a878bd02afe8e9e5cf705ebad254c333e0151ecd7'
4
- data.tar.gz: '049e366b5d3d1e4d0076c7c69d69240b00f0768b3b2687d23e9482dd7bf368dd'
3
+ metadata.gz: c927c297d565d278a09334334217dca98c5e05e72277f7fec4f5d07568f19c30
4
+ data.tar.gz: 353b4acde28a2a780a2b7a1ce85d7fb6e510f20d2862dd3dc4b2505140e2c014
5
5
  SHA512:
6
- metadata.gz: bc5ccc2b2765f3f5c4591c181445dbc4b0ef66982c982758fb6053fa79de968c7583f019be85a747c51474e6e2f55f8834b9c4fd3314b1186dd6185a9be9e092
7
- data.tar.gz: cc284a24f5f2a24bb016651e4252a9230d89c84d0e86b382fb4b936d11063422f78338d4c0917aedcf0a6e6d9fe7dcb66f8e7fb22c479c2903f355265e00cc79
6
+ metadata.gz: 77f42909ede7bb049ea1675bc2ccc4f486437be713bc2dd9c16cb5bfdd98b8723b77cacb87147335cb85d439f79c57b0ae18f0bbb0d874f80dd56b27a39bd5c9
7
+ data.tar.gz: 11f5c9a01fa32839b669d968c7cb154827bfd4cbe910c272094403280193c5115bef46fa4eb430ad476f9437d75fe13a515f12eadaa1d91080e48d4724c4866d
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "thor/interactive"
6
+
7
+ class EdgeCaseTest < Thor
8
+ include Thor::Interactive::Command
9
+
10
+ configure_interactive(
11
+ prompt: "test> ",
12
+ default_handler: ->(input, instance) {
13
+ puts "DEFAULT HANDLER: '#{input}'"
14
+ }
15
+ )
16
+
17
+ desc "topics [FILTER]", "List topics"
18
+ option :summarize, type: :boolean, desc: "Summarize topics"
19
+ option :format, type: :string, desc: "Output format"
20
+ def topics(filter = nil)
21
+ puts "TOPICS COMMAND:"
22
+ puts " Filter: #{filter.inspect}"
23
+ puts " Options: #{options.to_h.inspect}"
24
+ end
25
+
26
+ desc "echo TEXT", "Echo text"
27
+ def echo(text)
28
+ puts "ECHO: '#{text}'"
29
+ end
30
+
31
+ desc "test", "Run edge case tests"
32
+ def test
33
+ puts "\n=== Edge Case Tests ==="
34
+
35
+ puts "\n1. Unknown option:"
36
+ puts " Input: /topics --unknown-option"
37
+ process_input("/topics --unknown-option")
38
+
39
+ puts "\n2. Unknown option with value:"
40
+ puts " Input: /topics --unknown-option value"
41
+ process_input("/topics --unknown-option value")
42
+
43
+ puts "\n3. Mixed text and option-like strings:"
44
+ puts " Input: /topics The start of a string --option the rest"
45
+ process_input("/topics The start of a string --option the rest")
46
+
47
+ puts "\n4. Valid option mixed with text:"
48
+ puts " Input: /topics Some text --summarize more text"
49
+ process_input("/topics Some text --summarize more text")
50
+
51
+ puts "\n5. Option-like text in echo command (no options defined):"
52
+ puts " Input: /echo This has --what-looks-like an option"
53
+ process_input("/echo This has --what-looks-like an option")
54
+
55
+ puts "\n6. Real option after text:"
56
+ puts " Input: /topics AI and ML --format json"
57
+ process_input("/topics AI and ML --format json")
58
+
59
+ puts "\n=== End Tests ==="
60
+ end
61
+
62
+ private
63
+
64
+ def process_input(input)
65
+ # Simulate what the shell does
66
+ if input.start_with?('/')
67
+ send(:handle_slash_command, input[1..-1])
68
+ else
69
+ @default_handler.call(input, self)
70
+ end
71
+ rescue => e
72
+ puts " ERROR: #{e.message}"
73
+ end
74
+
75
+ def handle_slash_command(command_input)
76
+ parts = command_input.split(/\s+/, 2)
77
+ command = parts[0]
78
+ args = parts[1] || ""
79
+
80
+ if command == "topics"
81
+ # Parse with shellwords
82
+ require 'shellwords'
83
+ parsed = Shellwords.split(args) rescue args.split(/\s+/)
84
+ invoke("topics", parsed)
85
+ elsif command == "echo"
86
+ echo(args)
87
+ end
88
+ rescue => e
89
+ puts " ERROR: #{e.message}"
90
+ end
91
+ end
92
+
93
+ if __FILE__ == $0
94
+ puts "Edge Case Testing"
95
+ puts "================="
96
+
97
+ # Create instance and run tests
98
+ app = EdgeCaseTest.new
99
+ app.test
100
+
101
+ puts "\nInteractive mode - try these:"
102
+ puts " /topics --unknown-option"
103
+ puts " /topics The start --option the rest"
104
+ puts
105
+
106
+ app.interactive
107
+ end
@@ -0,0 +1,235 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "thor/interactive"
6
+
7
+ class OptionsDemo < Thor
8
+ include Thor::Interactive::Command
9
+
10
+ configure_interactive(
11
+ prompt: "opts> "
12
+ )
13
+
14
+ desc "process FILE", "Process a file with various options"
15
+ option :verbose, type: :boolean, aliases: "-v", desc: "Enable verbose output"
16
+ option :format, type: :string, default: "json", enum: ["json", "xml", "yaml", "csv"], desc: "Output format"
17
+ option :output, type: :string, aliases: "-o", desc: "Output file"
18
+ option :limit, type: :numeric, aliases: "-l", desc: "Limit number of results"
19
+ option :skip, type: :numeric, default: 0, desc: "Skip N results"
20
+ option :tags, type: :array, desc: "Tags to filter by"
21
+ option :config, type: :hash, desc: "Additional configuration"
22
+ option :dry_run, type: :boolean, desc: "Don't actually process, just show what would happen"
23
+ def process(file)
24
+ if options[:dry_run]
25
+ puts "DRY RUN MODE - No actual processing"
26
+ end
27
+
28
+ puts "Processing file: #{file}"
29
+ puts "=" * 50
30
+
31
+ if options[:verbose]
32
+ puts "Verbose mode enabled"
33
+ puts "All options:"
34
+ options.each do |key, value|
35
+ puts " #{key}: #{value.inspect}"
36
+ end
37
+ puts
38
+ end
39
+
40
+ puts "Format: #{options[:format]}"
41
+ puts "Output: #{options[:output] || 'stdout'}"
42
+
43
+ if options[:limit]
44
+ puts "Limiting to #{options[:limit]} results"
45
+ puts "Skipping first #{options[:skip]} results" if options[:skip] > 0
46
+ end
47
+
48
+ if options[:tags] && !options[:tags].empty?
49
+ puts "Filtering by tags: #{options[:tags].join(', ')}"
50
+ end
51
+
52
+ if options[:config] && !options[:config].empty?
53
+ puts "Configuration:"
54
+ options[:config].each do |key, value|
55
+ puts " #{key}: #{value}"
56
+ end
57
+ end
58
+
59
+ unless options[:dry_run]
60
+ puts "\n[Simulating processing...]"
61
+ sleep(1)
62
+ puts "✓ Processing complete!"
63
+ end
64
+ end
65
+
66
+ desc "search QUERY", "Search with options"
67
+ option :case_sensitive, type: :boolean, aliases: "-c", desc: "Case sensitive search"
68
+ option :regex, type: :boolean, aliases: "-r", desc: "Use regex"
69
+ option :files, type: :array, aliases: "-f", desc: "Files to search in"
70
+ option :max_results, type: :numeric, default: 10, desc: "Maximum results"
71
+ def search(query)
72
+ puts "Searching for: #{query}"
73
+ puts "Options:"
74
+ puts " Case sensitive: #{options[:case_sensitive] ? 'Yes' : 'No'}"
75
+ puts " Regex mode: #{options[:regex] ? 'Yes' : 'No'}"
76
+ puts " Max results: #{options[:max_results]}"
77
+
78
+ if options[:files]
79
+ puts " Searching in files: #{options[:files].join(', ')}"
80
+ else
81
+ puts " Searching in all files"
82
+ end
83
+
84
+ # Simulate search
85
+ results = [
86
+ "result_1.txt:10: #{query} found here",
87
+ "result_2.txt:25: another #{query} match",
88
+ "result_3.txt:40: #{query} appears again"
89
+ ]
90
+
91
+ puts "\nResults:"
92
+ results.take(options[:max_results]).each do |result|
93
+ puts " #{result}"
94
+ end
95
+ end
96
+
97
+ desc "convert INPUT OUTPUT", "Convert file from one format to another"
98
+ option :from, type: :string, required: true, desc: "Input format"
99
+ option :to, type: :string, required: true, desc: "Output format"
100
+ option :preserve_metadata, type: :boolean, desc: "Preserve file metadata"
101
+ option :compression, type: :string, enum: ["none", "gzip", "bzip2", "xz"], default: "none"
102
+ def convert(input, output)
103
+ puts "Converting: #{input} → #{output}"
104
+ puts "Format: #{options[:from]} → #{options[:to]}"
105
+ puts "Compression: #{options[:compression]}"
106
+ puts "Preserve metadata: #{options[:preserve_metadata] ? 'Yes' : 'No'}"
107
+
108
+ # Validation
109
+ unless File.exist?(input)
110
+ puts "Error: Input file '#{input}' not found"
111
+ return
112
+ end
113
+
114
+ puts "\n[Simulating conversion...]"
115
+ puts "✓ Conversion complete!"
116
+ end
117
+
118
+ desc "test", "Test various option formats"
119
+ def test
120
+ puts "\n=== Option Parsing Test Cases ==="
121
+ puts "\nTry these commands to test option parsing:\n"
122
+
123
+ examples = [
124
+ "/process file.txt --verbose",
125
+ "/process file.txt -v",
126
+ "/process data.json --format xml --output result.xml",
127
+ "/process data.json --format=yaml --limit=100",
128
+ "/process file.txt --tags important urgent todo",
129
+ "/process file.txt --config env:production db:postgres",
130
+ "/process file.txt -v --format csv -o output.csv --limit 50",
131
+ "/process file.txt --dry-run --verbose",
132
+ "",
133
+ "/search 'hello world' --case-sensitive",
134
+ "/search pattern -r -f file1.txt file2.txt file3.txt",
135
+ "/search query --max-results 5",
136
+ "",
137
+ "/convert input.json output.yaml --from json --to yaml",
138
+ "/convert data.csv data.json --from=csv --to=json --compression=gzip"
139
+ ]
140
+
141
+ examples.each do |example|
142
+ puts example.empty? ? "" : " #{example}"
143
+ end
144
+
145
+ puts "\n=== Features Demonstrated ==="
146
+ puts "✓ Boolean options (--verbose, -v)"
147
+ puts "✓ String options (--format xml, --format=xml)"
148
+ puts "✓ Numeric options (--limit 100)"
149
+ puts "✓ Array options (--tags tag1 tag2 tag3)"
150
+ puts "✓ Hash options (--config key1:val1 key2:val2)"
151
+ puts "✓ Required options (--from, --to in convert)"
152
+ puts "✓ Default values (format: json, skip: 0)"
153
+ puts "✓ Enum validation (format must be json/xml/yaml/csv)"
154
+ puts "✓ Short aliases (-v for --verbose, -o for --output)"
155
+ puts "✓ Multiple options in one command"
156
+ end
157
+
158
+ desc "help_options", "Explain how options work in interactive mode"
159
+ def help_options
160
+ puts <<~HELP
161
+
162
+ === Option Parsing in thor-interactive ===
163
+
164
+ Thor-interactive now fully supports Thor's option parsing!
165
+
166
+ BASIC USAGE:
167
+ /command arg1 arg2 --option value --flag
168
+
169
+ OPTION TYPES:
170
+ Boolean: --verbose or -v (no value needed)
171
+ String: --format json or --format=json
172
+ Numeric: --limit 100 or --limit=100
173
+ Array: --tags tag1 tag2 tag3
174
+ Hash: --config key1:val1 key2:val2
175
+
176
+ FEATURES:
177
+ • Long form: --option-name value
178
+ • Short form: -o value
179
+ • Equals syntax: --option=value
180
+ • Multiple options: --opt1 val1 --opt2 val2
181
+ • Default values: Defined in Thor command
182
+ • Required options: Must be provided
183
+ • Enum validation: Limited to specific values
184
+
185
+ BACKWARD COMPATIBILITY:
186
+ • Commands without options work as before
187
+ • Natural language still works for text commands
188
+ • Single-text commands preserve their behavior
189
+ • Default handler unaffected
190
+
191
+ EXAMPLES:
192
+ # Boolean flag
193
+ /process file.txt --verbose
194
+
195
+ # String option with equals
196
+ /process file.txt --format=xml
197
+
198
+ # Multiple options
199
+ /process file.txt -v --format csv --limit 10
200
+
201
+ # Array option
202
+ /search term --files file1.txt file2.txt
203
+
204
+ # Hash option
205
+ /deploy --config env:prod region:us-west
206
+
207
+ HOW IT WORKS:
208
+ 1. Thor-interactive detects if command has options defined
209
+ 2. Uses Thor's option parser to parse the arguments
210
+ 3. Separates options from regular arguments
211
+ 4. Sets options hash on Thor instance
212
+ 5. Calls command with remaining arguments
213
+ 6. Falls back to original behavior if parsing fails
214
+
215
+ NATURAL LANGUAGE:
216
+ Natural language input still works! Options are only
217
+ parsed for Thor commands that define them. Text sent
218
+ to default handlers is unchanged.
219
+
220
+ HELP
221
+ end
222
+
223
+ default_task :test
224
+ end
225
+
226
+ if __FILE__ == $0
227
+ puts "Thor Options Demo"
228
+ puts "=================="
229
+ puts
230
+ puts "This demo shows Thor option parsing in interactive mode."
231
+ puts "Type '/test' to see examples or '/help_options' for details."
232
+ puts
233
+
234
+ OptionsDemo.new.interactive
235
+ end
@@ -205,8 +205,8 @@ class Thor
205
205
  if thor_command?(command_word)
206
206
  task = @thor_class.tasks[command_word]
207
207
 
208
- if task && single_text_command?(task)
209
- # Single text command - pass everything after command as one argument
208
+ if task && single_text_command?(task) && !task.options.any?
209
+ # Single text command without options - pass everything after command as one argument
210
210
  text_part = command_input.sub(/^#{Regexp.escape(command_word)}\s*/, '')
211
211
  if text_part.empty?
212
212
  invoke_thor_command(command_word, [])
@@ -295,13 +295,29 @@ class Thor
295
295
  if command == "help"
296
296
  show_help(args.first)
297
297
  else
298
- # Always use direct method calls to avoid Thor's invoke deduplication
299
- # Thor's invoke method silently fails on subsequent calls to the same method
300
- if @thor_instance.respond_to?(command)
301
- @thor_instance.send(command, *args)
298
+ # Get the Thor task/command definition
299
+ task = @thor_class.tasks[command]
300
+
301
+ if task && task.options && !task.options.empty?
302
+ # Parse options if the command has them defined
303
+ parsed_args, parsed_options = parse_thor_options(args, task)
304
+
305
+ # Set options on the Thor instance
306
+ @thor_instance.options = Thor::CoreExt::HashWithIndifferentAccess.new(parsed_options)
307
+
308
+ # Call with parsed arguments only (options are in the options hash)
309
+ if @thor_instance.respond_to?(command)
310
+ @thor_instance.send(command, *parsed_args)
311
+ else
312
+ @thor_instance.send(command, *parsed_args)
313
+ end
302
314
  else
303
- # If method doesn't exist, this will raise a proper error
304
- @thor_instance.send(command, *args)
315
+ # No options defined, use original behavior
316
+ if @thor_instance.respond_to?(command)
317
+ @thor_instance.send(command, *args)
318
+ else
319
+ @thor_instance.send(command, *args)
320
+ end
305
321
  end
306
322
  end
307
323
  rescue SystemExit => e
@@ -320,6 +336,36 @@ class Thor
320
336
  puts "Error: #{e.message}"
321
337
  puts "Command: #{command}, Args: #{args.inspect}" if ENV["DEBUG"]
322
338
  end
339
+
340
+ def parse_thor_options(args, task)
341
+ # Convert args array to a format Thor's option parser expects
342
+ remaining_args = []
343
+ parsed_options = {}
344
+
345
+ # Create a temporary parser using Thor's options
346
+ parser = Thor::Options.new(task.options)
347
+
348
+ # Parse the arguments
349
+ begin
350
+ if args.is_a?(Array)
351
+ # Parse the options from the array
352
+ parsed_options = parser.parse(args)
353
+ remaining_args = parser.remaining
354
+ else
355
+ # Single string argument, split it first
356
+ split_args = safe_parse_input(args) || args.split(/\s+/)
357
+ parsed_options = parser.parse(split_args)
358
+ remaining_args = parser.remaining
359
+ end
360
+ rescue Thor::Error => e
361
+ # If parsing fails, treat everything as arguments (backward compatibility)
362
+ puts "Option parsing error: #{e.message}" if ENV["DEBUG"]
363
+ remaining_args = args.is_a?(Array) ? args : [args]
364
+ parsed_options = {}
365
+ end
366
+
367
+ [remaining_args, parsed_options]
368
+ end
323
369
 
324
370
  def show_help(command = nil)
325
371
  if command && @thor_class.tasks.key?(command)
@@ -4,5 +4,5 @@
4
4
  # This file is separate to avoid circular dependencies during gem installation
5
5
 
6
6
  module ThorInteractive
7
- VERSION = "0.1.0.pre.3"
7
+ VERSION = "0.1.0.pre.4"
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thor-interactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.3
4
+ version: 0.1.0.pre.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Petersen
@@ -55,7 +55,9 @@ files:
55
55
  - Rakefile
56
56
  - examples/README.md
57
57
  - examples/demo_session.rb
58
+ - examples/edge_case_test.rb
58
59
  - examples/nested_example.rb
60
+ - examples/options_demo.rb
59
61
  - examples/sample_app.rb
60
62
  - examples/signal_demo.rb
61
63
  - examples/test_interactive.rb