Class: Smol::Command
Defined Under Namespace
Modules: AutoMessage, Callbacks, ErrorHandler
Constant Summary
Constants included
from Coercion
Smol::Coercion::TRUTHY_VALUES
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Coercion
coerce_value
Methods included from Input
#ask, #choose, #confirm
Methods included from Output
#banner, #check_result, #debug, #desc, #failure, #header, #hint, #info, #label, #nl, #out, #success, #table, #verbose, #warning
Class Method Details
.after_action(method_name) ⇒ Object
110
111
112
113
|
# File 'lib/smol/command.rb', line 110
def after_action(method_name)
@after_actions ||= []
@after_actions << method_name
end
|
.after_actions ⇒ Object
115
116
117
|
# File 'lib/smol/command.rb', line 115
def after_actions
@after_actions || []
end
|
.aliases(*args) ⇒ Object
72
73
74
75
|
# File 'lib/smol/command.rb', line 72
def aliases(*args)
@aliases = args if args.any?
@aliases || []
end
|
.args(*args) ⇒ Object
77
78
79
80
|
# File 'lib/smol/command.rb', line 77
def args(*args)
@args = args if args.any?
@args || []
end
|
.before_action(method_name) ⇒ Object
101
102
103
104
|
# File 'lib/smol/command.rb', line 101
def before_action(method_name)
@before_actions ||= []
@before_actions << method_name
end
|
.before_actions ⇒ Object
106
107
108
|
# File 'lib/smol/command.rb', line 106
def before_actions
@before_actions || []
end
|
.command_name(text = nil) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/smol/command.rb', line 54
def command_name(text = nil)
if text
@command_name = text
else
@command_name || derive_command_name
end
end
|
.desc(text = nil) ⇒ Object
91
92
93
94
|
# File 'lib/smol/command.rb', line 91
def desc(text = nil)
@desc = text if text
@desc || ""
end
|
.error_handlers ⇒ Object
173
174
175
|
# File 'lib/smol/command.rb', line 173
def error_handlers
@error_handlers || []
end
|
.explain(text = nil) ⇒ Object
67
68
69
70
|
# File 'lib/smol/command.rb', line 67
def explain(text = nil)
@explain = text if text
@explain
end
|
.group(text = nil) ⇒ Object
96
97
98
99
|
# File 'lib/smol/command.rb', line 96
def group(text = nil)
@group = text if text
@group
end
|
.inherited(subclass) ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/smol/command.rb', line 10
def inherited(subclass)
super
subclass.prepend ErrorHandler
subclass.prepend Callbacks
subclass.prepend AutoMessage
register_to_app(subclass)
end
|
.matches?(input) ⇒ Boolean
119
120
121
|
# File 'lib/smol/command.rb', line 119
def matches?(input)
input == command_name.to_s || aliases.map(&:to_s).include?(input)
end
|
.option(name, short: nil, type: :string, default: nil, desc: nil) ⇒ Object
82
83
84
85
|
# File 'lib/smol/command.rb', line 82
def option(name, short: nil, type: :string, default: nil, desc: nil)
@options ||= {}
@options[name] = { short: short, type: type, default: default, desc: desc }
end
|
.options ⇒ Object
87
88
89
|
# File 'lib/smol/command.rb', line 87
def options
@options || {}
end
|
.parse_options(argv) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/smol/command.rb', line 133
def parse_options(argv)
positional = []
opts = options.transform_values { |o| o[:default] }
i = 0
while i < argv.length
arg = argv[i]
if arg.start_with?("--")
key, value = arg[2..].split("=", 2)
key = key.tr("-", "_").to_sym
if options[key]
value ||= argv[i += 1]
opts[key] = coerce_value(value, options[key][:type])
end
elsif arg.start_with?("-") && arg.length == 2
short = arg[1]
opt_name = options.find { |_, o| o[:short]&.to_s == short }&.first
if opt_name
value = argv[i += 1]
opts[opt_name] = coerce_value(value, options[opt_name][:type])
end
else
positional << arg
end
i += 1
end
[positional, opts]
end
|
.rescue_from(*exceptions, with: nil, &block) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/smol/command.rb', line 163
def rescue_from(*exceptions, with: nil, &block)
handler = block || with
raise ArgumentError, "rescue_from requires a block or :with handler" unless handler
@error_handlers ||= []
exceptions.each do |exception|
@error_handlers << [exception, handler]
end
end
|
.title(text = nil) ⇒ Object
62
63
64
65
|
# File 'lib/smol/command.rb', line 62
def title(text = nil)
@title = text if text
@title
end
|
.usage ⇒ Object
123
124
125
126
127
128
129
130
131
|
# File 'lib/smol/command.rb', line 123
def usage
parts = [command_name]
parts += args.map { |a| "<#{a}>" }
options.each do |name, opt|
flag = opt[:short] ? "-#{opt[:short]}/--#{name}" : "--#{name}"
parts << "[#{flag}]"
end
parts.join(" ")
end
|
Instance Method Details
#app ⇒ Object
250
251
252
|
# File 'lib/smol/command.rb', line 250
def app
app_class
end
|
#checking(name) ⇒ Object
254
255
256
257
|
# File 'lib/smol/command.rb', line 254
def checking(name)
warning "checking: #{name}"
nl
end
|
#checks_passed?(all_passed, pass_hint: nil, fail_hint: nil) ⇒ Boolean
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/smol/command.rb', line 270
def checks_passed?(all_passed, pass_hint: nil, fail_hint: nil)
nl
if all_passed
success "all checks passed"
hint pass_hint if pass_hint
else
failure "some checks failed"
hint fail_hint if fail_hint
end
all_passed
end
|
#config ⇒ Object
246
247
248
|
# File 'lib/smol/command.rb', line 246
def config
app_class.config
end
|
#done(hint_text = nil) ⇒ Object
264
265
266
267
268
|
# File 'lib/smol/command.rb', line 264
def done(hint_text = nil)
nl
success "done"
hint hint_text if hint_text
end
|
#dropping(target) ⇒ Object
259
260
261
262
|
# File 'lib/smol/command.rb', line 259
def dropping(target)
warning "dropping: #{target}"
nl
end
|
#run_checks(*check_classes, args: []) ⇒ Object
282
283
284
285
286
287
288
289
290
|
# File 'lib/smol/command.rb', line 282
def run_checks(*check_classes, args: [])
results = check_classes.map do |klass|
result = klass.new(*args).call
check_result(klass.check_name, result)
nl
result.passed?
end
results.all?
end
|