Class: Linen::CLI

Inherits:
Handler show all
Defined in:
lib/linen/handlers/cli.rb

Overview

The CLI handler. This uses Readline and friends to provide a command-line-like interface to the loaded plugins. It is the default handler.

Authors

Copyright © 2007 Laika, Inc.

This code released under the terms of the BSD license.

Version

$Id: cli.rb 431 2008-01-22 23:10:52Z bbleything $

Defined Under Namespace

Classes: AbstractAmbiguityError, AmbiguousCommandError, AmbiguousPluginError, CommandNotFoundError, PluginNotFoundError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.prompt=(value) ⇒ Object

Sets the attribute prompt

Parameters:

  • value

    the value to set the attribute prompt to.



24
25
26
# File 'lib/linen/handlers/cli.rb', line 24

def prompt=(value)
  @prompt = value
end

Class Method Details

.historyObject



107
108
109
# File 'lib/linen/handlers/cli.rb', line 107

def self::history
	return Readline::HISTORY
end

.parse_command(input) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linen/handlers/cli.rb', line 29

def self::parse_command( input )
	### * nil means ctrl-d, so exit.
	### * if they said "quit" or "exit", do so
	### * Size == 0 means empty command, so just return.
	###
	### otherwise, add to history.
	if input.nil?
		### blank line to make ctrl-d not make the error live on the existing line
		puts ; cleanup and exit
	elsif input.chomp.size == 0 # empty string
		return
	else
		Readline::HISTORY.push( input )
	end

	plugin, command, *arguments = input.split

	if ['quit', 'exit'].abbrev.include? plugin
		cleanup and exit
	elsif ['help', '?'].abbrev.include? plugin
		# they entered "help <plugin> <command> or some subset there of, which means
		# that we have the plugin in command and the command in the first element of args.
		plugin  = command.dup     rescue nil
		command = arguments.shift rescue nil

		if plugin and command
			plugin, command = canonicalize( "#{plugin} #{command}" ).split rescue nil

			# if either plugin or command is nil, lookup will fail; bail
			return unless plugin = Linen.plugins[ plugin ]
			return unless command = plugin.commands[ command ]

			puts command.help
		elsif plugin
			return unless plugin = Linen.plugins[ canonicalize( plugin ) ]
			puts plugin.help
		else
			help
		end
	elsif plugin.nil? or command.nil?
		puts "You must enter both a plugin name and a command."
	elsif Linen.plugins[ plugin ].nil?
		puts "Plugin '#{plugin}' not found."
	elsif Linen.plugins[ plugin ].commands[ command ].nil?
		puts "Command '#{command}' not found in '#{plugin}' plugin."
	else
		plugin, command, *args = canonicalize( input ).split

		plugin  = Linen.plugins[ plugin ]
		command = plugin.commands[ command ]

		execute_command plugin, command, args
	end
end

.startObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/linen/handlers/cli.rb', line 85

def self::start
	loop do
		input = nil

		begin
			input = Readline.readline( @prompt )
			parse_command input
		rescue Interrupt
			if input
				puts "\nCommand aborted."
			else
				puts "\nPlease type 'quit' or 'exit' to quit."
			end
		rescue Linen::Plugin::ArgumentError => e
			puts e.message
		end

		puts # blank line to clean things up
	end
end