-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcommand.rb
365 lines (346 loc) · 9.24 KB
/
command.rb
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
# frozen_string_literal: true
require 'forwardable'
require 'open3'
require 'dry/cli/program_name'
require 'concurrent/array'
require 'dry/cli/option'
module Dry
class CLI
# Base class for commands
#
# @since 0.1.0
class Command
# @since 0.1.0
# @api private
def self.inherited(base)
super
base.extend ClassMethods
base.include Dry::Effects.Reader(:cli)
end
# @since 0.1.0
# @api private
module ClassMethods
# @since 0.1.0
# @api private
def self.extended(base)
super
base.class_eval do
@description = nil
@examples = Concurrent::Array.new
@arguments = Concurrent::Array.new
@options = Concurrent::Array.new
end
end
# @since 0.1.0
# @api private
attr_reader :description
# @since 0.1.0
# @api private
attr_reader :examples
# @since 0.1.0
# @api private
attr_reader :arguments
# @since 0.1.0
# @api private
attr_reader :options
end
# Set the description of the command
#
# @param description [String] the description
#
# @since 0.1.0
#
# @example
# require "dry/cli"
#
# class Echo < Dry::CLI::Command
# desc "Prints given input"
#
# def call(*)
# # ...
# end
# end
def self.desc(description)
@description = description
end
# Describe the usage of the command
#
# @param examples [Array<String>] one or more examples
#
# @since 0.1.0
#
# @example
# require "dry/cli"
#
# class Server < Dry::CLI::Command
# example [
# " # Basic usage (it uses the bundled server engine)",
# "--server=webrick # Force `webrick` server engine",
# "--host=0.0.0.0 # Bind to a host",
# "--port=2306 # Bind to a port",
# "--no-code-reloading # Disable code reloading"
# ]
#
# def call(*)
# # ...
# end
# end
#
# # $ foo server --help
# # # ...
# #
# # Examples:
# # foo server # Basic usage (it uses the bundled server engine)
# # foo server --server=webrick # Force `webrick` server engine
# # foo server --host=0.0.0.0 # Bind to a host
# # foo server --port=2306 # Bind to a port
# # foo server --no-code-reloading # Disable code reloading
def self.example(*examples)
@examples += examples.flatten
end
# Specify an argument
#
# @param name [Symbol] the argument name
# @param options [Hash] a set of options
#
# @since 0.1.0
#
# @example Optional argument
# require "dry/cli"
#
# class Hello < Dry::CLI::Command
# argument :name
#
# def call(name: nil, **)
# if name.nil?
# puts "Hello, stranger"
# else
# puts "Hello, #{name}"
# end
# end
# end
#
# # $ foo hello
# # Hello, stranger
#
# # $ foo hello Luca
# # Hello, Luca
#
# @example Required argument
# require "dry/cli"
#
# class Hello < Dry::CLI::Command
# argument :name, required: true
#
# def call(name:, **)
# puts "Hello, #{name}"
# end
# end
#
# # $ foo hello Luca
# # Hello, Luca
#
# # $ foo hello
# # ERROR: "foo hello" was called with no arguments
# # Usage: "foo hello NAME"
#
# @example Multiple arguments
# require "dry/cli"
#
# module Generate
# class Action < Dry::CLI::Command
# argument :app, required: true
# argument :action, required: true
#
# def call(app:, action:, **)
# puts "Generating action: #{action} for app: #{app}"
# end
# end
# end
#
# # $ foo generate action web home
# # Generating action: home for app: web
#
# # $ foo generate action
# # ERROR: "foo generate action" was called with no arguments
# # Usage: "foo generate action APP ACTION"
#
# @example Description
# require "dry/cli"
#
# class Hello < Dry::CLI::Command
# argument :name, desc: "The name of the person to greet"
#
# def call(name: nil, **)
# # ...
# end
# end
#
# # $ foo hello --help
# # Command:
# # foo hello
# #
# # Usage:
# # foo hello [NAME]
# #
# # Arguments:
# # NAME # The name of the person to greet
# #
# # Options:
# # --help, -h # Print this help
def self.argument(name, options = {})
@arguments << Argument.new(name, options)
end
# Command line option (aka optional argument)
#
# @param name [Symbol] the param name
# @param options [Hash] a set of options
#
# @since 0.1.0
#
# @example Basic usage
# require "dry/cli"
#
# class Console < Dry::CLI::Command
# param :engine
#
# def call(engine: nil, **)
# puts "starting console (engine: #{engine || :irb})"
# end
# end
#
# # $ foo console
# # starting console (engine: irb)
#
# # $ foo console --engine=pry
# # starting console (engine: pry)
#
# @example List values
# require "dry/cli"
#
# class Console < Dry::CLI::Command
# param :engine, values: %w(irb pry ripl)
#
# def call(engine: nil, **)
# puts "starting console (engine: #{engine || :irb})"
# end
# end
#
# # $ foo console
# # starting console (engine: irb)
#
# # $ foo console --engine=pry
# # starting console (engine: pry)
#
# # $ foo console --engine=foo
# # Error: Invalid param provided
#
# @example Description
# require "dry/cli"
#
# class Console < Dry::CLI::Command
# param :engine, desc: "Force a console engine"
#
# def call(engine: nil, **)
# # ...
# end
# end
#
# # $ foo console --help
# # # ...
# #
# # Options:
# # --engine=VALUE # Force a console engine: (irb/pry/ripl)
# # --help, -h # Print this help
#
# @example Boolean
# require "dry/cli"
#
# class Server < Dry::CLI::Command
# param :code_reloading, type: :boolean, default: true
#
# def call(code_reloading:, **)
# puts "staring server (code reloading: #{code_reloading})"
# end
# end
#
# # $ foo server
# # starting server (code reloading: true)
#
# # $ foo server --no-code-reloading
# # starting server (code reloading: false)
#
# # $ foo server --help
# # # ...
# #
# # Options:
# # --[no]-code-reloading
#
# @example Aliases
# require "dry/cli"
#
# class Server < Dry::CLI::Command
# param :port, aliases: ["-p"]
#
# def call(options)
# puts "staring server (port: #{options.fetch(:port, 2300)})"
# end
# end
#
# # $ foo server
# # starting server (port: 2300)
#
# # $ foo server --port=2306
# # starting server (port: 2306)
#
# # $ foo server -p 2306
# # starting server (port: 2306)
#
# # $ foo server --help
# # # ...
# #
# # Options:
# # --port=VALUE, -p VALUE
def self.option(name, options = {})
@options << Option.new(name, options)
end
# @since 0.1.0
# @api private
def self.params
(@arguments + @options).uniq
end
# @since 0.1.0
# @api private
def self.default_params
params.each_with_object({}) do |param, result|
result[param.name] = param.default unless param.default.nil?
end
end
# @since 0.1.0
# @api private
def self.required_arguments
arguments.select(&:required?)
end
# @since 0.1.0
# @api private
def self.optional_arguments
arguments.reject(&:required?)
end
extend Forwardable
delegate %i[
description
examples
arguments
options
params
default_params
required_arguments
optional_arguments
] => 'self.class'
def invoke(command, *arguments)
command = [command] unless command.is_a?(Array)
cli.call(arguments: [*command, *arguments])
end
end
end
end