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.
56
57
58
59
60
|
# File 'lib/hammer_cli/abstract.rb', line 56
def initialize(*args)
super
context[:path] ||= []
context[:path] << self
end
|
Class Attribute Details
.validation_block ⇒ Object
Returns the value of attribute validation_block.
15
16
17
|
# File 'lib/hammer_cli/abstract.rb', line 15
def validation_block
@validation_block
end
|
Class Method Details
.build_options(builder_params = {}) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/hammer_cli/abstract.rb', line 130
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
125
126
127
128
|
# File 'lib/hammer_cli/abstract.rb', line 125
def self.option_builder
@option_builder ||= create_option_builder
@option_builder
end
|
.output(definition = nil, &block) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/hammer_cli/abstract.rb', line 99
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
115
116
117
118
|
# File 'lib/hammer_cli/abstract.rb', line 115
def self.output_definition
@output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new
@output_definition
end
|
.validate_options(&block) ⇒ Object
44
45
46
|
# File 'lib/hammer_cli/abstract.rb', line 44
def self.validate_options(&block)
self.validation_block = block
end
|
Instance Method Details
#adapter ⇒ Object
18
19
20
|
# File 'lib/hammer_cli/abstract.rb', line 18
def adapter
:base
end
|
#exception_handler ⇒ Object
52
53
54
|
# File 'lib/hammer_cli/abstract.rb', line 52
def exception_handler
@exception_handler ||= exception_handler_class.new(:output => output)
end
|
#execute ⇒ Object
40
41
42
|
# File 'lib/hammer_cli/abstract.rb', line 40
def execute
HammerCLI::EX_OK
end
|
#help ⇒ Object
95
96
97
|
# File 'lib/hammer_cli/abstract.rb', line 95
def help
self.class.help(invocation_path, SortedBuilder.new)
end
|
#interactive? ⇒ Boolean
121
122
123
|
# File 'lib/hammer_cli/abstract.rb', line 121
def interactive?
HammerCLI.interactive?
end
|
#output ⇒ Object
106
107
108
|
# File 'lib/hammer_cli/abstract.rb', line 106
def output
@output ||= HammerCLI::Output::Output.new(context, :default_adapter => adapter)
end
|
#output_definition ⇒ Object
110
111
112
|
# File 'lib/hammer_cli/abstract.rb', line 110
def output_definition
self.class.output_definition
end
|
#parent_command ⇒ Object
62
63
64
|
# File 'lib/hammer_cli/abstract.rb', line 62
def parent_command
context[:path][-2]
end
|
#parse(arguments) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/hammer_cli/abstract.rb', line 30
def parse(arguments)
super
validate_options
safe_options = options.dup
safe_options.keys.each { |k| safe_options[k] = '***' if k.end_with?('password') }
logger.info "Called with options: %s" % safe_options.inspect
rescue HammerCLI::Validator::ValidationError => e
signal_usage_error e.message
end
|
#run(arguments) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/hammer_cli/abstract.rb', line 22
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
48
49
50
|
# File 'lib/hammer_cli/abstract.rb', line 48
def validate_options
validator.run &self.class.validation_block if self.class.validation_block
end
|