Class: Gestopft::App

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/gestopft/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ App

Returns a new instance of App.



30
31
32
33
34
35
# File 'lib/gestopft/app.rb', line 30

def initialize(argv)
	@argv = argv.option_args
	@expectation = self.class.expectation
	@options = {}
	@commands = self.class.commands.map {|cmd| method(cmd) }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/gestopft/app.rb', line 28

def options
  @options
end

Class Method Details

.commandsObject



24
25
26
# File 'lib/gestopft/app.rb', line 24

def self.commands
	public_instance_methods - Gestopft::App.public_instance_methods
end

.expectationObject



6
7
8
# File 'lib/gestopft/app.rb', line 6

def self.expectation
	@expectation ||= []
end

.option(name, *args) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/gestopft/app.rb', line 10

def self.option(name, *args)
	desc = args.find {|arg| arg.is_a? String }
	params = args.find {|arg| arg.is_a? Array }
	expectation << Gestopft::Option.new(name, {
		:desc => desc,
		:params => params
	})
end

.run(argv = ARGV) ⇒ Object



19
20
21
22
# File 'lib/gestopft/app.rb', line 19

def self.run(argv=ARGV)
	new(argv).parse_arg.dispatch
	self
end

Instance Method Details

#__default__Object



98
99
100
# File 'lib/gestopft/app.rb', line 98

def __default__
	STDIN.puts help_message
end

#dispatchObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/gestopft/app.rb', line 57

def dispatch
	argv = @argv.reject {|arg| arg.option? }
	@commands.each do |cmd|
		if pos = argv.find_index(cmd.name.to_s)
			params = argv[pos + 1, cmd.arity.abs]
			return cmd.call(*params)
		end
	end
	send(:__default__)
end

#help_messageObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gestopft/app.rb', line 68

def help_message
	msg = []
	msg << <<-BANNER.strip
Usage:
$ #{File.basename($0)} [options] #{"command [args, ...]" unless @commands.empty?}
	BANNER

	unless @expectation.empty?
		msg << "Options: "
		@expectation.each do |opt|
			line = ["\t", opt.option_name]
			line << opt.params.map {|param|
				param.to_s.upcase
			}.join(' ') unless opt.params.empty?
			line << opt.desc unless opt.desc.empty?
			msg << line.join(" ")
		end
	end

	unless @commands.empty?
		msg << <<-COMMANDS.strip
Commands:
#{@commands.map {|cmd| cmd.name }.join(' ')}
		COMMANDS
	end

	msg << ""
	msg.join("\n\n")
end

#parse_argObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gestopft/app.rb', line 37

def parse_arg
	@expectation.each do |opt|
		if opt.require_args?
			if (
				pos = @argv.find_index(opt.option_name) and
				params = @argv[pos + 1, opt.arity] and
				params.none? {|param| param.option? } and
				params.size == opt.arity
			)
				@options[opt.name] = params
			else
				raise NotSatisfiedRequirements
			end
		else
			@options[opt.name] = opt.option_name
		end
	end
	self
end