forked from thbishop/fission
/
cli.rb
66 lines (54 loc) · 1.47 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
module Fission
class CLI
def self.execute(args=ARGV)
optparse = OptionParser.new do |opts|
opts.banner = "\nUsage: fission [options] COMMAND [arguments]"
opts.on_head('-v', '--version', 'Output the version of fission') do
Fission.ui.output Fission::VERSION
exit(0)
end
opts.on_head('-h', '--help', 'Displays this message') do
show_all_help(optparse)
exit(0)
end
opts.define_tail do
commands_banner
end
end
begin
optparse.order! args
rescue OptionParser::InvalidOption => e
Fission.ui.output e
show_all_help(optparse)
exit(1)
end
if commands.include?(args.first)
@cmd = Fission::Command.const_get(args.first.capitalize).new args.drop 1
@cmd.execute
else
show_all_help(optparse)
exit(1)
end
end
def self.commands
cmds = []
Dir.entries(File.join(File.dirname(__FILE__), 'command')).select do |file|
cmds << File.basename(file, '.rb') unless File.directory? file
end
cmds
end
private
def self.commands_banner
text = "\nCommands:\n"
Fission::Command.descendants.each do |command_klass|
text << (command_klass.send :help)
text << "\n\n"
end
text
end
def self.show_all_help(options)
Fission.ui.output options
Fission.ui.output commands_banner
end
end
end