linen 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,171 +8,171 @@
8
8
  ##############################################################
9
9
 
10
10
  class Linen::Plugin::Command
11
- attr_reader :name
11
+ attr_reader :name
12
12
 
13
- def initialize( plugin, name, &block )
14
- @plugin = plugin
15
- @name = name
16
- @arguments = []
17
- @help_text = "No help for #{plugin.short_name} #{name}"
13
+ def initialize( plugin, name, &block )
14
+ @plugin = plugin
15
+ @name = name
16
+ @arguments = []
17
+ @help_text = "No help for #{plugin.short_name} #{name}"
18
18
 
19
- self.instance_eval &block
20
- end
19
+ self.instance_eval &block
20
+ end
21
21
 
22
22
 
23
- def execute( workspace = Linen::Workspace.new )
24
- return workspace.instance_eval( &@action_proc )
25
- end
23
+ def execute( workspace = Linen::Workspace.new )
24
+ return workspace.instance_eval( &@action_proc )
25
+ end
26
26
 
27
27
 
28
- def help
29
- output = []
28
+ def help
29
+ output = []
30
30
 
31
- output << @help_text.wrap
32
- output << nil # blank line
31
+ output << @help_text.wrap
32
+ output << nil # blank line
33
33
 
34
- # this map turns our list of args into a list like this:
35
- # <arg1> <arg2> <arg3> <arg4>...
36
- arg_list = arguments.map {|a| "<#{a.to_s}>"}.join( ' ' )
34
+ # this map turns our list of args into a list like this:
35
+ # <arg1> <arg2> <arg3> <arg4>...
36
+ arg_list = arguments.map {|a| "<#{a.to_s}>"}.join( ' ' )
37
37
 
38
- output << "Usage: #{@plugin.short_name} #{name} #{arg_list}"
38
+ output << "Usage: #{@plugin.short_name} #{name} #{arg_list}"
39
39
 
40
- return output.join( "\n" )
41
- end
40
+ return output.join( "\n" )
41
+ end
42
42
 
43
43
 
44
- #############################
45
- # PLUGIN DEFINITION METHODS #
46
- #############################
44
+ #############################
45
+ # PLUGIN DEFINITION METHODS #
46
+ #############################
47
47
 
48
48
 
49
- def one_of( *args )
50
- raise Linen::Plugin::ArgumentError,
51
- "You may not specify both required and one_of arguments" if @argument_type == :required
49
+ def one_of( *args )
50
+ raise Linen::Plugin::ArgumentError,
51
+ "You may not specify both required and one_of arguments" if @argument_type == :required
52
52
 
53
- @argument_type = :one_of
53
+ @argument_type = :one_of
54
54
 
55
- args.each do |arg|
56
- raise Linen::Plugin::ArgumentError,
57
- "Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg
55
+ args.each do |arg|
56
+ raise Linen::Plugin::ArgumentError,
57
+ "Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg
58
58
 
59
- @arguments << arg
60
- end
61
- end
59
+ @arguments << arg
60
+ end
61
+ end
62
62
 
63
63
 
64
- def required_arguments( *args )
65
- raise Linen::Plugin::ArgumentError,
66
- "You may not specify both required and one_of arguments" if @argument_type == :one_of
64
+ def required_arguments( *args )
65
+ raise Linen::Plugin::ArgumentError,
66
+ "You may not specify both required and one_of arguments" if @argument_type == :one_of
67
67
 
68
- @argument_type = :required
68
+ @argument_type = :required
69
69
 
70
- args.each do |arg|
71
- raise Linen::Plugin::ArgumentError,
72
- "Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg
70
+ args.each do |arg|
71
+ raise Linen::Plugin::ArgumentError,
72
+ "Argument '#{arg}' has not been defined" unless @plugin.arguments.include? arg
73
73
 
74
- @arguments << arg
75
- end
76
- end
77
- alias required_argument required_arguments
74
+ @arguments << arg
75
+ end
76
+ end
77
+ alias required_argument required_arguments
78
78
 
79
79
 
80
- def help_message( message )
81
- @help_text = message
82
- end
80
+ def help_message( message )
81
+ @help_text = message
82
+ end
83
83
 
84
84
 
85
- def action( &block )
86
- @action_proc = block
87
- end
88
-
89
-
90
- def require_confirmation
91
- @require_confirmation = true
92
- end
85
+ def action( &block )
86
+ @action_proc = block
87
+ end
93
88
 
94
89
 
95
- ##################
96
- # HELPER METHODS #
97
- ##################
90
+ def require_confirmation
91
+ @require_confirmation = true
92
+ end
98
93
 
99
- def validate_arguments( args )
100
- if @argument_type == :one_of
101
- return validate_one_of_arguments( args.first )
102
- else # @argument_type == :required
103
- return validate_required_arguments( args )
104
- end
105
94
 
106
- # this is a Can't Happen(tm) so I'm comfortable with the crappy
107
- # exception string. :P
108
- raise "Something has happened!"
109
- end
110
-
111
-
112
- def requires_confirmation?
113
- @require_confirmation
114
- end
95
+ ##################
96
+ # HELPER METHODS #
97
+ ##################
115
98
 
116
- ###########
117
- private #
118
- ###########
99
+ def validate_arguments( args )
100
+ if @argument_type == :one_of
101
+ return validate_one_of_arguments( args.first )
102
+ else # @argument_type == :required
103
+ return validate_required_arguments( args )
104
+ end
119
105
 
120
- ### FIXME:bbleything
121
- ###
122
- ### The reprompting stuff below is going to come back to bite us when we try to
123
- ### add batch mode later... that said, in the interest of getting things out the
124
- ### door, we're going to leave it be for now.
106
+ # this is a Can't Happen(tm) so I'm comfortable with the crappy
107
+ # exception string. :P
108
+ raise "Something has happened!"
109
+ end
125
110
 
126
- def validate_one_of_arguments( arg )
127
- results = IndifferentHash.new
128
111
 
129
- begin
130
- @arguments.each do |arg_name|
131
- argument = @plugin.arguments[ arg_name ]
112
+ def requires_confirmation?
113
+ @require_confirmation
114
+ end
132
115
 
133
- results[ arg_name ] = argument.validate( arg ) rescue nil
134
- end
116
+ ###########
117
+ private #
118
+ ###########
135
119
 
136
- raise Linen::Plugin::ArgumentError,
137
- "The value you entered ('#{arg}') is invalid for all arguments." if
138
- results.values.compact.empty?
120
+ ### FIXME:bbleything
121
+ ###
122
+ ### The reprompting stuff below is going to come back to bite us when we try to
123
+ ### add batch mode later... that said, in the interest of getting things out the
124
+ ### door, we're going to leave it be for now.
139
125
 
140
- rescue Linen::Plugin::ArgumentError => e
141
- print "#{e} "
126
+ def validate_one_of_arguments( arg )
127
+ results = IndifferentHash.new
142
128
 
143
- arg = Linen::CLI.reprompt.split.first
129
+ begin
130
+ @arguments.each do |arg_name|
131
+ argument = @plugin.arguments[ arg_name ]
144
132
 
145
- retry
146
- end
133
+ results[ arg_name ] = argument.validate( arg ) rescue nil
134
+ end
147
135
 
148
- return results
149
- end
136
+ raise Linen::Plugin::ArgumentError,
137
+ "The value you entered ('#{arg}') is invalid for all arguments." if
138
+ results.values.compact.empty?
150
139
 
140
+ rescue Linen::Plugin::ArgumentError => e
141
+ print "#{e} "
151
142
 
152
- def validate_required_arguments( args )
153
- results = IndifferentHash.new
143
+ arg = Linen::CLI.reprompt.split.first
154
144
 
155
- @arguments.each do |arg_name|
156
- argument = @plugin.arguments[ arg_name ]
157
- arg_value = args.shift
145
+ retry
146
+ end
158
147
 
159
- begin
160
- arg_value = Linen::CLI.reprompt( argument.prompt ) if arg_value.nil?
148
+ return results
149
+ end
161
150
 
162
- argument.validate arg_value
163
151
 
164
- rescue Linen::Plugin::ArgumentError => e
165
- print "#{e} "
152
+ def validate_required_arguments( args )
153
+ results = IndifferentHash.new
166
154
 
167
- # reset arg_value to nil so we get prompted on retry
168
- arg_value = nil
155
+ @arguments.each do |arg_name|
156
+ argument = @plugin.arguments[ arg_name ]
157
+ arg_value = args.shift
169
158
 
170
- retry
171
- else
172
- results[ arg_name ] = argument.convert( arg_value )
173
- end
174
- end
159
+ begin
160
+ arg_value = Linen::CLI.reprompt( argument.prompt ) if arg_value.nil?
175
161
 
176
- return results
177
- end
162
+ argument.validate arg_value
163
+
164
+ rescue Linen::Plugin::ArgumentError => e
165
+ print "#{e} "
166
+
167
+ # reset arg_value to nil so we get prompted on retry
168
+ arg_value = nil
169
+
170
+ retry
171
+ else
172
+ results[ arg_name ] = argument.convert( arg_value )
173
+ end
174
+ end
175
+
176
+ return results
177
+ end
178
178
  end
@@ -14,26 +14,26 @@ class Linen::CLI::PluginNotFoundError < NameError ; end
14
14
  class Linen::CLI::CommandNotFoundError < NameError ; end
15
15
 
16
16
  class Linen::CLI::AbstractAmbiguityError < NameError
17
- attr_accessor :candidates, :input
17
+ attr_accessor :candidates, :input
18
18
 
19
- def initialize( candidates = [], input = '' )
20
- @candidates = candidates
21
- @input = input
22
- end
19
+ def initialize( candidates = [], input = '' )
20
+ @candidates = candidates
21
+ @input = input
22
+ end
23
23
 
24
- def to_s
25
- type = self.class.to_s.match( /Ambiguous(.*?)Error/ )[1].downcase
24
+ def to_s
25
+ type = self.class.to_s.match( /Ambiguous(.*?)Error/ )[1].downcase
26
26
 
27
- return "The #{type} you entered ('#{@input}') was ambiguous; please select from the following: #{@candidates.join ', '}"
28
- end
27
+ return "The #{type} you entered ('#{@input}') was ambiguous; please select from the following: #{@candidates.join ', '}"
28
+ end
29
29
  end
30
30
 
31
31
  class Linen::CLI::AmbiguousPluginError < Linen::CLI::AbstractAmbiguityError ; end
32
32
 
33
33
  class Linen::CLI::AmbiguousCommandError < Linen::CLI::AbstractAmbiguityError
34
- def to_s
35
- @candidates.map! {|c| [plugin, c].join ' '}
34
+ def to_s
35
+ @candidates.map! {|c| [plugin, c].join ' '}
36
36
 
37
- super
38
- end
37
+ super
38
+ end
39
39
  end
@@ -8,94 +8,94 @@
8
8
  ##############################################################
9
9
 
10
10
  class Linen::Plugin
11
- class << self
12
- attr_reader :commands
13
- end
11
+ class << self
12
+ attr_reader :commands
13
+ end
14
14
 
15
15
 
16
- def self::inherited( plugin )
17
- Linen::PluginRegistry.instance.register plugin
18
- end
16
+ def self::inherited( plugin )
17
+ Linen::PluginRegistry.instance.register plugin
18
+ end
19
19
 
20
20
 
21
- def self::short_name( short_name = nil )
22
- @short_name = short_name if short_name
23
- return @short_name || self.to_s.downcase.gsub( /plugin$/, '' )
24
- end
21
+ def self::short_name( short_name = nil )
22
+ @short_name = short_name if short_name
23
+ return @short_name || self.to_s.downcase.gsub( /plugin$/, '' )
24
+ end
25
25
 
26
26
 
27
- def self::help
28
- output = []
27
+ def self::help
28
+ output = []
29
29
 
30
- desc = description || "No help for #{short_name}"
30
+ desc = description || "No help for #{short_name}"
31
31
 
32
- output << desc
33
- output << nil #blank line
34
- output << "Available commands:"
35
- output << nil #blank line
32
+ output << desc
33
+ output << nil #blank line
34
+ output << "Available commands:"
35
+ output << nil #blank line
36
36
 
37
- commands.each do |name, command|
38
- output << "- #{name}"
39
- end
37
+ commands.each do |name, command|
38
+ output << "- #{name}"
39
+ end
40
40
 
41
- output << nil #blank line
42
- output << "For detailed help on a command, enter \"help #{short_name} <command>\"".wrap
41
+ output << nil #blank line
42
+ output << "For detailed help on a command, enter \"help #{short_name} <command>\"".wrap
43
43
 
44
- return output.join( "\n" )
45
- end
44
+ return output.join( "\n" )
45
+ end
46
46
 
47
47
 
48
- def self::argument( name, opts = {} )
49
- @defined_arguments ||= IndifferentHash.new
48
+ def self::argument( name, opts = {} )
49
+ @defined_arguments ||= IndifferentHash.new
50
50
 
51
- if opts[:error_message].nil? and opts['error_message'].nil?
52
- opts[ :error_message ] = "The value for #{name} is invalid."
53
- end
51
+ if opts[:error_message].nil? and opts['error_message'].nil?
52
+ opts[ :error_message ] = "The value for #{name} is invalid."
53
+ end
54
54
 
55
- @defined_arguments[ name ] = Argument.new( self, name, opts )
56
- end
55
+ @defined_arguments[ name ] = Argument.new( self, name, opts )
56
+ end
57
57
 
58
58
 
59
- ### If args is empty, assume it was called like Plugin.arguments,
60
- ### so return the hash.
61
- def self::arguments( *args )
62
- @defined_arguments ||= IndifferentHash.new
59
+ ### If args is empty, assume it was called like Plugin.arguments,
60
+ ### so return the hash.
61
+ def self::arguments( *args )
62
+ @defined_arguments ||= IndifferentHash.new
63
63
 
64
- return @defined_arguments if args.empty?
64
+ return @defined_arguments if args.empty?
65
65
 
66
- args.each do |arg|
67
- argument arg
68
- end
69
- end
66
+ args.each do |arg|
67
+ argument arg
68
+ end
69
+ end
70
70
 
71
71
 
72
- def self::command( name, &block )
73
- @commands ||= IndifferentHash.new
72
+ def self::command( name, &block )
73
+ @commands ||= IndifferentHash.new
74
74
 
75
- @commands[ name ] = Command.new( self, name, &block )
76
- end
75
+ @commands[ name ] = Command.new( self, name, &block )
76
+ end
77
77
 
78
78
 
79
- ### multi-purpose method!
80
- ###
81
- ### if passed a block, set this plugin's cleanup proc to the block.
82
- ### if called without a block, execute the proc.
83
- ###
84
- ### this is meant to allow plugins to clean up after themselves.
85
- def self::cleanup( &block )
86
- if block_given?
87
- @cleanup_proc = block
88
- else
89
- # if we didn't define a proc, don't try to call it
90
- @cleanup_proc.call if @cleanup_proc
91
- end
92
- end
79
+ ### multi-purpose method!
80
+ ###
81
+ ### if passed a block, set this plugin's cleanup proc to the block.
82
+ ### if called without a block, execute the proc.
83
+ ###
84
+ ### this is meant to allow plugins to clean up after themselves.
85
+ def self::cleanup( &block )
86
+ if block_given?
87
+ @cleanup_proc = block
88
+ else
89
+ # if we didn't define a proc, don't try to call it
90
+ @cleanup_proc.call if @cleanup_proc
91
+ end
92
+ end
93
93
 
94
94
 
95
- ### define the plugin's description, or fetch it if nothing passed
96
- def self::description( input = nil )
97
- return @description unless input
95
+ ### define the plugin's description, or fetch it if nothing passed
96
+ def self::description( input = nil )
97
+ return @description unless input
98
98
 
99
- @description = input
100
- end
99
+ @description = input
100
+ end
101
101
  end