Class: Incline::CLI
- Inherits:
-
Object
show all
- Defined in:
- lib/incline/cli.rb,
lib/incline/cli/usage.rb,
lib/incline/cli/errors.rb,
lib/incline/cli/version.rb
Defined Under Namespace
Classes: CliError, Usage, UsageError, Version
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
15
16
17
|
# File 'lib/incline/cli.rb', line 15
def initialize
end
|
Class Method Details
.valid_commands ⇒ Object
70
71
72
73
74
|
# File 'lib/incline/cli.rb', line 70
def self.valid_commands
command_list.map do |cmd_info|
[ cmd_info[:method], cmd_info[:klass], cmd_info[:new_params] ]
end
end
|
Instance Method Details
#execute(*args) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/incline/cli.rb', line 19
def execute(*args)
begin
if args.empty? || %w(usage help /? -? -help --help).include?(args.first)
process_command(:usage)
else
process_command(*args)
end
rescue UsageError => err
STDERR.puts err.message
process_command(:usage)
rescue CliError => err
STDERR.puts ANSI.code(:red) { 'ERROR:' }
STDERR.puts err.message
rescue RuntimeError => err
STDERR.puts ANSI.code(:red) { 'FATAL ERROR:' }
STDERR.puts err.inspect
end
end
|
#process_command(command, *args) ⇒ Object
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
67
68
|
# File 'lib/incline/cli.rb', line 38
def process_command(command, *args)
command = command.to_sym
cmd_info = self.class.command_list.find{|c| c[:method] == command}
if cmd_info
args = args.dup
args = []
cmd_info[:new_params].each do |(type,name)|
if type == :rest
args += args
break
elsif type == :req
if args.empty?
raise UsageError, "Missing required parameter '#{name}' for command '#{command}'."
end
args << args.delete_at(0)
elsif type == :opt
if args.empty?
break
else
args << args.delete_at(0)
end
else
raise UsageError, "Unknown parameter type '#{type}' for command '#{command}'."
end
end
cmd_object = cmd_info[:klass].new(*args)
cmd_object.send(:run)
else
raise UsageError, "Unknown command '#{command}'."
end
end
|