Class: CLI::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/cli-console.rb,
lib/cli-console/version.rb

Overview

Console class

  # io some input/output class (like HighLine)
  console = CLI::Console.new(io)

Constant Summary collapse

VERSION =

Version for gem

'0.1.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Console

Returns a new instance of Console.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cli-console.rb', line 14

def initialize(io)
    @Commands = {}
    @Aliases = {}
    @IO = io
    @Seperator = '-'
    @SepCount = 30
    @Backtrace = false
    @Partial = true
    @ExitCode = -100
    @UseReadline = true
end

Instance Attribute Details

#BacktraceObject

Returns the value of attribute Backtrace.



48
49
50
# File 'lib/cli-console.rb', line 48

def Backtrace
  @Backtrace
end

#ExitCodeObject

Returns the value of attribute ExitCode.



45
46
47
# File 'lib/cli-console.rb', line 45

def ExitCode
  @ExitCode
end

#PartialObject

Returns the value of attribute Partial.



44
45
46
# File 'lib/cli-console.rb', line 44

def Partial
  @Partial
end

#SepCountObject

Returns the value of attribute SepCount.



47
48
49
# File 'lib/cli-console.rb', line 47

def SepCount
  @SepCount
end

#SeperatorObject

Returns the value of attribute Seperator.



46
47
48
# File 'lib/cli-console.rb', line 46

def Seperator
  @Seperator
end

#UseReadlineObject

Returns the value of attribute UseReadline.



49
50
51
# File 'lib/cli-console.rb', line 49

def UseReadline
  @UseReadline
end

Class Method Details

.parseCommandString(commandString) ⇒ Array

Parses commandString into Array of words

Parameters:

  • commandString (String)

Returns:

  • (Array)

    command words



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cli-console.rb', line 138

def self.parseCommandString(commandString)
    commandWords = []
    return commandWords unless commandString

    pattern = %r{
        (?<command> [^\s"']+ ){0}
        (?<exceptDoubleQuotes> [^"\\]*(?:\\.[^"\\]*)* ){0}
        (?<exceptSingleQuotes> [^'\\]*(?:\\.[^'\\]*)* ){0}
        \g<command>|"\g<exceptDoubleQuotes>"|'\g<exceptSingleQuotes>'
    }x

    commandString.scan(pattern) do |m|
        commandWords << m[0] unless m[0].nil?
        commandWords << m[1].gsub('\"','"') unless m[1].nil?
        commandWords << m[2].gsub('\\\'','\'') unless m[2].nil?
    end
    commandWords
end

Instance Method Details

#addAlias(aliasName, commandNameParms, commandDescription = nil, commandLongDescription = nil, commandUsage = nil) ⇒ Array

Adds alias for command

Parameters:

  • aliasName (String)

    name of for alias

  • commandNameParms (String)

    command to alias

  • commandDescription (String) (defaults to: nil)

    short description about alias

  • commandLongDescription (String) (defaults to: nil)

    long description about alias

  • commandUsage (String) (defaults to: nil)

    usage information about alias

Returns:

  • (Array)

    alias information



95
96
97
# File 'lib/cli-console.rb', line 95

def addAlias(aliasName, commandNameParms, commandDescription = nil, commandLongDescription = nil, commandUsage = nil)
    @Aliases[aliasName] = [commandNameParms, commandDescription, commandLongDescription, commandUsage]
end

#addCommand(commandName, command, commandDescription = '', commandLongDescription = nil, commandUsage = nil) ⇒ Array

Adds command

Parameters:

  • commandName (String)

    name of command

  • command (Method)

    command method

  • commandDescription (String) (defaults to: '')

    short description about command

  • commandLongDescription (String) (defaults to: nil)

    long description about command

  • commandUsage (String) (defaults to: nil)

    usage information about command

Returns:

  • (Array)

    command information



58
59
60
61
62
# File 'lib/cli-console.rb', line 58

def addCommand(commandName, command, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
    commandLongDescription = command.owner.get_description(command.name) if commandLongDescription == nil
    commandUsage = command.owner.get_usage(command.name) if commandUsage == nil
    @Commands[commandName] = [command, commandDescription, commandLongDescription, commandUsage]
end

#addExitCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil) ⇒ Array

Adds name for exit command

Parameters:

  • commandName (String)

    name of exit command

  • commandDescription (String) (defaults to: '')

    short description about command

  • commandLongDescription (String) (defaults to: nil)

    long description about command

  • commandUsage (String) (defaults to: nil)

    usage information about command

Returns:

  • (Array)

    command information



70
71
72
73
74
# File 'lib/cli-console.rb', line 70

def addExitCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
    commandLongDescription = 'Exit...' unless commandLongDescription
    commandUsage = ['exit'] unless commandUsage
    @Commands[commandName] = [method(:end), commandDescription, commandLongDescription, commandUsage]
end

#addHelpCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil) ⇒ Array

Adds name for help command

Parameters:

  • commandName (String)

    name of exit command

  • commandDescription (String) (defaults to: '')

    short description about command

  • commandLongDescription (String) (defaults to: nil)

    long description about command

  • commandUsage (String) (defaults to: nil)

    usage information about command

Returns:

  • (Array)

    command information



82
83
84
85
86
# File 'lib/cli-console.rb', line 82

def addHelpCommand(commandName, commandDescription = '', commandLongDescription = nil, commandUsage = nil)
    commandLongDescription = 'Display Help for <command>' unless commandLongDescription
    commandUsage = ['help <command>'] unless commandUsage
    @Commands[commandName] = [method(:help), commandDescription, commandLongDescription, commandUsage]
end

#command_usage(helpCommand = 'help') ⇒ Object

Shows message to inform how to get more information about command

Parameters:

  • helpCommand (String) (defaults to: 'help')

    command for which display message



254
255
256
# File 'lib/cli-console.rb', line 254

def command_usage(helpCommand = 'help')
    @IO.say("You can type \"#{helpCommand} <command>\" for more info")
end

#commandHelp(command) ⇒ Object

Displays help about command

Parameters:

  • command (String)

    command for which display help



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cli-console.rb', line 233

def commandHelp(command)
    if @Commands.key?(command)
        printSeperator
        if not @Commands[command][3].to_a.empty?
            @IO.indent do |io|
                @Commands[command][3].each { |c| io.say c }
                io.newline
            end
        end
        @IO.say(@Commands[command][2]) unless @Commands[command][2].to_s.empty?
        printSeperator
    end
end

#commandMatch(command) ⇒ Array

Parameters:

  • command (String)

Returns:

  • (Array)


115
116
117
118
119
120
121
122
# File 'lib/cli-console.rb', line 115

def commandMatch(command)
    matches = []
    commandStrings = @Commands.merge(@Aliases)
    commandStrings.each do |key, value|
        matches.push([key, value]) if (@Partial and key.start_with?(command)) or key == command
    end
    matches
end

#end(params = []) ⇒ Fixnum

Parameters:

  • params (Array) (defaults to: [])

    unused

Returns:

  • (Fixnum)


314
315
316
# File 'lib/cli-console.rb', line 314

def end(params = [])
    @ExitCode
end

#getCommand(inputText) ⇒ String

Waits till user enters command

Parameters:

  • inputText (String)

    text to show to user

Returns:

  • (String)

    entered command



102
103
104
105
106
107
108
109
110
111
# File 'lib/cli-console.rb', line 102

def getCommand(inputText)
    @LastCommand = @IO.ask(inputText) do |h|
        h.whitespace = :strip_and_collapse
        h.validate = nil
        h.readline = @UseReadline
        h.completion = @Commands.keys + @Aliases.keys
    end
    @LastCommand = '' if (not @LastCommand.to_s.empty? and @LastCommand[0] == '#')
    return @LastCommand
end

#help(params = []) ⇒ Object

Shows help information about command

Parameters:

  • params (Array) (defaults to: [])

    command for which show help



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/cli-console.rb', line 260

def help(params = [])
    if not params.to_a.empty?
        command = params[0].downcase
        if (not @Commands.key?(command) and not @Aliases.key?(command))
            matches = commandMatch command
            if (matches.length == 1)
                commandHelp(matches[0][0])
            elsif matches.length > 0
                @IO.say('Help for ambiguous command.')
                showMatches matches
            else
                @IO.say("Help for command \"#{params[0]}\" not found!")
            end
        elsif @Aliases.key?(command)
            if not @Aliases[command][2].to_s.empty?
            @IO.say(@Aliases[command][2])
            else
                commandHelp(@Aliases[command][0].split()[0])
            end
        else
            commandHelp(command)
        end
    else
        helpCommand = 'help'
        printSeperator
        @IO.indent do |io|
            @Commands.each do |key,value|
                if (value[0].name != 'help')
                    helpText = key
                    if (not value[1].to_s.empty?)
                        helpText += ' - '+value[1]
                    end
                io.say(helpText)
                else
                helpCommand = value[0].name
                end
            end
            @Aliases.each do |key, value|
                helpText = key
                if (not value[1].to_s.empty?)
                    helpText += ' - '+value[1]
                else
                    helpText += ' - alias to "'+value[0]+'"'
                end
                io.say(helpText)
            end
        end
        command_usage(helpCommand)
        printSeperator
    end
end

#printSeperatorObject

Displays seperator line



227
228
229
# File 'lib/cli-console.rb', line 227

def printSeperator
    @IO.say(@Seperator * @SepCount)
end

#processCommand(commandString) ⇒ Object

Executes command with parameters

Parameters:

  • commandString (String)

Returns:

  • command result or error number



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cli-console.rb', line 160

def processCommand(commandString)
    return 0 if commandString.to_s.empty?
    commandWords = Console::parseCommandString(commandString)
    command = commandWords.shift.downcase
    if (not @Commands.key?(command) and not @Aliases.key?(command))
        matches = commandMatch(command)
        if matches.length == 1
            if matches[0][1][0].class == String
                oldCommandWords = [matches[0][1][0]]
            commandWords = @Aliases[matches[0][0]][0].split
            command = commandWords.shift.downcase
            commandWords += oldCommandWords
            else
            command = matches[0][0]
            end
        elsif matches.length > 0
            @IO.say('Ambiguous command.')
            showMatches(matches)
        return -3
        else
            @IO.say("Command \"#{command}\" not recognized.")
        return -2
        end
    elsif @Aliases.key?(command)
    oldCommandWords = commandWords
    commandWords = @Aliases[command][0].split
    command = commandWords.shift.downcase
    commandWords += oldCommandWords
    end
    begin
        return @Commands[command][0].call(commandWords)
    rescue Exception => e
        showException(e)
    end
    return -1
end

#showMatches(matches) ⇒ Object

displays matched commands

Parameters:

  • matches (Array)


126
127
128
129
130
131
132
133
# File 'lib/cli-console.rb', line 126

def showMatches(matches)
    @IO.say('Matches:')
    matches.each do |value|
        text = value[0].to_s
        text += ' - '+value[1][1].to_s unless value[1][1].to_s.empty?
        @IO.indent(1, text)
    end
end

#start(formatString, formatValues) ⇒ Fixnum

Starts main CLI loop, waits for user input

Parameters:

  • formatString (String)
  • formatValues (Array)

Returns:

  • (Fixnum)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cli-console.rb', line 201

def start(formatString, formatValues)
    indent_level = @IO.indent_level
    loop do
        begin
            currentValues = []
            formatValues.each do |value|
                if value.respond_to?("call")
                currentValues.push(value.call)
                else
                currentValues.push(value)
                end
            end
            command = getCommand( formatString % currentValues )
            @IO.indent_level += 1
            result = processCommand(command)
            @IO.indent_level -= 1
            return 0 if result == @ExitCode
        rescue Exception => e
            showException(e)
        @IO.indent_level = indent_level
        end
    end
    -1
end

#usageObject

Displays usage



248
249
250
# File 'lib/cli-console.rb', line 248

def usage
    @IO.say('Type "help" to display available commands')
end