cliqr 2.0.0 → 2.1.1

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
  SHA1:
3
- metadata.gz: 5305e3f607fd5cbf6d6c55840630671129ec96bf
4
- data.tar.gz: 21995e4b7b23b3c3d29011eb9b42a614ca54d38a
3
+ metadata.gz: 2892f4cce1901d6018999df394a46e27d591af4b
4
+ data.tar.gz: 709e7b21684d0f463382e4c3b1c49fd59b874bb9
5
5
  SHA512:
6
- metadata.gz: b2e59b44f6b5656afbd8e75039fbf0cd46fff0421b1923fb8b3cf04f2972ad96359dbbef31f205190476123f80b5b686b3270db6f603329f6ab2194be6771e08
7
- data.tar.gz: 3b0b7571dcbf6d460e4b61fbbfee83923e2c784c450311a00060ecd828ec17ba2a70820d4f8302ac9dbd27b2ba2189f2e713a96ba78fcc8bf66b98140ebf52b2
6
+ metadata.gz: e11d059ede7010a84baa7dfe33ccf85b1776534c2f5b9ca87d6840519e376c46feca6c84d48ce69d9b1c86316623e548bf34f054e32521aa2344916a3b3fb588
7
+ data.tar.gz: 3e340c1aff3fc1a664345d08406ad8e11b1b5ab7178f9e22fe31ad743aa8da8949b88ead8eda1ed67c326e0dd7b10d06a23e76e58411acca62a428fb6a2af793
@@ -5,6 +5,33 @@ item in this nested table for further details.
5
5
  <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
6
6
  **Table of Contents**
7
7
 
8
+ - [2.1.1 / 2015-07-15](#211--2015-07-15)
9
+ - [Bugfix](#bugfix)
10
+ - [2.1.0 / 2015-07-15](#210--2015-07-15)
11
+ - [Features](#features)
12
+ - [Make use of `readline` lib for shell mode.](#make-use-of-readline-lib-for-shell-mode)
13
+ - [Bugfix](#bugfix)
14
+ - [Handle spaces in command arguments](#handle-spaces-in-command-arguments)
15
+ - [Minor improvements](#minor-improvements)
16
+ - [Use shortened URl for gif in README](#use-shortened-url-for-gif-in-readme)
17
+ - [2.0.0 / 2015-07-09](#200--2015-07-09)
18
+ - [Features](#features)
19
+ - [Event handling](#event-handling)
20
+ - [Default events for shell](#default-events-for-shell)
21
+ - [Colors](#colors)
22
+ - [Customizable banner and prompt for shell](#customizable-banner-and-prompt-for-shell)
23
+ - [Backward incompatible changes](#backward-incompatible-changes)
24
+ - [Improvements](#improvements)
25
+ - [Partial templated](#partial-templated)
26
+ - [Improve default prompt](#improve-default-prompt)
27
+ - [Screen capture in README](#screen-capture-in-readme)
28
+ - [Minor changes](#minor-changes)
29
+ - [Shorten the quickstart example](#shorten-the-quickstart-example)
30
+ - [Re-organize code and move specs around](#re-organize-code-and-move-specs-around)
31
+ - [Multi OS testing suprted added to CI](#multi-os-testing-suprted-added-to-ci)
32
+ - [Bugfixes](#bugfixes)
33
+ - [Fix examples to follow new breaking shell config change](#fix-examples-to-follow-new-breaking-shell-config-change)
34
+ - [Do not put allow arguments in shell config](#do-not-put-allow-arguments-in-shell-config)
8
35
  - [1.2.0 / 2015-06-18](#120--2015-06-18)
9
36
  - [Features](#features)
10
37
  - [Nested actions](#nested-actions)
@@ -91,6 +118,40 @@ item in this nested table for further details.
91
118
 
92
119
  <!-- markdown-toc end -->
93
120
 
121
+ 2.1.1 / 2015-07-15
122
+ ==================
123
+
124
+ ## Bugfix
125
+
126
+ Fix the version tag in gem
127
+
128
+ 2.1.0 / 2015-07-15
129
+ ==================
130
+
131
+ ## Features
132
+
133
+ ### Make use of `readline` lib for shell mode.
134
+
135
+ This gives us a huge advantage by supporting command history and meta
136
+ character handling out of the box.
137
+
138
+ Some tweaks will come in smaller patches soon.
139
+
140
+ ## Bugfix
141
+
142
+ ### Handle spaces in command arguments
143
+
144
+ Reported by @AlgyTaylor.
145
+
146
+ We were not able to parse a argument as a continuous value if it had a
147
+ space in it. This was fixed by using `shellwords`.
148
+
149
+ ## Minor improvements
150
+
151
+ ### Use shortened URl for gif in README
152
+
153
+ This allows a little bit of analytics for the trafic to the git repo.
154
+
94
155
  2.0.0 / 2015-07-09
95
156
  ==================
96
157
 
@@ -100,7 +161,8 @@ Another big release!
100
161
 
101
162
  ### Event handling
102
163
 
103
- Added ability to invoke arbitiary events and define handlers to handle certain kind of events.
164
+ Added ability to invoke arbitiary events and define handlers to handle
165
+ certain kind of events.
104
166
 
105
167
  Here is an example:
106
168
  ``` ruby
@@ -142,15 +204,19 @@ Events can be chained as above example shows.
142
204
 
143
205
  #### Default events for shell
144
206
 
145
- When a shell starts a `shell_start` event is invoked. Upon exit, a `shell_exit` event is invoked.
207
+ When a shell starts a `shell_start` event is invoked. Upon exit, a
208
+ `shell_exit` event is invoked.
146
209
 
147
210
  ### Colors
148
211
 
149
- Enabled colors in command handlers and usage output. Just call a function with the name of the color you want. Colors can also be disabled.
212
+ Enabled colors in command handlers and usage output. Just call a
213
+ function with the name of the color you want. Colors can also be
214
+ disabled.
150
215
 
151
- ### Customizable banner and prompt for shell
216
+ ### Customizable banner and prompt for shell
152
217
 
153
- The shell action now allows you to configure the banner displayed in the beginning and define a method to build the prompt.
218
+ The shell action now allows you to configure the banner displayed in the
219
+ beginning and define a method to build the prompt.
154
220
 
155
221
  ## Backward incompatible changes
156
222
 
@@ -160,7 +226,8 @@ The shell action can only be used in the base command config.
160
226
 
161
227
  ### Partial templated
162
228
 
163
- Templates are reused to build help doc by incorporation of partial erb templating.
229
+ Templates are reused to build help doc by incorporation of partial erb
230
+ templating.
164
231
 
165
232
  ### Improve default prompt
166
233
 
data/README.md CHANGED
@@ -72,9 +72,7 @@ cli.execute(ARGV)
72
72
  ```
73
73
 
74
74
  Here is a screen capture of this script in action:
75
- ![greet script demo]
76
- (https://raw.githubusercontent.com/anshulverma/cliqr/master/screenshots/greet.gif
77
- "Binary tree example")
75
+ ![greet script demo](https://goo.gl/sJgxFX "Binary tree example")
78
76
 
79
77
  ## Installation
80
78
 
@@ -3,6 +3,8 @@
3
3
  require 'cliqr/command/color'
4
4
  require 'cliqr/command/argument_operator_context'
5
5
  require 'cliqr/events/invoker'
6
+ require 'cliqr/command/output/standard_output_stream'
7
+ require 'cliqr/command/output/file_output_stream'
6
8
 
7
9
  module Cliqr
8
10
  # Definition and builder for command context
@@ -53,7 +55,7 @@ module Cliqr
53
55
  # Initialize the command context (called by the CommandContextBuilder)
54
56
  #
55
57
  # @return [Cliqr::Command::CommandContext]
56
- def initialize(config, options, arguments, environment, executor)
58
+ def initialize(config, options, arguments, environment, executor, output_stream)
57
59
  super(config)
58
60
 
59
61
  @config = config
@@ -64,6 +66,7 @@ module Cliqr
64
66
  @environment = environment
65
67
  @executor = executor
66
68
  @event_invoker = Events::Invoker.new(config, self)
69
+ @output_stream = output_stream
67
70
 
68
71
  # make option map from array
69
72
  @options = Hash[*options.collect { |option| [option.name, option] }.flatten]
@@ -162,7 +165,14 @@ module Cliqr
162
165
  # @return [Cliqr::Command::CommandContext]
163
166
  def root(environment_type = nil)
164
167
  environment_type = @environment if environment_type.nil?
165
- CommandContext.new(@config.root, [], [], environment_type, @executor)
168
+ CommandContext.new(@config.root, [], [], environment_type, @executor, @output_stream)
169
+ end
170
+
171
+ # Override the default puts implementation to enable output buffering
172
+ #
173
+ # @return [Nothing]
174
+ def puts(message)
175
+ @output_stream.write(message)
166
176
  end
167
177
 
168
178
  private :initialize, :default
@@ -201,7 +211,22 @@ module Cliqr
201
211
  option_contexts,
202
212
  @parsed_input.arguments,
203
213
  @options[:environment],
204
- @executor
214
+ @executor,
215
+ build_output_stream(@options)
216
+ end
217
+
218
+ private
219
+
220
+ # Build a output stream object to be used in the command handler
221
+ #
222
+ # @return [OutputStream]
223
+ def build_output_stream(options)
224
+ case options[:output]
225
+ when :file
226
+ Command::Output::FileOutputStream.new
227
+ else
228
+ Command::Output::StandardOutputStream.new
229
+ end
205
230
  end
206
231
  end
207
232
 
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Cliqr
4
+ module Command
5
+ module Output
6
+ # Write output to a file
7
+ #
8
+ # @api private
9
+ class FileOutputStream
10
+ # Write to a file and flush the stream
11
+ #
12
+ # @return [Nothing]
13
+ def write(message)
14
+ puts message
15
+ $stdout.flush
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Cliqr
4
+ module Command
5
+ # Control how output is written out to the stream
6
+ #
7
+ # @api private
8
+ module Output
9
+ # Standard output stream writer
10
+ class StandardOutputStream
11
+ # Write a message directly to the output stream
12
+ #
13
+ # @return [Nothing]
14
+ def write(message)
15
+ puts message
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'readline'
4
+
3
5
  module Cliqr
4
6
  # @api private
5
7
  module Command
@@ -20,12 +22,12 @@ module Cliqr
20
22
 
21
23
  root_context = context.root(:shell)
22
24
 
23
- puts banner(root_context, build_proc(@shell_config.banner))
25
+ context.puts banner(root_context, build_proc(@shell_config.banner))
24
26
 
25
27
  context.invoke(:shell_start)
26
28
  exit_code = build_runner(context, root_context).run
27
29
  context.invoke(:shell_stop)
28
- puts "shell exited with code #{exit_code}"
30
+ context.puts "shell exited with code #{exit_code}"
29
31
  exit_code
30
32
  end
31
33
 
@@ -105,20 +107,21 @@ module Cliqr
105
107
  return if command.empty?
106
108
  action_name = command.split(' ').first
107
109
  unless @context.action?(action_name)
108
- puts "unknown action \"#{action_name}\""
110
+ @context.puts "unknown action \"#{action_name}\""
109
111
  return Cliqr::Executor::ExitCode.code(nil)
110
112
  end
111
113
  @context.forward("#{@base_command} #{command}", :environment => @context.environment)
112
114
  rescue StandardError => e
113
- puts e.message
115
+ @context.puts e.message
114
116
  end
115
117
 
116
118
  # Show a prompt and ask for input
117
119
  #
118
120
  # @return [String]
119
121
  def prompt
120
- print @context.instance_eval(&@prompt)
121
- $stdin.gets.chomp
122
+ Readline.input = $stdin
123
+ Readline.output = $stdout
124
+ Readline.readline(@context.instance_eval(&@prompt), true)
122
125
  end
123
126
  end
124
127
  end
@@ -16,13 +16,10 @@ module Cliqr
16
16
  # @return [Cliqr::Executor::BufferedCommandRunner] If command's output needs to be buffered
17
17
  def self.get(**options)
18
18
  case options[:output]
19
- when :default
20
- StandardCommandRunner.new
21
19
  when :buffer
22
20
  BufferedCommandRunner.new
23
21
  else
24
- fail Cliqr::Error::UnknownCommandRunnerException,
25
- 'cannot find a command runner for the given options'
22
+ StandardCommandRunner.new
26
23
  end
27
24
  end
28
25
  end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'shellwords'
3
4
  require 'cliqr/command/shell_command'
4
5
 
5
6
  module Cliqr
@@ -137,9 +138,9 @@ module Cliqr
137
138
  def self.sanitize_args(args, config = nil)
138
139
  sanitized = []
139
140
  if args.is_a?(Array)
140
- args.each { |arg| sanitized.concat(sanitize_args(arg)) }
141
+ sanitized = args
141
142
  elsif args.is_a?(String)
142
- sanitized = args.split(' ')
143
+ sanitized = Shellwords.shellwords(args)
143
144
  end
144
145
  remove_base_command(sanitized, config)
145
146
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Versioned gem
4
4
  module Cliqr
5
- VERSION = '2.0.0'
5
+ VERSION = '2.1.1'
6
6
  end
@@ -67,7 +67,7 @@ describe Cliqr::Command::Color do
67
67
  end
68
68
  end
69
69
  end
70
- result = cli.execute_internal ['my-command foo'], output: :buffer
70
+ result = cli.execute_internal %w(my-command foo), output: :buffer
71
71
  expect(result[:stdout]).to eq <<-EOS
72
72
  \e[31mthis should be colorized in red\e[0m
73
73
  EOS
@@ -98,7 +98,7 @@ this should not be colorized
98
98
  end
99
99
  end
100
100
  end
101
- result = cli.execute_internal ['my-command foo'], output: :buffer
101
+ result = cli.execute_internal %w(my-command foo), output: :buffer
102
102
  expect(result[:stdout]).to eq <<-EOS
103
103
  this should not be colorized
104
104
  EOS
@@ -117,7 +117,7 @@ this should not be colorized
117
117
  end
118
118
  end
119
119
  end
120
- result = cli.execute_internal ['my-command foo bar'], output: :buffer
120
+ result = cli.execute_internal %w(my-command foo bar), output: :buffer
121
121
  expect(result[:stdout]).to eq <<-EOS
122
122
  this should not be colorized
123
123
  EOS
@@ -14,8 +14,7 @@ describe Cliqr::Executor::CommandRunnerFactory do
14
14
  end
15
15
 
16
16
  it 'throws error for default output type' do
17
- expect { Cliqr::Executor::CommandRunnerFactory.get(output: :unknown) }.to(
18
- raise_error(be_kind_of(Cliqr::Error::UnknownCommandRunnerException))
19
- )
17
+ runner = Cliqr::Executor::CommandRunnerFactory.get(output: :unknown)
18
+ expect(runner).to be_kind_of(Cliqr::Executor::StandardCommandRunner)
20
19
  end
21
20
  end
@@ -162,7 +162,7 @@ Time
162
162
  end
163
163
  end
164
164
  end
165
- result = cli.execute_internal ['my-command my_action'], output: :buffer
165
+ result = cli.execute_internal 'my-command my_action', output: :buffer
166
166
  expect(result[:stdout]).to eq <<-EOS
167
167
  invoked action
168
168
  invoked bar : my-command my_action : a : 1
@@ -198,7 +198,7 @@ action ending
198
198
  end
199
199
  end
200
200
  end
201
- result = cli.execute_internal ['my-command my_action'], output: :buffer
201
+ result = cli.execute_internal 'my-command my_action', output: :buffer
202
202
  expect(result[:stdout]).to eq <<-EOS
203
203
  invoked bar : my-command my_action : a : 1
204
204
  invoked foo : my-command my_action : b : 2 : {:t=>1}
@@ -256,16 +256,16 @@ bar
256
256
  end
257
257
  end
258
258
  end
259
- result = cli.execute_internal ['my-command'], output: :buffer
259
+ result = cli.execute_internal 'my-command', output: :buffer
260
260
  expect(result[:stdout]).to eq <<-EOS
261
261
  invoked first : my-command : a : 1
262
262
  EOS
263
- result = cli.execute_internal ['my-command foo'], output: :buffer
263
+ result = cli.execute_internal 'my-command foo', output: :buffer
264
264
  expect(result[:stdout]).to eq <<-EOS
265
265
  invoked second : my-command foo : b : 2
266
266
  invoked second : my-command foo : b : 2
267
267
  EOS
268
- result = cli.execute_internal ['my-command foo bar'], output: :buffer
268
+ result = cli.execute_internal 'my-command foo bar', output: :buffer
269
269
  expect(result[:stdout]).to eq <<-EOS
270
270
  invoked first : my-command foo bar : b : 2
271
271
  invoked first : my-command foo bar : b : 2
@@ -273,7 +273,7 @@ invoked first : my-command foo bar : b : 2
273
273
  invoked third : my-command foo bar : d : 4
274
274
  invoked third : my-command foo bar : d : 4
275
275
  EOS
276
- result = cli.execute_internal ['my-command foo baz'], output: :buffer
276
+ result = cli.execute_internal 'my-command foo baz', output: :buffer
277
277
  expect(result[:stdout]).to eq <<-EOS
278
278
  invoked second : my-command foo baz : e : 5
279
279
  invoked second : my-command foo baz : e : 5
@@ -298,7 +298,7 @@ invoked second : my-command foo baz : e : 5
298
298
  end
299
299
  end
300
300
  end
301
- result = cli.execute_internal ['my-command foo'], output: :buffer
301
+ result = cli.execute_internal %w(my-command foo), output: :buffer
302
302
  expect(result[:stdout]).to eq <<-EOS
303
303
  should be invoked
304
304
  EOS
@@ -332,7 +332,7 @@ should be invoked
332
332
  option :abc
333
333
  end
334
334
  end
335
- result = cli.execute_internal ['my-command my_action --opt val'], output: :buffer
335
+ result = cli.execute_internal 'my-command my_action --opt val', output: :buffer
336
336
  expect(result[:stdout]).to eq <<-EOS
337
337
  invoked bar
338
338
  true
@@ -491,6 +491,34 @@ back in action_1
491
491
  EOS
492
492
  end
493
493
 
494
+ it 'can forward command with space in arguments' do
495
+ cli = Cliqr.interface do
496
+ name :my_command
497
+
498
+ action :foo do
499
+ handler do
500
+ forward 'bar --opt1 "simple value" --opt2 "a question"?'
501
+ end
502
+ end
503
+
504
+ action :bar do
505
+ option :opt1
506
+ option :opt2
507
+
508
+ handler do
509
+ puts opt1
510
+ puts opt2
511
+ end
512
+ end
513
+ end
514
+
515
+ result = cli.execute_internal ['foo'], output: :buffer
516
+ expect(result[:stdout]).to eq <<-EOS
517
+ simple value
518
+ a question?
519
+ EOS
520
+ end
521
+
494
522
  describe 'error handling' do
495
523
  it 'returns 0 if no error' do
496
524
  cli = Cliqr.interface do
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'tempfile'
4
+
3
5
  require 'spec_helper'
4
6
 
5
7
  require 'fixtures/test_command'
@@ -25,40 +27,40 @@ describe Cliqr::Command::ShellCommand do
25
27
  end
26
28
  end
27
29
 
28
- with_input(%w(help -h)) do
29
- result = cli.execute_internal %w(my-command shell), output: :buffer
30
- expect(result[:stdout]).to eq <<-EOS
31
- Starting shell for command "my-command"
30
+ with_input_output(%w(help -h)) do |output|
31
+ cli.execute_internal %w(my-command shell), output: :file
32
+ expect(output.call).to eq <<-EOS
33
+ Starting shell for command "my-command".
32
34
  [my-command][1] $ help.
33
- my-command -- this is a test command
34
-
35
- Available actions:
36
- [ Type "help [action-name]" to get more information about that action ]
37
-
38
- foo -- the foo action
39
- bar -- bar command
40
- help -- The help action for command "my-command" which provides details and usage information on how to use the command.
35
+ my-command -- this is a test command.
36
+ .
37
+ Available actions:.
38
+ [ Type "help [action-name]" to get more information about that action ].
39
+ .
40
+ foo -- the foo action.
41
+ bar -- bar command.
42
+ help -- The help action for command "my-command" which provides details and usage information on how to use the command..
41
43
  [my-command][2] $ -h.
42
- unknown action "-h"
44
+ unknown action "-h".
43
45
  [my-command][3] $ exit.
44
- shell exited with code 0
46
+ shell exited with code 0.
45
47
  EOS
46
48
  end
47
49
 
48
- with_input(['help bar']) do
49
- result = cli.execute_internal %w(my-command shell), output: :buffer
50
- expect(result[:stdout]).to eq <<-EOS
51
- Starting shell for command "my-command"
50
+ with_input_output(['help bar']) do |output|
51
+ cli.execute_internal %w(my-command shell), output: :file
52
+ expect(output.call).to eq <<-EOS
53
+ Starting shell for command "my-command".
52
54
  [my-command][4] $ help bar.
53
- my-command bar -- bar command
54
-
55
- Available actions:
56
- [ Type "help [action-name]" to get more information about that action ]
57
-
58
- baz
59
- help -- The help action for command "my-command bar" which provides details and usage information on how to use the command.
55
+ my-command bar -- bar command.
56
+ .
57
+ Available actions:.
58
+ [ Type "help [action-name]" to get more information about that action ].
59
+ .
60
+ baz.
61
+ help -- The help action for command "my-command bar" which provides details and usage information on how to use the command..
60
62
  [my-command][5] $ exit.
61
- shell exited with code 0
63
+ shell exited with code 0.
62
64
  EOS
63
65
  end
64
66
  end
@@ -75,20 +77,20 @@ shell exited with code 0
75
77
  end
76
78
  end
77
79
 
78
- with_input(%w(help)) do
79
- result = cli.execute_internal %w(my-command custom-shell), output: :buffer
80
- expect(result[:stdout]).to eq <<-EOS
81
- Starting shell for command "my-command"
80
+ with_input_output(%w(help)) do |output|
81
+ cli.execute_internal %w(my-command custom-shell), output: :file
82
+ expect(output.call).to eq <<-EOS
83
+ Starting shell for command "my-command".
82
84
  [my-command][1] $ help.
83
- my-command -- this is a test command
84
-
85
- Available actions:
86
- [ Type "help [action-name]" to get more information about that action ]
87
-
88
- help -- The help action for command "my-command" which provides details and usage information on how to use the command.
89
- custom-shell -- this is a custom shell
85
+ my-command -- this is a test command.
86
+ .
87
+ Available actions:.
88
+ [ Type "help [action-name]" to get more information about that action ].
89
+ .
90
+ help -- The help action for command "my-command" which provides details and usage information on how to use the command..
91
+ custom-shell -- this is a custom shell.
90
92
  [my-command][2] $ exit.
91
- shell exited with code 0
93
+ shell exited with code 0.
92
94
  EOS
93
95
  end
94
96
  end
@@ -118,29 +120,29 @@ shell exited with code 0
118
120
  end
119
121
  end
120
122
 
121
- with_input(['', 'my-command', 'foo', 'foo bar', 'foo bar --opt yes', 'foo bar help']) do
122
- result = cli.execute_internal %w(my-command shell), output: :buffer
123
- expect(result[:stdout]).to eq <<-EOS
124
- Starting shell for command "my-command"
123
+ with_input_output(['', 'my-command', 'foo', 'foo bar', 'foo bar --opt yes', 'foo bar help']) do |output|
124
+ cli.execute_internal %w(my-command shell), output: :file
125
+ expect(output.call).to eq <<-EOS
126
+ Starting shell for command "my-command".
125
127
  [my-command][1] $ .
126
128
  [my-command][2] $ my-command.
127
- unknown action "my-command"
129
+ unknown action "my-command".
128
130
  [my-command][3] $ foo.
129
- foo executed
131
+ foo executed.
130
132
  [my-command][4] $ foo bar.
131
- bar executed
133
+ bar executed.
132
134
  [my-command][5] $ foo bar --opt yes.
133
- bar executed
134
- option: yes
135
+ bar executed.
136
+ option: yes.
135
137
  [my-command][6] $ foo bar help.
136
- my-command foo bar
137
-
138
- Available actions:
139
- [ Type "help [action-name]" to get more information about that action ]
140
-
141
- help -- The help action for command "my-command foo bar" which provides details and usage information on how to use the command.
138
+ my-command foo bar.
139
+ .
140
+ Available actions:.
141
+ [ Type "help [action-name]" to get more information about that action ].
142
+ .
143
+ help -- The help action for command "my-command foo bar" which provides details and usage information on how to use the command..
142
144
  [my-command][7] $ exit.
143
- shell exited with code 0
145
+ shell exited with code 0.
144
146
  EOS
145
147
  end
146
148
  end
@@ -170,20 +172,20 @@ shell exited with code 0
170
172
  end
171
173
  end
172
174
 
173
- with_input(['unknown', '--opt-1 val', 'foo']) do
174
- result = cli.execute_internal %w(my-command shell), output: :buffer
175
- expect(result[:stdout]).to eq <<-EOS
176
- Starting shell for command "my-command"
175
+ with_input_output(['unknown', '--opt-1 val', 'foo']) do |output|
176
+ cli.execute_internal %w(my-command shell), output: :file
177
+ expect(output.call).to eq <<-EOS
178
+ Starting shell for command "my-command".
177
179
  [my-command][1] $ unknown.
178
- unknown action "unknown"
180
+ unknown action "unknown".
179
181
  [my-command][2] $ --opt-1 val.
180
- unknown action "--opt-1"
182
+ unknown action "--opt-1".
181
183
  [my-command][3] $ foo.
182
- command 'my-command foo' failed
183
-
184
- Cause: StandardError - I failed!
184
+ command 'my-command foo' failed.
185
+ .
186
+ Cause: StandardError - I failed!.
185
187
  [my-command][4] $ exit.
186
- shell exited with code 0
188
+ shell exited with code 0.
187
189
  EOS
188
190
  end
189
191
  end
@@ -249,41 +251,41 @@ shell exited with code 0
249
251
  end
250
252
  end
251
253
 
252
- with_input(['']) do
253
- result = cli.execute_internal %w(my-command shell -f qwerty --no-bar), output: :buffer
254
- expect(result[:stdout]).to eq <<-EOS
255
- Starting shell for command "my-command"
256
- shell started
257
- true
258
- qwerty
259
- true
260
- false
261
- false
262
- 10
263
- base: shell started
264
- true
265
- qwerty
266
- true
267
- false
268
- false
269
- 10
254
+ with_input_output(['']) do |output|
255
+ cli.execute_internal %w(my-command shell -f qwerty --no-bar), output: :file
256
+ expect(output.call).to eq <<-EOS
257
+ Starting shell for command "my-command".
270
258
  [my-command][1] $ .
271
259
  [my-command][2] $ exit.
272
- shell stopped
273
- true
274
- qwerty
275
- true
276
- false
277
- false
278
- 10
279
- base: shell stopped
280
- true
281
- qwerty
282
- true
283
- false
284
- false
285
- 10
286
- shell exited with code 0
260
+ shell started.
261
+ true.
262
+ qwerty.
263
+ true.
264
+ false.
265
+ false.
266
+ 10.
267
+ base: shell started.
268
+ true.
269
+ qwerty.
270
+ true.
271
+ false.
272
+ false.
273
+ 10.
274
+ shell stopped.
275
+ true.
276
+ qwerty.
277
+ true.
278
+ false.
279
+ false.
280
+ 10.
281
+ base: shell stopped.
282
+ true.
283
+ qwerty.
284
+ true.
285
+ false.
286
+ false.
287
+ 10.
288
+ shell exited with code 0.
287
289
  EOS
288
290
  end
289
291
  end
@@ -310,16 +312,16 @@ shell exited with code 0
310
312
  end
311
313
  end
312
314
 
313
- with_input(['shell']) do
314
- result = cli.execute_internal %w(my-command shell), output: :buffer
315
- expect(result[:stdout]).to eq <<-EOS
316
- Starting shell for command "my-command"
315
+ with_input_output(['shell']) do |output|
316
+ cli.execute_internal %w(my-command shell), output: :file
317
+ expect(output.call).to eq <<-EOS
318
+ Starting shell for command "my-command".
317
319
  [my-command][1] $ shell.
318
- command 'my-command shell' failed
319
-
320
- Cause: Cliqr::Error::IllegalCommandError - Cannot run another shell within an already running shell
320
+ command 'my-command shell' failed.
321
+ .
322
+ Cause: Cliqr::Error::IllegalCommandError - Cannot run another shell within an already running shell.
321
323
  [my-command][2] $ exit.
322
- shell exited with code 0
324
+ shell exited with code 0.
323
325
  EOS
324
326
  end
325
327
  end
@@ -338,16 +340,16 @@ shell exited with code 0
338
340
  raise_error(Cliqr::Error::CommandRuntimeError,
339
341
  "command 'my-command foo' failed\n\nCause: Cliqr::Error::IllegalArgumentError - no arguments allowed for default help action\n"))
340
342
 
341
- with_input(['foo shell']) do
342
- result = cli.execute_internal %w(my-command shell), output: :buffer
343
- expect(result[:stdout]).to eq <<-EOS
344
- Starting shell for command "my-command"
343
+ with_input_output(['foo shell']) do |output|
344
+ cli.execute_internal %w(my-command shell), output: :file
345
+ expect(output.call).to eq <<-EOS
346
+ Starting shell for command "my-command".
345
347
  [my-command][1] $ foo shell.
346
- command 'my-command foo' failed
347
-
348
- Cause: Cliqr::Error::IllegalArgumentError - no arguments allowed for default help action
348
+ command 'my-command foo' failed.
349
+ .
350
+ Cause: Cliqr::Error::IllegalArgumentError - no arguments allowed for default help action.
349
351
  [my-command][2] $ exit.
350
- shell exited with code 0
352
+ shell exited with code 0.
351
353
  EOS
352
354
  end
353
355
  end
@@ -369,16 +371,16 @@ shell exited with code 0
369
371
  end
370
372
  end
371
373
 
372
- with_input(['', '', 'foo']) do
373
- result = cli.execute_internal %w(my-command shell), output: :buffer
374
- expect(result[:stdout]).to eq <<-EOS
375
- Starting shell for command "my-command"
374
+ with_input_output(['', '', 'foo']) do |output|
375
+ cli.execute_internal %w(my-command shell), output: :file
376
+ expect(output.call).to eq <<-EOS
377
+ Starting shell for command "my-command".
376
378
  test-prompt $ .
377
379
  test-prompt $ .
378
380
  test-prompt $ foo.
379
- foo executed
381
+ foo executed.
380
382
  test-prompt $ exit.
381
- shell exited with code 0
383
+ shell exited with code 0.
382
384
  EOS
383
385
  end
384
386
  end
@@ -402,17 +404,17 @@ shell exited with code 0
402
404
  end
403
405
  end
404
406
 
405
- with_input(['', '', 'foo', '']) do
406
- result = cli.execute_internal %w(my-command shell), output: :buffer
407
- expect(result[:stdout]).to eq <<-EOS
408
- Starting shell for command "my-command"
407
+ with_input_output(['', '', 'foo', '']) do |output|
408
+ cli.execute_internal %w(my-command shell), output: :file
409
+ expect(output.call).to eq <<-EOS
410
+ Starting shell for command "my-command".
409
411
  my-command [1]$ .
410
412
  my-command [2]$ .
411
413
  my-command [3]$ foo.
412
- foo executed
414
+ foo executed.
413
415
  my-command [4]$ .
414
416
  my-command [5]$ exit.
415
- shell exited with code 0
417
+ shell exited with code 0.
416
418
  EOS
417
419
  end
418
420
  end
@@ -432,17 +434,17 @@ shell exited with code 0
432
434
  end
433
435
  end
434
436
 
435
- with_input(['', '', 'foo', '']) do
436
- result = cli.execute_internal %w(my-command shell), output: :buffer
437
- expect(result[:stdout]).to eq <<-EOS
438
- Starting shell for command "my-command"
437
+ with_input_output(['', '', 'foo', '']) do |output|
438
+ cli.execute_internal %w(my-command shell), output: :file
439
+ expect(output.call).to eq <<-EOS
440
+ Starting shell for command "my-command".
439
441
  test-prompt [1] > .
440
442
  test-prompt [2] > .
441
443
  test-prompt [3] > foo.
442
- foo executed
444
+ foo executed.
443
445
  test-prompt [4] > .
444
446
  test-prompt [5] > exit.
445
- shell exited with code 0
447
+ shell exited with code 0.
446
448
  EOS
447
449
  end
448
450
  end
@@ -460,17 +462,17 @@ shell exited with code 0
460
462
  end
461
463
  end
462
464
 
463
- with_input(['', '', 'foo', '']) do
464
- result = cli.execute_internal %w(my-command shell), output: :buffer
465
- expect(result[:stdout]).to eq <<-EOS
466
- Starting shell for command "my-command"
465
+ with_input_output(['', '', 'foo', '']) do |output|
466
+ cli.execute_internal %w(my-command shell), output: :file
467
+ expect(output.call).to eq <<-EOS
468
+ Starting shell for command "my-command".
467
469
  [my-command][1] $ .
468
470
  [my-command][2] $ .
469
471
  [my-command][3] $ foo.
470
- foo executed
472
+ foo executed.
471
473
  [my-command][4] $ .
472
474
  [my-command][5] $ exit.
473
- shell exited with code 0
475
+ shell exited with code 0.
474
476
  EOS
475
477
  end
476
478
  end
@@ -490,20 +492,20 @@ shell exited with code 0
490
492
  end
491
493
  end
492
494
 
493
- with_input(['unknown', '--opt-1 val', 'foo']) do
494
- result = cli.execute_internal %w(my-command shell), output: :buffer
495
- expect(result[:stdout]).to eq <<-EOS
496
- Starting shell for command "my-command"
495
+ with_input_output(['unknown', '--opt-1 val', 'foo']) do |output|
496
+ cli.execute_internal %w(my-command shell), output: :file
497
+ expect(output.call).to eq <<-EOS
498
+ Starting shell for command "my-command".
497
499
  [my-command][1] $ unknown.
498
- unknown action "unknown"
500
+ unknown action "unknown".
499
501
  [my-command][2] $ --opt-1 val.
500
- unknown action "--opt-1"
502
+ unknown action "--opt-1".
501
503
  [my-command][3] $ foo.
502
- command 'my-command foo' failed
503
-
504
- Cause: StandardError - I failed!
504
+ command 'my-command foo' failed.
505
+ .
506
+ Cause: StandardError - I failed!.
505
507
  [my-command][4] $ exit.
506
- shell exited with code 0
508
+ shell exited with code 0.
507
509
  EOS
508
510
  end
509
511
  end
@@ -526,20 +528,20 @@ shell exited with code 0
526
528
  end
527
529
  end
528
530
 
529
- with_input(['unknown', '--opt-1 val', 'foo']) do
530
- result = cli.execute_internal %w(my-command shell), output: :buffer
531
- expect(result[:stdout]).to eq <<-EOS
532
- Starting shell for command "my-command"
531
+ with_input_output(['unknown', '--opt-1 val', 'foo']) do |output|
532
+ cli.execute_internal %w(my-command shell), output: :file
533
+ expect(output.call).to eq <<-EOS
534
+ Starting shell for command "my-command".
533
535
  test-prompt [1] > unknown.
534
- unknown action "unknown"
536
+ unknown action "unknown".
535
537
  test-prompt [2] > --opt-1 val.
536
- unknown action "--opt-1"
538
+ unknown action "--opt-1".
537
539
  test-prompt [3] > foo.
538
- command 'my-command foo' failed
539
-
540
- Cause: StandardError - I failed!
540
+ command 'my-command foo' failed.
541
+ .
542
+ Cause: StandardError - I failed!.
541
543
  test-prompt [4] > exit.
542
- shell exited with code 0
544
+ shell exited with code 0.
543
545
  EOS
544
546
  end
545
547
  end
@@ -564,20 +566,20 @@ shell exited with code 0
564
566
  end
565
567
  end
566
568
 
567
- with_input(['unknown', '--opt-1 val', 'foo']) do
568
- result = cli.execute_internal %w(my-command shell), output: :buffer
569
- expect(result[:stdout]).to eq <<-EOS
570
- Starting shell for command "my-command"
569
+ with_input_output(['unknown', '--opt-1 val', 'foo']) do |output|
570
+ cli.execute_internal %w(my-command shell), output: :file
571
+ expect(output.call).to eq <<-EOS
572
+ Starting shell for command "my-command".
571
573
  green prompt > unknown.
572
- unknown action "unknown"
574
+ unknown action "unknown".
573
575
  green prompt > --opt-1 val.
574
- unknown action "--opt-1"
576
+ unknown action "--opt-1".
575
577
  green prompt > foo.
576
- command 'my-command foo' failed
577
-
578
- Cause: StandardError - I failed!
578
+ command 'my-command foo' failed.
579
+ .
580
+ Cause: StandardError - I failed!.
579
581
  green prompt > exit.
580
- shell exited with code 0
582
+ shell exited with code 0.
581
583
  EOS
582
584
  end
583
585
  end
@@ -600,16 +602,16 @@ shell exited with code 0
600
602
  end
601
603
  end
602
604
 
603
- with_input(['', '', 'foo']) do
604
- result = cli.execute_internal %w(my-command shell), output: :buffer
605
- expect(result[:stdout]).to eq <<-EOS
606
- Welcome to my-command!!!
605
+ with_input_output(['', '', 'foo']) do |output|
606
+ cli.execute_internal %w(my-command shell), output: :file
607
+ expect(output.call).to eq <<-EOS
608
+ Welcome to my-command!!!.
607
609
  [my-command][1] $ .
608
610
  [my-command][2] $ .
609
611
  [my-command][3] $ foo.
610
- foo executed
612
+ foo executed.
611
613
  [my-command][4] $ exit.
612
- shell exited with code 0
614
+ shell exited with code 0.
613
615
  EOS
614
616
  end
615
617
  end
@@ -631,17 +633,17 @@ shell exited with code 0
631
633
  end
632
634
  end
633
635
 
634
- with_input(['', '', 'foo', '']) do
635
- result = cli.execute_internal %w(my-command shell), output: :buffer
636
- expect(result[:stdout]).to eq <<-EOS
637
- welcome to my-command
636
+ with_input_output(['', '', 'foo', '']) do |output|
637
+ cli.execute_internal %w(my-command shell), output: :file
638
+ expect(output.call).to eq <<-EOS
639
+ welcome to my-command.
638
640
  [my-command][1] $ .
639
641
  [my-command][2] $ .
640
642
  [my-command][3] $ foo.
641
- foo executed
643
+ foo executed.
642
644
  [my-command][4] $ .
643
645
  [my-command][5] $ exit.
644
- shell exited with code 0
646
+ shell exited with code 0.
645
647
  EOS
646
648
  end
647
649
  end
@@ -661,40 +663,43 @@ shell exited with code 0
661
663
  end
662
664
  end
663
665
 
664
- with_input(['', '', 'foo', '']) do
665
- result = cli.execute_internal %w(my-command shell), output: :buffer
666
- expect(result[:stdout]).to eq <<-EOS
667
- welcome to the command my-command
666
+ with_input_output(['', '', 'foo', '']) do |output|
667
+ cli.execute_internal %w(my-command shell), output: :file
668
+ expect(output.call).to eq <<-EOS
669
+ welcome to the command my-command.
668
670
  [my-command][1] $ .
669
671
  [my-command][2] $ .
670
672
  [my-command][3] $ foo.
671
- foo executed
673
+ foo executed.
672
674
  [my-command][4] $ .
673
675
  [my-command][5] $ exit.
674
- shell exited with code 0
676
+ shell exited with code 0.
675
677
  EOS
676
678
  end
677
679
  end
678
680
  end
679
681
  end
680
682
 
681
- def with_input(lines, &block)
683
+ def with_input_output(lines, &block)
682
684
  old_stdin = $stdin
683
- $stdin = TestIO.new(lines)
684
- block.call
685
- ensure
686
- $stdin = old_stdin
687
- end
688
-
689
- # A test class for wrapping stdin
690
- class TestIO
691
- def initialize(lines)
692
- @lines = lines.reverse
685
+ old_stdout = $stdout
686
+ input_file = Tempfile.new('cliqr').tap do |file|
687
+ lines.push('exit').each { |line| file.write("#{line}\n") }
693
688
  end
694
-
695
- def gets
696
- input = "#{@lines.length > 0 ? @lines.pop : 'exit'}"
697
- puts "#{input}."
698
- "#{input}\n"
689
+ output_file = Tempfile.new('cliqr')
690
+ begin
691
+ $stdin = input_file.open
692
+ $stdout = output_file.open
693
+ output_getter = proc do
694
+ IO.read(output_file.path).gsub(/(.*)\n/, "\\1.\n")
695
+ end
696
+ block.call(output_getter)
697
+ ensure
698
+ $stdin = old_stdin
699
+ $stdout = old_stdout
700
+ input_file.close
701
+ output_file.close
702
+ input_file.unlink
703
+ output_file.unlink
699
704
  end
700
705
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anshul Verma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-10 00:00:00.000000000 Z
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log4r
@@ -68,6 +68,8 @@ files:
68
68
  - lib/cliqr/command/base_command.rb
69
69
  - lib/cliqr/command/color.rb
70
70
  - lib/cliqr/command/command_context.rb
71
+ - lib/cliqr/command/output/file_output_stream.rb
72
+ - lib/cliqr/command/output/standard_output_stream.rb
71
73
  - lib/cliqr/command/shell_banner_builder.rb
72
74
  - lib/cliqr/command/shell_command.rb
73
75
  - lib/cliqr/command/shell_prompt_builder.rb