Class: CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cli.rb,
lib/cli/dsl.rb

Defined Under Namespace

Modules: DSL Classes: Arguments, Options, ParserError, ParsingError, Switches, Values

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CLI

Returns a new instance of CLI.



111
112
113
114
115
116
117
118
119
120
# File 'lib/cli.rb', line 111

def initialize(&block)
	@arguments = Arguments.new
	@switches = Switches.new
	@options = Options.new

	instance_eval(&block) if block_given?

	switch :help, :short => :h, :description => 'display this help message'
	switch :version, :description => 'display version string' if @version
end

Instance Method Details

#argument(name, options = {}) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/cli.rb', line 134

def argument(name, options = {})
	argument_dsl = DSL::Argument.new(name, options)

	raise ParserError::ArgumentNameSpecifiedTwice.new(argument_dsl.name) if @arguments.has?(argument_dsl)

	@arguments << argument_dsl
end

#arguments(name, options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/cli.rb', line 142

def arguments(name, options = {})
	arguments_dsl = DSL::Arguments.new(name, options)

	raise ParserError::ArgumentNameSpecifiedTwice.new(arguments_dsl.name) if @arguments.has?(arguments_dsl)

	@arguments << arguments_dsl

	raise ParserError::MultipleArgumentsSpecifierError.new(@arguments.multiple) if @arguments.multiple.length > 1
end

#description(desc) ⇒ Object



122
123
124
# File 'lib/cli.rb', line 122

def description(desc)
	@description = desc
end

#nameObject



288
289
290
# File 'lib/cli.rb', line 288

def name
	File.basename $0
end

#option(name, options = {}) ⇒ Object



158
159
160
161
162
# File 'lib/cli.rb', line 158

def option(name, options = {})
	option_dsl = DSL::Option.new(name, options)
	check_switch_collision!(option_dsl)
	@options << option_dsl
end

#options(name, options = {}) ⇒ Object



164
165
166
167
168
# File 'lib/cli.rb', line 164

def options(name, options = {})
	option_dsl = DSL::Options.new(name, options)
	check_switch_collision!(option_dsl)
	@options << option_dsl
end

#parse(_argv = ARGV, stdin = STDIN, stderr = STDERR) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cli.rb', line 170

def parse(_argv = ARGV, stdin = STDIN, stderr = STDERR)
	values = Values.new
	argv = _argv.dup

	# check help and version
	argv.each do |arg|
		break if arg == '--'

		if arg == '-h' or arg == '--help' 
			values.help = usage
			return values
		end

		if @version and arg == '--version' 
			values.version = "#{name} version \"#{@version}\"\n"
			return values
		end
	end

	# initialize values
	@options.each do |o|
		values.value(o, nil)
	end

	@switches.each do |o|
		values.value(o, nil)
	end

	# initialize multi options
	@options.multiple.each do |o|
		values.value(o, [])
	end

	# set defaults
	@options.defaults.each do |o|
		values.value(o, o.cast(o.default))
	end

	# process switches
	mandatory_options = @options.mandatory.dup

	while not argv.first == '--' and Switches.is_switch?(argv.first)
		arg = argv.shift

		if switch = @switches.find(arg)
			values.set(switch)
		elsif option = @options.find(arg)
			value = argv.shift or raise ParsingError::MissingOptionValueError.new(option)
			if option.multiple?
				values.append(option, option.cast(value))
			else
				values.value(option, option.cast(value))
			end
			mandatory_options.delete(option)
		else
			raise ParsingError::UnknownSwitchError.new(arg) unless switch
		end
	end

	argv.shift if argv.first == '--'

	# check mandatory options
	raise ParsingError::MandatoryOptionsNotSpecifiedError.new(mandatory_options) unless mandatory_options.empty?

	# process arguments
	arguments = @arguments.dup
	while argument = arguments.shift
		value = if arguments.mandatory.length >= argv.length and argument.has_default?
			argument.default
		else
			raise ParsingError::MandatoryArgumentNotSpecifiedError.new(argument) if argv.empty?
			arg = argv.shift

			if argument.multiple?
				v = [arg]
				while argv.length > arguments.length
					v << argv.shift
				end
				v
			else
				arg
			end
		end

		values.value(argument, argument.cast(value))
	end

	# process stdin
	values.stdin = @stdin.cast(stdin) if @stdin

	values
end

#parse!(argv = ARGV, stdin = STDIN, stderr = STDERR, stdout = STDOUT) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cli.rb', line 263

def parse!(argv = ARGV, stdin = STDIN, stderr = STDERR, stdout = STDOUT)
	begin
		pp = parse(argv, stdin, stderr)
		if pp.help
			stdout.write pp.help
			exit 0
		end
		if pp.version
			stdout.write pp.version
			exit 0
		end

		if block_given?
			begin
				yield pp
			rescue RuntimeError => e
				raise ParsingError::UsageError, e.message
			end
		end
		pp
	rescue ParsingError => pe
		usage!(pe, stderr)
	end
end

#stdin(name = :data, options = {}) ⇒ Object



130
131
132
# File 'lib/cli.rb', line 130

def stdin(name = :data, options = {})
	@stdin = DSL::Input.new(name, options)
end

#switch(name, options = {}) ⇒ Object



152
153
154
155
156
# File 'lib/cli.rb', line 152

def switch(name, options = {})
	switch_dsl = DSL::Switch.new(name, options)
	check_switch_collision!(switch_dsl)
	@switches << switch_dsl
end

#usage(msg = nil) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/cli.rb', line 292

def usage(msg = nil)
	out = StringIO.new
	out.puts msg if msg
	out.print "Usage: #{name}"
	out.print ' [switches|options]' if not @switches.empty? and not @options.empty?
	out.print ' [switches]' if not @switches.empty? and @options.empty?
	out.print ' [options]' if @switches.empty? and not @options.empty?
	out.print ' [--]' if not @arguments.empty? and (not @switches.empty? or not @options.empty?)
	out.print ' ' + @arguments.map{|a| a.multiple? ? a.to_s + '*': a.to_s}.join(' ') unless @arguments.empty?
	out.print " < #{@stdin}" if @stdin

	out.puts
	out.puts @description if @description

	if @stdin and @stdin.description?
		out.puts "Input:"
		out.puts "   #{@stdin} - #{@stdin.description}"
	end

	unless @switches.empty?
		out.puts "Switches:"
		@switches.each do |s|
			out.print '   '
			out.print s.switch
			out.print " (#{s.switch_short})" if s.has_short?
			out.print " - #{s.description}" if s.description?
			out.puts
		end
	end

	unless @options.empty?
		out.puts "Options:"
		@options.each do |o|
			unless o.multiple?
				out.print "   #{o.switch}"
			else
				out.print "   #{o.switch}*"
			end
			out.print " (#{o.switch_short})" if o.has_short?
			out.print " [%s]" % o.default if o.has_default?
			out.print " - #{o.description}" if o.description?
			out.puts
		end
	end

	described_arguments = @arguments.select{|a| a.description?}
	unless described_arguments.empty?
		out.puts "Arguments:"
		described_arguments.each do |a|
			unless a.multiple?
				out.print "   #{a}"
			else
				out.print "   #{a}*"
			end
			out.print " [%s]" % (a.default.is_a?(Array) ? a.default.join(' ') : a.default) if a.has_default?
			out.print " - #{a.description}"
			out.puts
		end
	end

	out.rewind
	out.read
end

#usage!(msg = nil, io = STDERR) ⇒ Object



356
357
358
359
360
# File 'lib/cli.rb', line 356

def usage!(msg = nil, io = STDERR)
	msg = "Error: #{msg}" if msg
	io.write usage(msg)
	exit 42
end

#version(version) ⇒ Object



126
127
128
# File 'lib/cli.rb', line 126

def version(version)
	@version = version.to_s
end