cli-console 0.1.3 → 0.1.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
  SHA1:
3
- metadata.gz: 15e1e212d7c3e98b863ebdce0699830ac93fb3ce
4
- data.tar.gz: c1e19fbeadd0e6ddbb17bf199973c767807926a9
3
+ metadata.gz: ff85b32926069f72276b38416a049e475ed749f8
4
+ data.tar.gz: ad1350a5d8fd0dcd079bae9b3c3953c083d797b3
5
5
  SHA512:
6
- metadata.gz: dd9384d1a876fc9f3a2956d15ef820282db199fff064b0324b901634d3a75cb3c4f284e27d4056277b678be1ab65dc48952593b8adeac952b5f6d151f9083c28
7
- data.tar.gz: 0c791d344d834dc20f65b4bac27cbe77c79fb7c04172c5fafd2b27327c682f99fa6cdacb18f0896bdf4a4578a3fbc630c4cb8a00c3aecd1ffbf8ff35c6ae7b35
6
+ metadata.gz: f5067a9e8d9b11a595d1406e1f8c1e7c131e5c01102e8dfad2330e1402f7f921ecf4ecf1925b6ef036704a0a802e80baf4c82717f8232f04d7bdd53d09c68b88
7
+ data.tar.gz: 6e35101ccded3b310cdb9525deb6cc6b1277c52d7018161b7554b87d640cea377823ba280877dbc29f446d651d06f9132de965ec95f88e4c4e92d2c6ecc4e093
@@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
22
22
  gem.add_development_dependency 'simplecov'
23
23
  gem.add_development_dependency 'yard'
24
24
  gem.add_development_dependency 'redcarpet'
25
+ gem.add_development_dependency 'highline', '>= 1.6.16'
25
26
  end
@@ -55,7 +55,7 @@ module CLI
55
55
  # @param commandLongDescription [String] long description about command
56
56
  # @param commandUsage [String] usage information about command
57
57
  # @return [Array] command information
58
- def addCommand(commandName, command, commandDescription='', commandLongDescription = nil, commandUsage = nil)
58
+ def addCommand(commandName, command, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
59
59
  commandLongDescription = command.owner.get_description(command.name) if commandLongDescription == nil
60
60
  commandUsage = command.owner.get_usage(command.name) if commandUsage == nil
61
61
  @Commands[commandName] = [command, commandDescription, commandLongDescription, commandUsage]
@@ -67,7 +67,7 @@ module CLI
67
67
  # @param commandLongDescription [String] long description about command
68
68
  # @param commandUsage [String] usage information about command
69
69
  # @return [Array] command information
70
- def addExitCommand(commandName, commandDescription='', commandLongDescription = nil, commandUsage = nil)
70
+ def addExitCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
71
71
  commandLongDescription = 'Exit...' unless commandLongDescription
72
72
  commandUsage = ['exit'] unless commandUsage
73
73
  @Commands[commandName] = [method(:end), commandDescription, commandLongDescription, commandUsage]
@@ -79,7 +79,7 @@ module CLI
79
79
  # @param commandLongDescription [String] long description about command
80
80
  # @param commandUsage [String] usage information about command
81
81
  # @return [Array] command information
82
- def addHelpCommand(commandName, commandDescription='', commandLongDescription = nil, commandUsage = nil)
82
+ def addHelpCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
83
83
  commandLongDescription = 'Display Help for <command>' unless commandLongDescription
84
84
  commandUsage = ['help <command>'] unless commandUsage
85
85
  @Commands[commandName] = [method(:help), commandDescription, commandLongDescription, commandUsage]
@@ -92,7 +92,7 @@ module CLI
92
92
  # @param commandLongDescription [String] long description about alias
93
93
  # @param commandUsage [String] usage information about alias
94
94
  # @return [Array] alias information
95
- def addAlias(aliasName, commandNameParms, commandDescription=nil, commandLongDescription = nil, commandUsage = nil)
95
+ def addAlias(aliasName, commandNameParms, commandDescription = nil, commandLongDescription = nil, commandUsage = nil)
96
96
  @Aliases[aliasName] = [commandNameParms, commandDescription, commandLongDescription, commandUsage]
97
97
  end
98
98
 
@@ -104,7 +104,7 @@ module CLI
104
104
  h.whitespace = :strip_and_collapse
105
105
  h.validate = nil
106
106
  h.readline = @UseReadline
107
- h.completion = @Commands.keys+@Aliases.keys
107
+ h.completion = @Commands.keys + @Aliases.keys
108
108
  end
109
109
  @LastCommand = '' if (not @LastCommand.to_s.empty? and @LastCommand[0] == '#')
110
110
  return @LastCommand
@@ -128,23 +128,39 @@ module CLI
128
128
  matches.each do |value|
129
129
  text = value[0].to_s
130
130
  text += ' - '+value[1][1].to_s unless value[1][1].to_s.empty?
131
- @IO.indent(1, text )
131
+ @IO.indent(1, text)
132
132
  end
133
133
  end
134
134
 
135
+ # Parses commandString into Array of words
136
+ # @param commandString [String]
137
+ # @return [Array] command words
138
+ def self.parseCommandString(commandString)
139
+ commandWords = []
140
+ return commandWords unless commandString
141
+
142
+ pattern = %r{
143
+ (?<command> [^\s"']+ ){0}
144
+ (?<exceptDoubleQuotes> [^"\\]*(?:\\.[^"\\]*)* ){0}
145
+ (?<exceptSingleQuotes> [^'\\]*(?:\\.[^'\\]*)* ){0}
146
+ \g<command>|"\g<exceptDoubleQuotes>"|'\g<exceptSingleQuotes>'
147
+ }x
148
+
149
+ commandString.scan(pattern) do |m|
150
+ commandWords << m[0] unless m[0].nil?
151
+ commandWords << m[1].gsub('\"','"') unless m[1].nil?
152
+ commandWords << m[2].gsub('\\\'','\'') unless m[2].nil?
153
+ end
154
+ commandWords
155
+ end
156
+
135
157
  # Executes command with parameters
136
158
  # @param commandString [String]
137
159
  # @return command result or error number
138
160
  def processCommand(commandString)
139
161
  return 0 if commandString.to_s.empty?
140
- commandWords = []
141
- commandString.scan(/([^\s"']+)|"([^"]*)"|'([^']*)'/) do |m|
142
- commandWords << m[0] unless m[0].nil?
143
- commandWords << m[1] unless m[1].nil?
144
- commandWords << m[2] unless m[2].nil?
145
- end
162
+ commandWords = Console::parseCommandString(commandString)
146
163
  command = commandWords.shift.downcase
147
-
148
164
  if (not @Commands.key?(command) and not @Aliases.key?(command))
149
165
  matches = commandMatch(command)
150
166
  if matches.length == 1
@@ -195,13 +211,13 @@ module CLI
195
211
  end
196
212
  end
197
213
  command = getCommand( formatString % currentValues )
198
- @IO.indent_level+=1
214
+ @IO.indent_level += 1
199
215
  result = processCommand(command)
200
- @IO.indent_level-=1
216
+ @IO.indent_level -= 1
201
217
  return 0 if result == @ExitCode
202
218
  rescue Exception => e
203
219
  showException(e)
204
- @IO.indent_level=indent_level
220
+ @IO.indent_level = indent_level
205
221
  end
206
222
  end
207
223
  -1
@@ -209,7 +225,7 @@ module CLI
209
225
 
210
226
  # Displays seperator line
211
227
  def printSeperator
212
- @IO.say(@Seperator*@SepCount)
228
+ @IO.say(@Seperator * @SepCount)
213
229
  end
214
230
 
215
231
  # Displays help about command
@@ -219,7 +235,7 @@ module CLI
219
235
  printSeperator
220
236
  if not @Commands[command][3].to_a.empty?
221
237
  @IO.indent do |io|
222
- @Commands[command][3].each { |c| io.say c}
238
+ @Commands[command][3].each { |c| io.say c }
223
239
  io.newline
224
240
  end
225
241
  end
@@ -235,13 +251,13 @@ module CLI
235
251
 
236
252
  # Shows message to inform how to get more information about command
237
253
  # @param helpCommand [String] command for which display message
238
- def command_usage(helpCommand='help')
254
+ def command_usage(helpCommand = 'help')
239
255
  @IO.say("You can type \"#{helpCommand} <command>\" for more info")
240
256
  end
241
257
 
242
258
  # Shows help information about command
243
259
  # @param params [Array] command for which show help
244
- def help(params=[])
260
+ def help(params = [])
245
261
  if not params.to_a.empty?
246
262
  command = params[0].downcase
247
263
  if (not @Commands.key?(command) and not @Aliases.key?(command))
@@ -295,7 +311,7 @@ module CLI
295
311
 
296
312
  # @param params [Array] unused
297
313
  # @return [Fixnum]
298
- def end(params=[])
314
+ def end(params = [])
299
315
  @ExitCode
300
316
  end
301
317
  end
@@ -1,6 +1,6 @@
1
1
  module CLI
2
2
  class Console
3
3
  # Version for gem
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
6
6
  end
@@ -30,6 +30,39 @@ describe CLI::Console do
30
30
  console.addAlias('quit','exit')
31
31
  end
32
32
 
33
+ describe '.parseCommandString' do
34
+ it 'should parse correctly simple command' do
35
+ CLI::Console::parseCommandString('command param1 param2').should eq(['command', 'param1', 'param2'])
36
+ end
37
+
38
+ it 'should parse command with double quotes' do
39
+ CLI::Console::parseCommandString('command "param1 extra" param2').should eq(['command', 'param1 extra', 'param2'])
40
+ CLI::Console::parseCommandString('command "param1" "param2 extra"').should eq(['command', 'param1', 'param2 extra'])
41
+ end
42
+
43
+ it 'should parse command with single quotes' do
44
+ CLI::Console::parseCommandString('command \'param1 extra\' param2').should eq(['command', 'param1 extra', 'param2'])
45
+ CLI::Console::parseCommandString('command \'param1\' \'param2 extra\'').should eq(['command', 'param1', 'param2 extra'])
46
+ end
47
+
48
+ it 'should parse command with mixed quotes' do
49
+ CLI::Console::parseCommandString('command "param1 \'extra double\'" param2').should eq(['command', 'param1 \'extra double\'', 'param2'])
50
+ CLI::Console::parseCommandString('command "param1" "param2 \'double extra\'"').should eq(['command', 'param1', 'param2 \'double extra\''])
51
+ CLI::Console::parseCommandString('command \'param1 "extra double"\' param2').should eq(['command', 'param1 "extra double"', 'param2'])
52
+ CLI::Console::parseCommandString('command \'param1\' \'param2 "double extra"\'').should eq(['command', 'param1', 'param2 "double extra"'])
53
+ end
54
+
55
+ it 'should parse command with escaped quotes' do
56
+ CLI::Console::parseCommandString('command "param with \" quote"').should eq(['command', 'param with " quote'])
57
+ CLI::Console::parseCommandString('command \'param with \\\' quote\'').should eq(['command', 'param with \' quote'])
58
+ end
59
+
60
+ it 'should parse command with escaped quotes in quotes' do
61
+ CLI::Console::parseCommandString('command "param1 \'extra \"double double\"\'" param2').should eq(['command', 'param1 \'extra "double double"\'', 'param2'])
62
+ end
63
+
64
+ end
65
+
33
66
  describe '#initialize' do
34
67
  it 'should return cli-console instance' do
35
68
  console.should be_kind_of(CLI::Console)
@@ -72,17 +105,40 @@ describe CLI::Console do
72
105
  end
73
106
 
74
107
  describe '#processCommand' do
75
- it 'should execute succesfully' do
108
+ before do
76
109
  init
77
- console.processCommand('tr')
78
- console.processCommand('try more')
79
- console.processCommand('q')
80
- console.processCommand('quit')
81
- console.processCommand('help')
82
- console.processCommand('help tr')
83
- console.processCommand('help try')
84
- console.processCommand('help not')
85
- console.processCommand('help e')
110
+ end
111
+
112
+ it 'should return zero' do
113
+ console.processCommand('').should eq(0)
114
+ end
115
+
116
+ it 'should be ambiguous command' do
117
+ console.processCommand('tr').should eq(-3)
118
+ end
119
+
120
+ it 'should throw exception' do
121
+ console.processCommand('try more').should eq(-1)
122
+ end
123
+
124
+ it 'should quit' do
125
+ console.processCommand('q').should eq(console.ExitCode)
126
+ console.processCommand('quit').should eq(console.ExitCode)
127
+ end
128
+
129
+ it 'should show help' do
130
+ console.processCommand('help').should be_nil
131
+ console.processCommand('help try').should be_nil
132
+ console.processCommand('help e').should be_nil
133
+ end
134
+
135
+ it 'should show help for method and alias' do
136
+ console.processCommand('help tr').should be_kind_of(Array)
137
+ end
138
+
139
+ it 'should execute succesfully' do
140
+ console.processCommand('help not').should be_nil
141
+
86
142
  end
87
143
  end
88
144
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dāvis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-15 00:00:00.000000000 Z
11
+ date: 2013-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: highline
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.16
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.16
69
83
  description: Basic library for making interactive command-line applications much easier
70
84
  email:
71
85
  - davispuh@gmail.com
@@ -113,3 +127,4 @@ summary: Create CLI applications easily
113
127
  test_files:
114
128
  - spec/cli-console_spec.rb
115
129
  - spec/spec_helper.rb
130
+ has_rdoc: