-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand_base.cr
More file actions
389 lines (333 loc) · 10.8 KB
/
command_base.cr
File metadata and controls
389 lines (333 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
require "./command_base/macros"
module Cli
# The base of command classes.
#
# Your application should not directly inherit this class. Instead, use `Command` or `Supercommand`.
abstract class CommandBase
Callback.enable
macro inherited
{% if @type.superclass != ::Cli::CommandBase %}
{%
type_id = @type.name.split("(")[0].split("::").join("_").id
last_name = @type.name.split("::").last.id
snake_type_id = type_id.underscore
%}
{% if @type.superclass == ::Cli::Command %}
{%
is_command_root = true
is_supercommand_root = false
is_supercommand = false
super_option_data = "Cli::OptionModel".id
%}
{% elsif @type.superclass == ::Cli::Supercommand %}
{%
is_command_root = false
is_supercommand_root = true
is_supercommand = true
super_option_data = "Cli::OptionModel".id
%}
{% else %}
{%
is_command_root = false
is_supercommand_root = false
is_supercommand = @type < ::Cli::Supercommand
super_option_data = "#{@type.superclass}::Options".id
%}
{% end %}
{% if is_command_root || is_supercommand_root %}
define_callback_group :initialize
define_callback_group :exit, proc_type: Proc(::Cli::Exit, Nil)
{% else %}
inherit_callback_group :initialize
inherit_callback_group :exit, proc_type: Proc(::Cli::Exit, Nil)
{% end %}
# The dedicated Cli::OptionModel subclass for the `{{last_name}}` class.
#
# This class is automatically defined by the Crystal CLI library.
class Options < ::{{super_option_data}}
end
# :nodoc:
macro __define_run(klass)
\{%
klass = klass.resolve if klass.class_name == "Path"
\%}
# :nodoc:
class ::Cli::CommandClass
{% unless @type.abstract? %}
# :nodoc:
def {{snake_type_id}}__run(argv)
{{snake_type_id}}__run(nil, argv)
end
# :nodoc:
def {{snake_type_id}}__run(argv, &block : ::\{{klass}} ->)
{{snake_type_id}}__run(nil, argv, &block)
end
# :nodoc:
def {{snake_type_id}}__run(previous, argv)
{{snake_type_id}}__run(previous, argv) {}
end
# :nodoc:
def {{snake_type_id}}__run(previous, argv, &block : ::\{{klass}} ->)
cmd = ::\{{klass}}.new(previous, argv)
rescue_exit(cmd) do
rescue_error(cmd) do
begin
cmd.__option_data.__parse
result = cmd.run
cmd.io.close_writer unless previous
yield cmd
result
ensure
cmd.io.close_writer unless previous
end
end
end
end
# :nodoc:
def rescue_exit(cmd)
if cmd.__previous?
yield
else
begin
result = yield
cmd.run_callbacks_for_exit(::Cli::Exit.new) {}
result
rescue ex : ::Cli::Exit
if ::Cli.test?
cmd.run_callbacks_for_exit(ex) {}
ex
else
cmd.run_callbacks_for_exit ex do
ex.stdout.puts ex.message if ex.message
end
exit ex.exit_code
end
end
end
end
@@runners[\{{klass.name.stringify}}] = Runner.new do |previous, args|
::\{{klass}}.__klass.{{snake_type_id}}__run(previous, args)
end
{% end %}
end
# Run the command.
#
# This method is automatically defined by the Crystal CLI library.
def self.run(argv : Array(String) = \\%w(), &block : ::\{{klass}} ->)
__klass.{{snake_type_id}}__run(argv, &block)
end
end
__define_run ::{{@type}}
@@__klass = ::Cli::CommandClass.new(
supercommand: __get_supercommand_class,
inherited_class: {{ is_command_root || is_supercommand_root ? nil : "::#{@type.superclass}.__klass".id }},
class_name: {{@type.name.stringify}},
name: ::StringInflection.kebab(::{{@type}}.name.split("::").last),
is_supercommand: {{is_supercommand}},
abstract: {{@type.abstract?}},
options: Options.__klass
)
# :nodoc:
def self.__klass; @@__klass; end
# :nodoc:
def __klass; @@__klass; end
{% unless @type.abstract? %}
if @@__klass.supercommand?
@@__klass.supercommand << @@__klass
end
# Run the command.
#
# This method is automatically defined by the Crystal CLI library.
def self.run
run(\%w())
end
# Run the command.
#
# This method is automatically defined by the Crystal CLI library.
def self.run(argv : Array(String))
__klass.{{snake_type_id}}__run(argv)
end
# Run the command.
#
# This method is automatically defined by the Crystal CLI library.
def self.run(previous : ::Cli::CommandBase, argv : Array(String) = \%w())
__klass.{{snake_type_id}}__run(previous, argv)
end
{% end %}
class Options
# :nodoc:
def self.__cli_command
::{{@type}}
end
# :nodoc:
def __cli_command
@__cli_command.as(::{{@type}})
end
{% if is_supercommand_root %}
__klass.definitions << ::{{@type}}.__klass.subcommand_option_model_definition
{% end %}
end
# Configures help message attributes for the `{{last_name}}` class.
#
# This class is automatically defined by the Crystal CLI library.
class Help
# Sets the caption.
def self.caption(s : String)
::{{@type}}.__klass.caption = s
end
# Sets the title.
def self.title(s : String)
::{{@type}}.__klass.title = s
end
# Sets the header.
def self.header(s : String)
::{{@type}}.__klass.header = s
end
# Sets the footer.
def self.footer(s : String)
::{{@type}}.__klass.footer = s
end
# Sets the string for unparsed arguments.
def self.unparsed_args(s)
::{{@type}}.__klass.unparsed_args = s
end
end
# :nodoc:
def __option_data
(@__option_data.var ||= Options.new(@__argv, self)).as(Options)
end
{% end %}
end
# :nodoc:
getter? __previous : ::Cli::CommandBase?
@__argv : Array(String)
# :nodoc:
def initialize(argv)
initialize nil, argv
end
# :nodoc:
def initialize(@__previous, @__argv)
run_callbacks_for_initialize { }
end
@__option_data = Util::Var(Optarg::Model).new
# Returns option and argument values (an `OptionModel` instance).
#
# This method is the same as `#args`.
def options
__option_data
end
# Returns option and argument values (an `OptionModel` instance).
#
# This method is the same as `#options`.
def args
__option_data
end
# Returns an array of nameless argument values.
#
# This method is a short form of `#args`.nameless_args.
def nameless_args : Array(String)
__option_data.nameless_args
end
# Returns an array of unparsed argument values.
#
# This method is a short form of `#args`.unparsed_args.
def unparsed_args : Array(String)
__option_data.unparsed_args
end
# Returns the command version.
def version : String
__klass.version
end
# Returns the command version.
#
# Returns nil, if no version is set.
def version? : String?
__klass.version?
end
# Sets the command name.
def self.command_name(value : String)
__klass.name = value
end
# Disables printing a help message when a parsing error occurs.
def self.disable_help_on_parsing_error!
__klass.disable_help_on_parsing_error!
end
# Sets the command version.
def self.version(value : String)
__klass.version = value
end
# Prints a help message and exits the command.
def help!(message : String? = nil, error : Bool? = nil, code : Int32? = nil, indent = 2)
error = !message.nil? if error.nil?
exit! message, error, code, true, indent
end
# Exits the command.
def exit!(message : String? = nil, error : Bool = false, code : Int32? = nil, help = false, indent = 2)
a = %w()
a << message if message
if help
a << __klass.new_help(indent: indent).text
end
message = a.join("\n\n") unless a.empty?
code ||= error ? 1 : 0
raise ::Cli::Exit.new(message, code)
end
# Exits the command with an error status.
def error!(message : String? = nil, code : Int32? = nil, help : Bool = false, indent = 2)
exit! message, true, code, help, indent
end
# Prints a version string and exits the command.
def version!
exit! version
end
# Runs the command.
#
# This method is an entrypoint for running a command.
#
# Subclasses must override this method.
def run
raise "Not implemented."
end
# Generates a bash completion script.
def self.generate_bash_completion
__klass.generate_bash_completion
end
# Generates a zsh completion script.
def self.generate_zsh_completion(functional : Bool = true)
__klass.generate_zsh_completion(functional)
end
@io : IoHash?
# Returns a named IO container.
def io
@io ||= if prev = @__previous
prev.io
else
Cli.new_default_io
end
end
# Invokes the :out IO's puts method.
def puts(*args)
io[:out].puts *args
end
# Invokes the :out IO'S print method.
def print(*args)
io[:out].print *args
end
# Returns the :out IO.
def out
io[:out]
end
# Returns the :err IO.
def err
io[:err]
end
# :nodoc:
def self.replacer_command
end
# Replaces this command with the *klass* command.
macro replacer_command(klass)
__define_run {{klass}}
@@__klass.completable = false
end
end
end