Class: Linen::Plugin::SimpleCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/linen/simple_command.rb

Overview

Copyright 2007, LAIKA, Inc. #

#

Authors: #

* Ben Bleything <[email protected]>                    #

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name, &block) ⇒ SimpleCommand

Returns a new instance of SimpleCommand.



13
14
15
16
17
18
19
20
# File 'lib/linen/simple_command.rb', line 13

def initialize( plugin, name, &block )
	@plugin    = plugin
	@name      = name
	@arguments = []
	@help_text = "No help for #{plugin.short_name} #{name}"

	self.instance_eval &block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/linen/simple_command.rb', line 11

def name
  @name
end

Instance Method Details

#action(&block) ⇒ Object



85
86
87
# File 'lib/linen/simple_command.rb', line 85

def action( &block )
	@action_proc = block
end

#execute(workspace = Linen::Workspace.new) ⇒ Object



23
24
25
# File 'lib/linen/simple_command.rb', line 23

def execute( workspace = Linen::Workspace.new )
	return workspace.instance_eval( &@action_proc )
end

#helpObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/linen/simple_command.rb', line 28

def help
	output = []

	output << @help_text.wrap
	output << nil # blank line

	# this map turns our list of args into a list like this:
	# <arg1> <arg2> <arg3> <arg4>...
	arg_list = @arguments.map {|a| "<#{a.to_s}>"}.join( ' ' )

	output << "Usage: #{@plugin.short_name} #{name} #{arg_list}"

	return output.join( "\n" )
end

#help_message(message) ⇒ Object



80
81
82
# File 'lib/linen/simple_command.rb', line 80

def help_message( message )
	@help_text = message
end

#one_of(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/linen/simple_command.rb', line 49

def one_of( *args )
	raise Linen::Plugin::ArgumentError,
		"You may not specify both required and one_of arguments" if @argument_type == :required

	@argument_type = :one_of

	args.each do |arg|
		raise Linen::Plugin::ArgumentError,
			"Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg

		@arguments << arg
	end
end

#require_confirmationObject



90
91
92
# File 'lib/linen/simple_command.rb', line 90

def require_confirmation
	@require_confirmation = true
end

#required_arguments(*args) ⇒ Object Also known as: required_argument



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/linen/simple_command.rb', line 64

def required_arguments( *args )
	raise Linen::Plugin::ArgumentError,
		"You may not specify both required and one_of arguments" if @argument_type == :one_of

	@argument_type = :required

	args.each do |arg|
		raise Linen::Plugin::ArgumentError,
			"Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg

		@arguments << arg
	end
end

#requires_confirmation?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/linen/simple_command.rb', line 112

def requires_confirmation?
	@require_confirmation
end

#validate_arguments(args) ⇒ Object

HELPER METHODS #



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/linen/simple_command.rb', line 99

def validate_arguments( args )
	if @argument_type == :one_of
		return validate_one_of_arguments( args.first )
	else # @argument_type == :required
		return validate_required_arguments( args )
	end

	# this is a Can't Happen(tm) so I'm comfortable with the crappy
	# exception string.  :P
	raise "Something has happened!"
end