Class: HammerCLI::AbstractCommand
- Inherits:
-
Clamp::Command
- Object
- Clamp::Command
- HammerCLI::AbstractCommand
show all
- Includes:
- Subcommand
- Defined in:
- lib/hammer_cli/abstract.rb
Defined Under Namespace
Classes: SortedBuilder
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Subcommand
included
Constructor Details
Returns a new instance of AbstractCommand.
53
54
55
56
57
|
# File 'lib/hammer_cli/abstract.rb', line 53
def initialize(*args)
super
context[:path] ||= []
context[:path] << self
end
|
Class Attribute Details
.validation_block ⇒ Object
Returns the value of attribute validation_block.
14
15
16
|
# File 'lib/hammer_cli/abstract.rb', line 14
def validation_block
@validation_block
end
|
Class Method Details
.build_options(builder_params = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/hammer_cli/abstract.rb', line 128
def self.build_options(builder_params={})
builder_params = yield(builder_params) if block_given?
option_builder.build(builder_params).each do |option|
next if option.nil? or option.switches.any? {|s| find_option(s) }
declared_options << option
block ||= option.default_conversion_block
define_accessors_for(option, &block)
end
end
|
.option_builder ⇒ Object
123
124
125
126
|
# File 'lib/hammer_cli/abstract.rb', line 123
def self.option_builder
@option_builder ||= create_option_builder
@option_builder
end
|
.output(definition = nil, &block) ⇒ Object
97
98
99
100
101
102
|
# File 'lib/hammer_cli/abstract.rb', line 97
def self.output(definition=nil, &block)
dsl = HammerCLI::Output::Dsl.new
dsl.build &block if block_given?
output_definition.append definition.fields unless definition.nil?
output_definition.append dsl.fields
end
|
.output_definition ⇒ Object
113
114
115
116
|
# File 'lib/hammer_cli/abstract.rb', line 113
def self.output_definition
@output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new
@output_definition
end
|
.validate_options(&block) ⇒ Object
41
42
43
|
# File 'lib/hammer_cli/abstract.rb', line 41
def self.validate_options(&block)
self.validation_block = block
end
|
Instance Method Details
#adapter ⇒ Object
17
18
19
|
# File 'lib/hammer_cli/abstract.rb', line 17
def adapter
:base
end
|
#exception_handler ⇒ Object
49
50
51
|
# File 'lib/hammer_cli/abstract.rb', line 49
def exception_handler
@exception_handler ||= exception_handler_class.new(:output => output)
end
|
#execute ⇒ Object
37
38
39
|
# File 'lib/hammer_cli/abstract.rb', line 37
def execute
HammerCLI::EX_OK
end
|
#help ⇒ Object
93
94
95
|
# File 'lib/hammer_cli/abstract.rb', line 93
def help
self.class.help(invocation_path, SortedBuilder.new)
end
|
#output ⇒ Object
104
105
106
|
# File 'lib/hammer_cli/abstract.rb', line 104
def output
@output ||= HammerCLI::Output::Output.new(context, :default_adapter => adapter)
end
|
#output_definition ⇒ Object
108
109
110
|
# File 'lib/hammer_cli/abstract.rb', line 108
def output_definition
self.class.output_definition
end
|
#parent_command ⇒ Object
59
60
61
|
# File 'lib/hammer_cli/abstract.rb', line 59
def parent_command
context[:path][-2]
end
|
#parse(arguments) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/hammer_cli/abstract.rb', line 29
def parse(arguments)
super
validate_options
logger.info "Called with options: %s" % options.inspect
rescue HammerCLI::Validator::ValidationError => e
signal_usage_error e.message
end
|
#run(arguments) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/hammer_cli/abstract.rb', line 21
def run(arguments)
exit_code = super
raise "exit code must be integer" unless exit_code.is_a? Integer
return exit_code
rescue => e
handle_exception e
end
|
#validate_options ⇒ Object
45
46
47
|
# File 'lib/hammer_cli/abstract.rb', line 45
def validate_options
validator.run &self.class.validation_block if self.class.validation_block
end
|