-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcli.rb
198 lines (176 loc) · 4.88 KB
/
cli.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
# frozen_string_literal: true
require 'dry/effects'
# Dry
#
# @since 0.1.0
module Dry
# General purpose Command Line Interface (CLI) framework for Ruby
#
# @since 0.1.0
class CLI
require 'dry/cli/version'
require 'dry/cli/errors'
require 'dry/cli/command'
require 'dry/cli/registry'
require 'dry/cli/parser'
require 'dry/cli/usage'
require 'dry/cli/banner'
require 'dry/cli/inflector'
include Dry::Effects::Handler.Reader(:cli)
# Check if command
#
# @param command [Object] the command to check
#
# @return [TrueClass,FalseClass] true if instance of `Dry::CLI::Command`
#
# @since 0.1.0
# @api private
def self.command?(command)
case command
when Class
command.ancestors.include?(Command)
else
command.is_a?(Command)
end
end
# Create a new instance
#
# @param command_or_registry [Dry::CLI::Registry, Dry::CLI::Command]
# a registry or singular command
# @param &block [Block] a configuration block for registry
#
# @return [Dry::CLI] the new instance
# @since 0.1.0
def initialize(command_or_registry = nil, &block)
@kommand = command_or_registry if command?(command_or_registry)
@registry =
if block_given?
anonymous_registry(&block)
else
command_or_registry
end
end
# Invoke the CLI
#
# @param arguments [Array<string>] the command line arguments (defaults to `ARGV`)
# @param out [IO] the standard output (defaults to `$stdout`)
#
# @since 0.1.0
def call(arguments: ARGV, out: $stdout)
return perform_command(arguments, out) if kommand
perform_registry(arguments, out)
end
private
# @since 0.6.x
# @api private
attr_reader :registry
# @since 0.6.x
# @api private
attr_reader :kommand
# Invoke the CLI if singular command passed
#
# @param arguments [Array<string>] the command line arguments
# @param out [IO] the standard output (defaults to `$stdout`)
#
# @since 0.6.x
# @api private
def perform_command(arguments, out)
command, args = parse(kommand, arguments, [], out)
command.call(**args)
end
# Invoke the CLI if registry passed
#
# @param arguments [Array<string>] the command line arguments
# @param out [IO] the standard output (defaults to `$stdout`)
#
# @since 0.6.x
# @api private
def perform_registry(arguments, out)
result = registry.get(arguments)
if result.found?
command, args = parse(result.command, result.arguments, result.names, out)
result.before_callbacks.run(command, args)
with_cli(self) { command.call(**args) }
result.after_callbacks.run(command, args)
else
usage(result, out)
end
end
# Parse arguments for a command.
#
# It may exit in case of error, or in case of help.
#
# @param result [Dry::CLI::CommandRegistry::LookupResult]
# @param out [IO] sta output
#
# @return [Array<Dry:CLI::Command, Array>] returns an array where the
# first element is a command and the second one is the list of arguments
#
# @since 0.6.x
# @api private
def parse(command, arguments, names, out)
result = Parser.call(command, arguments, names)
if result.help?
Banner.call(command, out, names)
exit(0)
end
if result.error?
out.puts(result.error)
exit(1)
end
[command.new, result.arguments]
end
# Prints the command usage and exit.
#
# @param result [Dry::CLI::CommandRegistry::LookupResult]
# @param out [IO] sta output
#
# @since 0.1.0
# @api private
def usage(result, out)
Usage.call(result, out)
exit(1)
end
# Check if command
#
# @param command [Object] the command to check
#
# @return [TrueClass,FalseClass] true if instance of `Dry::CLI::Command`
#
# @since 0.1.0
# @api private
#
# @see .command?
def command?(command)
CLI.command?(command)
end
# Generates registry in runtime
#
# @param &block [Block] configuration for the registry
#
# @return [Module] module extended with registry abilities and configured with a block
#
# @since 0.4.0
# @api private
def anonymous_registry(&block)
registry = Module.new { extend(Dry::CLI::Registry) }
if block.arity.zero?
registry.instance_eval(&block)
else
yield(registry)
end
registry
end
end
# Create a new instance
#
# @param registry_or_command [Dry::CLI::Registry, Dry::CLI::Command]
# a registry or singular command
# @param &block [Block] a configuration block for registry
#
# @return [Dry::CLI] the new instance
# @since 0.4.0
def self.CLI(registry_or_command = nil, &block)
CLI.new(registry_or_command, &block)
end
end