cliqr 1.2.0 → 2.0.0

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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +9 -71
  4. data/examples/numbers +1 -2
  5. data/examples/vagrant +0 -3
  6. data/lib/cliqr.rb +52 -11
  7. data/lib/cliqr/argument_validation/argument_type_validator.rb +2 -2
  8. data/lib/cliqr/argument_validation/validator.rb +3 -3
  9. data/lib/cliqr/{cli → command}/argument_operator.rb +2 -2
  10. data/lib/cliqr/{cli → command}/argument_operator_context.rb +1 -1
  11. data/lib/cliqr/{cli/command.rb → command/base_command.rb} +2 -2
  12. data/lib/cliqr/command/color.rb +174 -0
  13. data/lib/cliqr/{cli → command}/command_context.rb +68 -20
  14. data/lib/cliqr/command/shell_banner_builder.rb +17 -0
  15. data/lib/cliqr/command/shell_command.rb +125 -0
  16. data/lib/cliqr/command/shell_prompt_builder.rb +26 -0
  17. data/lib/cliqr/config/action.rb +226 -0
  18. data/lib/cliqr/config/base.rb +84 -0
  19. data/lib/cliqr/config/command.rb +137 -0
  20. data/lib/cliqr/config/dsl.rb +81 -0
  21. data/lib/cliqr/config/event.rb +43 -0
  22. data/lib/cliqr/config/event_based.rb +78 -0
  23. data/lib/cliqr/config/named.rb +55 -0
  24. data/lib/cliqr/config/option.rb +95 -0
  25. data/lib/cliqr/config/option_based.rb +130 -0
  26. data/lib/cliqr/config/shell.rb +87 -0
  27. data/lib/cliqr/config/validation/validation_set.rb +66 -0
  28. data/lib/cliqr/config/validation/validator_factory.rb +403 -0
  29. data/lib/cliqr/config/validation/verifiable.rb +91 -0
  30. data/lib/cliqr/error.rb +20 -4
  31. data/lib/cliqr/events/event.rb +56 -0
  32. data/lib/cliqr/events/event_context.rb +31 -0
  33. data/lib/cliqr/events/handler.rb +32 -0
  34. data/lib/cliqr/events/invoker.rb +70 -0
  35. data/lib/cliqr/{cli → executor}/command_runner_factory.rb +3 -3
  36. data/lib/cliqr/{cli → executor}/router.rb +4 -4
  37. data/lib/cliqr/{cli/executor.rb → executor/runner.rb} +25 -10
  38. data/lib/cliqr/interface.rb +98 -0
  39. data/lib/cliqr/parser/token_factory.rb +1 -1
  40. data/lib/cliqr/usage/command_usage_context.rb +94 -0
  41. data/lib/cliqr/usage/option_usage_context.rb +86 -0
  42. data/lib/cliqr/usage/templates/partial/action_list.erb +10 -0
  43. data/lib/cliqr/usage/templates/partial/command_name.erb +3 -0
  44. data/lib/cliqr/usage/templates/partial/option_list.erb +18 -0
  45. data/lib/cliqr/usage/templates/partial/usage_info.erb +5 -0
  46. data/lib/cliqr/usage/templates/usage/cli.erb +4 -0
  47. data/lib/cliqr/usage/templates/usage/shell.erb +2 -0
  48. data/lib/cliqr/usage/usage_builder.rb +59 -0
  49. data/lib/cliqr/util.rb +81 -34
  50. data/lib/cliqr/version.rb +1 -1
  51. data/spec/config/action_config_validator_spec.rb +127 -5
  52. data/spec/config/config_finalize_spec.rb +3 -3
  53. data/spec/config/config_validator_spec.rb +120 -17
  54. data/spec/config/option_config_validator_spec.rb +1 -1
  55. data/spec/dsl/interface_spec.rb +2 -2
  56. data/spec/dsl/usage_spec.rb +461 -465
  57. data/spec/executor/action_executor_spec.rb +1 -1
  58. data/spec/executor/color_executor_spec.rb +125 -0
  59. data/spec/executor/command_runner_spec.rb +6 -8
  60. data/spec/executor/event_executor_spec.rb +365 -0
  61. data/spec/executor/executor_spec.rb +49 -11
  62. data/spec/executor/help_executor_spec.rb +107 -103
  63. data/spec/fixtures/action_reader_command.rb +1 -1
  64. data/spec/fixtures/test_arg_printer_event_handler.rb +9 -0
  65. data/spec/fixtures/test_color_shell_prompt.rb +13 -0
  66. data/spec/fixtures/test_empty_event_handler.rb +5 -0
  67. data/spec/fixtures/test_invoker_event_handler.rb +9 -0
  68. data/spec/fixtures/test_shell_banner.rb +8 -0
  69. data/spec/fixtures/test_shell_prompt.rb +13 -0
  70. data/spec/shell/shell_executor_spec.rb +700 -0
  71. data/spec/validation/validation_spec.rb +2 -2
  72. metadata +65 -27
  73. data/lib/cliqr/cli/config.rb +0 -554
  74. data/lib/cliqr/cli/interface.rb +0 -107
  75. data/lib/cliqr/cli/shell_command.rb +0 -69
  76. data/lib/cliqr/cli/usage_builder.rb +0 -185
  77. data/lib/cliqr/config_validation/validation_set.rb +0 -48
  78. data/lib/cliqr/config_validation/validator_factory.rb +0 -319
  79. data/lib/cliqr/config_validation/verifiable.rb +0 -89
  80. data/lib/cliqr/dsl.rb +0 -59
  81. data/spec/executor/shell_executor_spec.rb +0 -233
  82. data/templates/usage.erb +0 -39
@@ -3,6 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  require 'cliqr/error'
6
+ require 'cliqr/executor/runner'
6
7
 
7
8
  require 'fixtures/test_command'
8
9
  require 'fixtures/always_error_command'
@@ -13,7 +14,7 @@ require 'fixtures/argument_reader_command'
13
14
  require 'fixtures/test_option_type_checker_command'
14
15
  require 'fixtures/csv_argument_operator'
15
16
 
16
- describe Cliqr::CLI::Executor do
17
+ describe Cliqr::Executor::Runner do
17
18
  it 'returns code 0 for default command runner' do
18
19
  expect(Cliqr.command.new.execute(nil)).to eq(0)
19
20
  end
@@ -244,7 +245,7 @@ value = operator-inline
244
245
  handler do
245
246
  puts 'in my-command'
246
247
  puts options.map { |option| "#{option.name} => #{option.value}" }
247
- puts action?
248
+ puts action_type?
248
249
  puts option?('option-1')
249
250
  puts option?('option-2')
250
251
  puts option?('option-3')
@@ -258,7 +259,7 @@ value = operator-inline
258
259
  puts 'in my-action'
259
260
  puts options.map { |option| "#{option.name} => #{option.value}" }
260
261
  puts option('option-3').value
261
- puts action?
262
+ puts action_type?
262
263
  puts option?('option-1')
263
264
  puts option?('option-2')
264
265
  puts option?('option-3')
@@ -438,18 +439,55 @@ false
438
439
  EOS
439
440
  end
440
441
 
441
- it 'can use version action on action command' do
442
+ it 'can not use version action on action command' do
443
+ def define_interface
444
+ Cliqr.interface do
445
+ name 'my-command'
446
+
447
+ action :bla do
448
+ version '1234'
449
+ end
450
+ end
451
+ end
452
+ expect { define_interface }.to(raise_error(NoMethodError))
453
+ end
454
+
455
+ it 'can forward command to another action' do
442
456
  cli = Cliqr.interface do
443
- name 'my-command'
457
+ name :my_command
458
+ description 'test command has no description'
444
459
 
445
- action :bla do
446
- version '1234'
460
+ action :action_1 do
461
+ description 'test action'
462
+ handler do
463
+ puts 'in action_1'
464
+ forward 'my_command action_2 sub-action' # starting with base command name
465
+ puts 'back in action_1'
466
+ end
467
+ end
468
+
469
+ action 'action_2' do
470
+ handler do
471
+ puts 'in action_2'
472
+ end
473
+
474
+ action 'sub-action' do
475
+ handler do
476
+ puts 'in sub-action'
477
+ forward 'action_2' # not starting with base command name
478
+ puts 'back in sub-action'
479
+ end
480
+ end
447
481
  end
448
482
  end
449
483
 
450
- result = cli.execute_internal ['bla version'], output: :buffer
484
+ result = cli.execute_internal ['action_1'], output: :buffer
451
485
  expect(result[:stdout]).to eq <<-EOS
452
- 1234
486
+ in action_1
487
+ in sub-action
488
+ in action_2
489
+ back in sub-action
490
+ back in action_1
453
491
  EOS
454
492
  end
455
493
 
@@ -483,7 +521,7 @@ false
483
521
  old_stdout = $stdout
484
522
  $stdout = old_stdout.is_a?(StringIO) ? old_stdout : StringIO.new('', 'w')
485
523
  begin
486
- expect(cli.execute(['abcd'])).to(eq(1))
524
+ expect(cli.execute(['abcd'])).to(eq(101))
487
525
  expect($stdout.string).to(eq("command 'my-command' failed\n\nCause: StandardError - I am not a happy handler!\n"))
488
526
  ensure
489
527
  $stdout = old_stdout
@@ -499,7 +537,7 @@ false
499
537
  old_stdout = $stdout
500
538
  $stdout = old_stdout.is_a?(StringIO) ? old_stdout : StringIO.new('', 'w')
501
539
  begin
502
- expect(cli.execute(['abcd'])).to(eq(2))
540
+ expect(cli.execute(['abcd'])).to(eq(102))
503
541
  expect($stdout.string).to(eq("invalid command argument \"abcd\"\n"))
504
542
  ensure
505
543
  $stdout = old_stdout
@@ -6,43 +6,18 @@ require 'cliqr/error'
6
6
 
7
7
  require 'fixtures/test_command'
8
8
 
9
- describe Cliqr::CLI::Executor do
9
+ describe Cliqr::Executor do
10
10
  it 'can execute help action to get help for base command' do
11
11
  cli = Cliqr.interface do
12
12
  name 'my-command'
13
13
  description 'test command has no description'
14
- handler TestCommand
14
+ color :disable
15
15
  end
16
16
 
17
17
  result = cli.execute_internal ['help'], output: :buffer
18
18
  expect(result[:stdout]).to eq <<-EOS
19
19
  my-command -- test command has no description
20
20
 
21
- USAGE:
22
- my-command [actions] [options] [arguments]
23
-
24
- Available options:
25
-
26
- --help, -h : Get helpful information for action "my-command" along with its usage information.
27
-
28
- Available actions:
29
- [ Type "my-command help [action-name]" to get more information about that action ]
30
-
31
- help -- The help action for command "my-command" which provides details and usage information on how to use the command.
32
- EOS
33
- end
34
-
35
- it 'can execute help option to get help for base command' do
36
- cli = Cliqr.interface do
37
- name 'my-command'
38
- description 'test command has no description'
39
- handler TestCommand
40
- end
41
-
42
- result = cli.execute_internal ['--help'], output: :buffer
43
- expect(result[:stdout]).to eq <<-EOS
44
- my-command -- test command has no description
45
-
46
21
  USAGE:
47
22
  my-command [actions] [options] [arguments]
48
23
 
@@ -61,16 +36,13 @@ Available actions:
61
36
  cli = Cliqr.interface do
62
37
  name :my_command
63
38
  description 'test command has no description'
64
- handler TestCommand
39
+ color :disable
65
40
 
66
41
  action :action_1 do
67
42
  description 'test action'
68
- handler TestCommand
69
- shell :disable
70
43
 
71
44
  action :sub_action do
72
45
  description 'This is a sub action.'
73
- handler TestCommand
74
46
  end
75
47
 
76
48
  option :temp do
@@ -80,7 +52,6 @@ Available actions:
80
52
 
81
53
  action :action_2 do
82
54
  description 'another cool action for the base command'
83
- handler TestCommand
84
55
  end
85
56
  end
86
57
 
@@ -100,7 +71,6 @@ Available actions:
100
71
  [ Type "my_command action_1 help [action-name]" to get more information about that action ]
101
72
 
102
73
  sub_action -- This is a sub action.
103
-
104
74
  help -- The help action for command "my_command action_1" which provides details and usage information on how to use the command.
105
75
  EOS
106
76
  end
@@ -109,15 +79,13 @@ Available actions:
109
79
  cli = Cliqr.interface do
110
80
  name :my_command
111
81
  description 'test command has no description'
112
- handler TestCommand
82
+ color :disable
113
83
 
114
84
  action :action_1 do
115
85
  description 'test action'
116
- handler TestCommand
117
86
 
118
87
  action :sub_action do
119
88
  description 'This is a sub action.'
120
- handler TestCommand
121
89
  end
122
90
 
123
91
  option :temp do
@@ -127,7 +95,6 @@ Available actions:
127
95
 
128
96
  action :action_2 do
129
97
  description 'another cool action for the base command'
130
- handler TestCommand
131
98
  end
132
99
  end
133
100
 
@@ -153,16 +120,14 @@ Available actions:
153
120
  cli = Cliqr.interface do
154
121
  name :my_command
155
122
  description 'test command has no description'
156
- handler TestCommand
157
123
  shell :disable
124
+ color :disable
158
125
 
159
126
  action :action_1 do
160
127
  description 'test action'
161
- handler TestCommand
162
128
 
163
129
  action :sub_action do
164
130
  description 'This is a sub action.'
165
- handler TestCommand
166
131
  end
167
132
 
168
133
  option :temp do
@@ -172,7 +137,6 @@ Available actions:
172
137
 
173
138
  action :action_2 do
174
139
  description 'another cool action for the base command'
175
- handler TestCommand
176
140
  end
177
141
  end
178
142
 
@@ -191,9 +155,7 @@ Available actions:
191
155
  [ Type "my_command help [action-name]" to get more information about that action ]
192
156
 
193
157
  action_1 -- test action
194
-
195
158
  action_2 -- another cool action for the base command
196
-
197
159
  help -- The help action for command "my_command" which provides details and usage information on how to use the command.
198
160
  EOS
199
161
  end
@@ -202,7 +164,7 @@ Available actions:
202
164
  cli = Cliqr.interface do
203
165
  name 'my-command'
204
166
  description 'test command has no description'
205
- handler TestCommand
167
+ color :disable
206
168
  end
207
169
 
208
170
  result = cli.execute_internal ['--help'], output: :buffer
@@ -227,11 +189,9 @@ Available actions:
227
189
  cli = Cliqr.interface do
228
190
  name :my_command
229
191
  description 'test command has no description'
230
- handler TestCommand
192
+ color :disable
231
193
 
232
- action :action_1 do
233
- handler TestCommand
234
- end
194
+ action :action_1
235
195
  end
236
196
 
237
197
  result = cli.execute_internal %w(--help action_1), output: :buffer
@@ -256,11 +216,10 @@ Available actions:
256
216
  cli = Cliqr.interface do
257
217
  name :my_command
258
218
  description 'test command has no description'
259
- handler TestCommand
219
+ color :disable
260
220
 
261
221
  action :action_1 do
262
222
  description 'test action'
263
- handler TestCommand
264
223
  end
265
224
  end
266
225
 
@@ -286,11 +245,10 @@ Available actions:
286
245
  cli = Cliqr.interface do
287
246
  name :my_command
288
247
  description 'test command has no description'
289
- handler TestCommand
248
+ color :disable
290
249
 
291
250
  action :action_1 do
292
251
  description 'test action'
293
- handler TestCommand
294
252
  end
295
253
  end
296
254
 
@@ -307,11 +265,9 @@ USAGE:
307
265
  cli = Cliqr.interface do
308
266
  name :my_command
309
267
  description 'test command has no description'
310
- handler TestCommand
311
268
 
312
269
  action :action_1 do
313
270
  description 'test action'
314
- handler TestCommand
315
271
  end
316
272
  end
317
273
 
@@ -321,63 +277,19 @@ USAGE:
321
277
  "Cause: Cliqr::Error::IllegalArgumentError - too many arguments for \"my_command help\" command\n"))
322
278
  end
323
279
 
324
- it 'can forward command to another action' do
325
- cli = Cliqr.interface do
326
- name :my_command
327
- description 'test command has no description'
328
- handler TestCommand
329
-
330
- action :action_1 do
331
- description 'test action'
332
- handler do
333
- puts 'in action_1'
334
- forward 'my_command action_2 sub-action' # starting with base command name
335
- puts 'back in action_1'
336
- end
337
- end
338
-
339
- action 'action_2' do
340
- handler do
341
- puts 'in action_2'
342
- end
343
-
344
- action 'sub-action' do
345
- handler do
346
- puts 'in sub-action'
347
- forward 'action_2' # not starting with base command name
348
- puts 'back in sub-action'
349
- end
350
- end
351
- end
352
- end
353
-
354
- result = cli.execute_internal ['action_1'], output: :buffer
355
- expect(result[:stdout]).to eq <<-EOS
356
- in action_1
357
- in sub-action
358
- in action_2
359
- back in sub-action
360
- back in action_1
361
- EOS
362
- end
363
-
364
280
  it 'executes help for action without handler' do
365
281
  cli = Cliqr.interface do
366
282
  name :my_command
367
283
  description 'test command has no description'
368
284
  shell :disable
285
+ color :disable
369
286
 
370
287
  action :action_1 do
371
288
  description 'test action'
372
- handler TestCommand
373
289
  end
374
290
 
375
291
  action 'action_2' do
376
- shell :disable
377
-
378
- action 'sub-action' do
379
- handler TestCommand
380
- end
292
+ action 'sub-action'
381
293
  end
382
294
  end
383
295
 
@@ -396,9 +308,7 @@ Available actions:
396
308
  [ Type "my_command help [action-name]" to get more information about that action ]
397
309
 
398
310
  action_1 -- test action
399
-
400
311
  action_2
401
-
402
312
  help -- The help action for command "my_command" which provides details and usage information on how to use the command.
403
313
  EOS
404
314
 
@@ -417,8 +327,102 @@ Available actions:
417
327
  [ Type "my_command action_2 help [action-name]" to get more information about that action ]
418
328
 
419
329
  sub-action
420
-
421
330
  help -- The help action for command "my_command action_2" which provides details and usage information on how to use the command.
422
331
  EOS
423
332
  end
333
+
334
+ it 'can display help in color' do
335
+ cli = Cliqr.interface do
336
+ name :my_command
337
+ description 'test command has no description'
338
+
339
+ action :foo do
340
+ description 'test action'
341
+
342
+ action :bar do
343
+ description 'this is a bar'
344
+
345
+ option :opt1 do
346
+ type :numeric
347
+ default 123
348
+ description 'a temporary option'
349
+ end
350
+ end
351
+ end
352
+ end
353
+
354
+ result = cli.execute_internal %w(foo help bar), output: :buffer
355
+ expect(result[:stdout]).to eq <<-EOS
356
+ my_command foo bar -- this is a bar
357
+
358
+ USAGE:
359
+ my_command foo bar [actions] [options] [arguments]
360
+
361
+ Available options:
362
+
363
+ --opt1 : <numeric> a temporary option (default => 123)
364
+ --help, -h : Get helpful information for action "my_command foo bar" along with its usage information.
365
+
366
+ Available actions:
367
+ [ Type "my_command foo bar help [action-name]" to get more information about that action ]
368
+ 
369
+ help -- The help action for command "my_command foo bar" which provides details and usage information on how to use the command.
370
+ EOS
371
+ end
372
+
373
+ it 'can use options in shell config' do
374
+ cli = Cliqr.interface do
375
+ name 'my-command'
376
+ handler TestCommand
377
+ color :disable
378
+
379
+ shell do
380
+ name 'b00m'
381
+ description 'this is a custom shell implementation'
382
+
383
+ option :foo
384
+
385
+ option :bar do
386
+ type :boolean
387
+ default true
388
+ description 'some bar'
389
+ end
390
+
391
+ option :baz do
392
+ type :numeric
393
+ default 10
394
+ end
395
+ end
396
+ end
397
+
398
+ result = cli.execute_internal %w(help b00m), output: :buffer
399
+ expect(result[:stdout]).to eq <<-EOS
400
+ my-command b00m -- this is a custom shell implementation
401
+
402
+ USAGE:
403
+ my-command b00m [options]
404
+
405
+ Available options:
406
+
407
+ --foo
408
+ --[no-]bar : <boolean> some bar (default => true)
409
+ --baz : <numeric> (default => 10)
410
+ EOS
411
+ end
412
+
413
+ it 'does not allow version command to have arguments' do
414
+ cli = Cliqr.interface do
415
+ name 'my-command'
416
+ color :disable
417
+ version '1.2.3.4'
418
+ end
419
+
420
+ result = cli.execute_internal %w(help version), output: :buffer
421
+ expect(result[:stdout]).to eq <<-EOS
422
+ my-command version -- Get version information for command "my-command".
423
+
424
+ USAGE:
425
+ my-command version
426
+ EOS
427
+ end
424
428
  end