Module: Climate
- Defined in:
- lib/climate.rb,
lib/climate/help.rb,
lib/climate/errors.rb,
lib/climate/parser.rb,
lib/climate/script.rb,
lib/climate/command.rb,
lib/climate/version.rb,
lib/climate/command_compat.rb
Defined Under Namespace
Modules: Described, ParsingMethods, Script
Classes: Argument, Command, CommandError, ConflictingOptionError, DefinitionError, Error, ExitException, Help, HelpNeeded, MissingArgumentError, MissingSubcommandError, Option, Parser, ParsingError, UnexpectedArgumentError, UnknownCommandError
Constant Summary
collapse
- VERSION =
'0.7.0'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.Command(name, &block) ⇒ Object
Create a new sub-class of Command with the given name. You can either extend this class in the traditional ‘class MyCommand < Command(’bob’) way` or you can define the class using class_eval by passing a block.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/climate/command.rb', line 6
def self.Command(name, &block)
Class.new(Command).tap do |clazz|
clazz.instance_eval """
def command_name
'#{name}'
end
"""
clazz.class_eval(&block) if block_given?
end
end
|
.error_messages ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/climate.rb', line 5
def self.error_messages
error_messages = {
UnexpectedArgumentError =>
proc {|e| "Unknown argument: #{e}" },
UnknownCommandError =>
proc {|e| "Unknown command '#{e}': #{e.command_class.ancestors.map(&:command_name).join(' ')} expects one of: #{e.command_class.subcommands.map(&:command_name).join(' ')}" },
MissingArgumentError =>
proc {|e| "Missing argument: #{e.message}" },
MissingSubcommandError =>
proc {|e| "Missing argument: #{e.command_class.ancestors.map(&:command_name).join(' ')} expects one of: #{e.command_class.subcommands.map(&:command_name).join(' ')}" },
ConflictingOptionError =>
proc {|e| "Conflicting options given: #{e}" }
}
end
|
.handle_error(e, options) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/climate.rb', line 32
def self.handle_error(e, options)
case e
when ExitException
stderr.puts(e.message) if e.has_message?
e.exit_code
when HelpNeeded
help(e.command_class).print(options)
0
when ParsingError
stderr.puts(error_messages[e.class].call(e))
help(e.command_class).print_usage
1
else
stderr.puts("Unexpected error: #{e.class.name} - #{e.message}")
stderr.puts(e.backtrace)
2
end
end
|
.help(command_class) ⇒ Object
52
53
54
|
# File 'lib/climate.rb', line 52
def self.help(command_class)
Help.new(command_class)
end
|
.print_usage(command_class, options = {}) ⇒ Object
56
57
58
|
# File 'lib/climate.rb', line 56
def self.print_usage(command_class, options={})
help(command_class).print_usage(options)
end
|
.stderr ⇒ Object
extracted for stubbing/overriding without having to do it globally
29
|
# File 'lib/climate.rb', line 29
def self.stderr ; $stderr ; end
|
.stdout ⇒ Object
30
|
# File 'lib/climate.rb', line 30
def self.stdout ; $stdout ; end
|
.with_standard_exception_handling(options = {}, &block) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/climate.rb', line 20
def self.with_standard_exception_handling(options={}, &block)
begin
yield
rescue => e
exit handle_error(e, options)
end
end
|
Instance Method Details
#run(&block) ⇒ Object
60
61
|
# File 'lib/climate.rb', line 60
def run(&block)
end
|