toys-core 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +98 -0
  3. data/LICENSE.md +16 -24
  4. data/README.md +307 -59
  5. data/docs/guide.md +44 -4
  6. data/lib/toys-core.rb +58 -49
  7. data/lib/toys/acceptor.rb +672 -0
  8. data/lib/toys/alias.rb +106 -0
  9. data/lib/toys/arg_parser.rb +624 -0
  10. data/lib/toys/cli.rb +422 -181
  11. data/lib/toys/compat.rb +83 -0
  12. data/lib/toys/completion.rb +442 -0
  13. data/lib/toys/context.rb +354 -0
  14. data/lib/toys/core_version.rb +18 -26
  15. data/lib/toys/dsl/flag.rb +213 -56
  16. data/lib/toys/dsl/flag_group.rb +237 -51
  17. data/lib/toys/dsl/positional_arg.rb +210 -0
  18. data/lib/toys/dsl/tool.rb +968 -317
  19. data/lib/toys/errors.rb +46 -28
  20. data/lib/toys/flag.rb +821 -0
  21. data/lib/toys/flag_group.rb +282 -0
  22. data/lib/toys/input_file.rb +18 -26
  23. data/lib/toys/loader.rb +110 -100
  24. data/lib/toys/middleware.rb +24 -31
  25. data/lib/toys/mixin.rb +90 -59
  26. data/lib/toys/module_lookup.rb +125 -0
  27. data/lib/toys/positional_arg.rb +184 -0
  28. data/lib/toys/source_info.rb +192 -0
  29. data/lib/toys/standard_middleware/add_verbosity_flags.rb +38 -43
  30. data/lib/toys/standard_middleware/handle_usage_errors.rb +39 -40
  31. data/lib/toys/standard_middleware/set_default_descriptions.rb +111 -89
  32. data/lib/toys/standard_middleware/show_help.rb +130 -113
  33. data/lib/toys/standard_middleware/show_root_version.rb +29 -35
  34. data/lib/toys/standard_mixins/exec.rb +116 -78
  35. data/lib/toys/standard_mixins/fileutils.rb +16 -24
  36. data/lib/toys/standard_mixins/gems.rb +29 -30
  37. data/lib/toys/standard_mixins/highline.rb +34 -41
  38. data/lib/toys/standard_mixins/terminal.rb +72 -26
  39. data/lib/toys/template.rb +51 -35
  40. data/lib/toys/tool.rb +1161 -206
  41. data/lib/toys/utils/completion_engine.rb +171 -0
  42. data/lib/toys/utils/exec.rb +279 -182
  43. data/lib/toys/utils/gems.rb +58 -49
  44. data/lib/toys/utils/help_text.rb +117 -111
  45. data/lib/toys/utils/terminal.rb +69 -62
  46. data/lib/toys/wrappable_string.rb +162 -0
  47. metadata +24 -22
  48. data/lib/toys/definition/acceptor.rb +0 -191
  49. data/lib/toys/definition/alias.rb +0 -112
  50. data/lib/toys/definition/arg.rb +0 -140
  51. data/lib/toys/definition/flag.rb +0 -370
  52. data/lib/toys/definition/flag_group.rb +0 -205
  53. data/lib/toys/definition/source_info.rb +0 -190
  54. data/lib/toys/definition/tool.rb +0 -842
  55. data/lib/toys/dsl/arg.rb +0 -132
  56. data/lib/toys/runner.rb +0 -188
  57. data/lib/toys/standard_middleware.rb +0 -47
  58. data/lib/toys/utils/module_lookup.rb +0 -135
  59. data/lib/toys/utils/wrappable_string.rb +0 -165
@@ -1,32 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2018 Daniel Azuma
3
+ # Copyright 2019 Daniel Azuma
4
4
  #
5
- # All rights reserved.
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
6
11
  #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
9
14
  #
10
- # * Redistributions of source code must retain the above copyright notice,
11
- # this list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the copyright holder, nor the names of any other
16
- # contributors to this software, may be used to endorse or promote products
17
- # derived from this software without specific prior written permission.
18
- #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
- # POSSIBILITY OF SUCH DAMAGE.
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ # IN THE SOFTWARE.
30
22
  ;
31
23
 
32
24
  module Toys
@@ -125,7 +117,7 @@ module Toys
125
117
  ##
126
118
  # Create a ShowHelp middleware.
127
119
  #
128
- # @param [Boolean,Array<String>,Proc] help_flags Specify flags to
120
+ # @param help_flags [Boolean,Array<String>,Proc] Specify flags to
129
121
  # display help. The value may be any of the following:
130
122
  #
131
123
  # * An array of flags.
@@ -133,7 +125,7 @@ module Toys
133
125
  # * The `false` value for no flags. (Default)
134
126
  # * A proc that takes a tool and returns any of the above.
135
127
  #
136
- # @param [Boolean,Array<String>,Proc] usage_flags Specify flags to
128
+ # @param usage_flags [Boolean,Array<String>,Proc] Specify flags to
137
129
  # display usage. The value may be any of the following:
138
130
  #
139
131
  # * An array of flags.
@@ -141,7 +133,7 @@ module Toys
141
133
  # * The `false` value for no flags. (Default)
142
134
  # * A proc that takes a tool and returns any of the above.
143
135
  #
144
- # @param [Boolean,Array<String>,Proc] list_flags Specify flags to
136
+ # @param list_flags [Boolean,Array<String>,Proc] Specify flags to
145
137
  # display subtool list. The value may be any of the following:
146
138
  #
147
139
  # * An array of flags.
@@ -149,7 +141,7 @@ module Toys
149
141
  # * The `false` value for no flags. (Default)
150
142
  # * A proc that takes a tool and returns any of the above.
151
143
  #
152
- # @param [Boolean,Array<String>,Proc] recursive_flags Specify flags
144
+ # @param recursive_flags [Boolean,Array<String>,Proc] Specify flags
153
145
  # to control recursive subtool search. The value may be any of the
154
146
  # following:
155
147
  #
@@ -158,7 +150,7 @@ module Toys
158
150
  # * The `false` value for no flags. (Default)
159
151
  # * A proc that takes a tool and returns any of the above.
160
152
  #
161
- # @param [Boolean,Array<String>,Proc] search_flags Specify flags
153
+ # @param search_flags [Boolean,Array<String>,Proc] Specify flags
162
154
  # to search subtools for a search term. The value may be any of
163
155
  # the following:
164
156
  #
@@ -167,7 +159,7 @@ module Toys
167
159
  # * The `false` value for no flags. (Default)
168
160
  # * A proc that takes a tool and returns any of the above.
169
161
  #
170
- # @param [Boolean,Array<String>,Proc] show_all_subtools_flags Specify
162
+ # @param show_all_subtools_flags [Boolean,Array<String>,Proc] Specify
171
163
  # flags to show all subtools, including hidden tools and non-runnable
172
164
  # namespaces. The value may be any of the following:
173
165
  #
@@ -176,24 +168,24 @@ module Toys
176
168
  # * The `false` value for no flags. (Default)
177
169
  # * A proc that takes a tool and returns any of the above.
178
170
  #
179
- # @param [Boolean] default_recursive Whether to search recursively for
171
+ # @param default_recursive [Boolean] Whether to search recursively for
180
172
  # subtools by default. Default is `false`.
181
- # @param [Boolean] default_show_all_subtools Whether to show all subtools
173
+ # @param default_show_all_subtools [Boolean] Whether to show all subtools
182
174
  # by default. Default is `false`.
183
- # @param [Boolean] fallback_execution Cause the tool to display its own
175
+ # @param fallback_execution [Boolean] Cause the tool to display its own
184
176
  # help text if it is not otherwise runnable. This is mostly useful
185
177
  # for namespaces, which have children are not runnable. Default is
186
178
  # `false`.
187
- # @param [Boolean] allow_root_args If the root tool includes flags for
179
+ # @param allow_root_args [Boolean] If the root tool includes flags for
188
180
  # help or usage, and doesn't otherwise use positional arguments,
189
181
  # then a tool name can be passed as arguments to display help for
190
182
  # that tool.
191
- # @param [Boolean] show_source_path Show the source path section. Default
183
+ # @param show_source_path [Boolean] Show the source path section. Default
192
184
  # is `false`.
193
- # @param [Boolean] use_less If the `less` tool is available, and the
185
+ # @param use_less [Boolean] If the `less` tool is available, and the
194
186
  # output stream is a tty, then use `less` to display help text.
195
- # @param [IO] stream Output stream to write to. Default is stdout.
196
- # @param [Boolean,nil] styled_output Cause the tool to display help text
187
+ # @param stream [IO] Output stream to write to. Default is stdout.
188
+ # @param styled_output [Boolean,nil] Cause the tool to display help text
197
189
  # with ansi styles. If `nil`, display styles if the output stream is
198
190
  # a tty. Default is `nil`.
199
191
  #
@@ -229,21 +221,21 @@ module Toys
229
221
 
230
222
  ##
231
223
  # Configure flags and default data.
224
+ # @private
232
225
  #
233
- def config(tool_definition, loader)
234
- unless tool_definition.argument_parsing_disabled?
235
- StandardMiddleware.append_common_flag_group(tool_definition)
236
- has_subtools = loader.has_subtools?(tool_definition.full_name)
237
- help_flags = add_help_flags(tool_definition)
238
- usage_flags = add_usage_flags(tool_definition)
239
- list_flags = has_subtools ? add_list_flags(tool_definition) : []
240
- if (!help_flags.empty? || !list_flags.empty? || @fallback_execution) && has_subtools
241
- add_recursive_flags(tool_definition)
242
- add_search_flags(tool_definition)
243
- add_show_all_subtools_flags(tool_definition)
244
- end
245
- if !help_flags.empty? || !usage_flags.empty? || !list_flags.empty?
246
- add_root_args(tool_definition)
226
+ def config(tool, loader)
227
+ unless tool.argument_parsing_disabled?
228
+ StandardMiddleware.append_common_flag_group(tool)
229
+ has_subtools = loader.has_subtools?(tool.full_name)
230
+ help_flags = add_help_flags(tool)
231
+ usage_flags = add_usage_flags(tool)
232
+ list_flags = has_subtools ? add_list_flags(tool) : []
233
+ can_display_help = !help_flags.empty? || !list_flags.empty? ||
234
+ !usage_flags.empty? || @fallback_execution
235
+ if can_display_help && has_subtools
236
+ add_recursive_flags(tool)
237
+ add_search_flags(tool)
238
+ add_show_all_subtools_flags(tool)
247
239
  end
248
240
  end
249
241
  yield
@@ -251,39 +243,59 @@ module Toys
251
243
 
252
244
  ##
253
245
  # Display help text if requested.
246
+ # @private
254
247
  #
255
- def run(tool) # rubocop:disable Metrics/AbcSize
256
- if tool[SHOW_USAGE_KEY]
257
- terminal.puts(get_help_text(tool).usage_string(wrap_width: terminal.width))
258
- elsif tool[SHOW_LIST_KEY]
259
- terminal.puts(get_help_text(tool).list_string(recursive: tool[RECURSIVE_SUBTOOLS_KEY],
260
- search: tool[SEARCH_STRING_KEY],
261
- include_hidden: tool[SHOW_ALL_SUBTOOLS_KEY],
262
- wrap_width: terminal.width))
263
- elsif should_show_help(tool)
264
- output_help(get_help_text(tool).help_string(recursive: tool[RECURSIVE_SUBTOOLS_KEY],
265
- search: tool[SEARCH_STRING_KEY],
266
- include_hidden: tool[SHOW_ALL_SUBTOOLS_KEY],
267
- show_source_path: @show_source_path,
268
- wrap_width: terminal.width))
248
+ def run(context)
249
+ if context[SHOW_USAGE_KEY]
250
+ show_usage(context)
251
+ elsif context[SHOW_LIST_KEY]
252
+ show_list(context)
253
+ elsif context[SHOW_HELP_KEY]
254
+ show_help(context, true)
269
255
  else
270
- yield
256
+ begin
257
+ yield
258
+ rescue NotRunnableError => e
259
+ raise e unless @fallback_execution
260
+ show_help(context, false)
261
+ end
271
262
  end
272
263
  end
273
264
 
274
265
  private
275
266
 
276
267
  def terminal
268
+ require "toys/utils/terminal"
277
269
  @terminal ||= Utils::Terminal.new(output: @stream, styled: @styled_output)
278
270
  end
279
271
 
280
- def should_show_help(tool)
281
- @fallback_execution && !tool[Tool::Keys::TOOL_DEFINITION].runnable? ||
282
- tool[SHOW_HELP_KEY]
272
+ def show_usage(context)
273
+ help_text = get_help_text(context, true)
274
+ str = help_text.usage_string(
275
+ recursive: context[RECURSIVE_SUBTOOLS_KEY],
276
+ include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], wrap_width: terminal.width
277
+ )
278
+ terminal.puts(str)
283
279
  end
284
280
 
285
- def output_help(str)
281
+ def show_list(context)
282
+ help_text = get_help_text(context, true)
283
+ str = help_text.list_string(
284
+ recursive: context[RECURSIVE_SUBTOOLS_KEY], search: context[SEARCH_STRING_KEY],
285
+ include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], wrap_width: terminal.width
286
+ )
287
+ terminal.puts(str)
288
+ end
289
+
290
+ def show_help(context, use_extra_args)
291
+ help_text = get_help_text(context, use_extra_args)
292
+ str = help_text.help_string(
293
+ recursive: context[RECURSIVE_SUBTOOLS_KEY], search: context[SEARCH_STRING_KEY],
294
+ include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], show_source_path: @show_source_path,
295
+ wrap_width: terminal.width
296
+ )
286
297
  if less_path
298
+ require "toys/utils/exec"
287
299
  Utils::Exec.new.exec([less_path, "-R"], in: [:string, str])
288
300
  else
289
301
  terminal.puts(str)
@@ -301,27 +313,41 @@ module Toys
301
313
  @less_path
302
314
  end
303
315
 
304
- def get_help_text(tool)
305
- tool_name = tool[TOOL_NAME_KEY]
306
- return Utils::HelpText.from_tool(tool) if tool_name.nil? || tool_name.empty?
307
- loader = tool[Tool::Keys::LOADER]
308
- tool_definition, rest = loader.lookup(tool_name)
309
- help_text = Utils::HelpText.new(tool_definition, loader, tool[Tool::Keys::BINARY_NAME])
310
- report_usage_error(tool_name, help_text) unless rest.empty?
311
- help_text
316
+ def get_help_text(context, use_extra_args)
317
+ require "toys/utils/help_text"
318
+ if use_extra_args && @allow_root_args && context[Context::Key::TOOL].root?
319
+ tool_name = Array(context[Context::Key::UNMATCHED_POSITIONAL])
320
+ unless tool_name.empty?
321
+ cli = context[Context::Key::CLI]
322
+ loader = cli.loader
323
+ tool, rest = loader.lookup(tool_name)
324
+ help_text = Utils::HelpText.new(tool, loader, cli.executable_name)
325
+ report_usage_error(help_text, loader, tool.full_name, rest.first) unless rest.empty?
326
+ return help_text
327
+ end
328
+ end
329
+ Utils::HelpText.from_context(context)
312
330
  end
313
331
 
314
- def report_usage_error(tool_name, help_text)
315
- terminal.puts("Tool not found: #{tool_name.join(' ')}", :bright_red, :bold)
332
+ def report_usage_error(help_text, loader, tool_name, next_word)
333
+ dict = loader.list_subtools(tool_name).map(&:simple_name)
334
+ suggestions = Compat.suggestions(next_word, dict)
335
+ tool_name = (tool_name + [next_word]).join(" ")
336
+ message = "Tool not found: \"#{tool_name}\"."
337
+ unless suggestions.empty?
338
+ suggestions_str = suggestions.join("\n ")
339
+ message = "#{message}\nDid you mean... #{suggestions_str}"
340
+ end
341
+ terminal.puts(message, :bright_red, :bold)
316
342
  terminal.puts
317
343
  terminal.puts help_text.usage_string(wrap_width: terminal.width)
318
- Tool.exit(1)
344
+ Context.exit(1)
319
345
  end
320
346
 
321
- def add_help_flags(tool_definition)
322
- flags = resolve_flags_spec(@help_flags, tool_definition, DEFAULT_HELP_FLAGS)
347
+ def add_help_flags(tool)
348
+ flags = resolve_flags_spec(@help_flags, tool, DEFAULT_HELP_FLAGS)
323
349
  unless flags.empty?
324
- tool_definition.add_flag(
350
+ tool.add_flag(
325
351
  SHOW_HELP_KEY, flags,
326
352
  report_collisions: false,
327
353
  desc: "Display help for this tool",
@@ -331,10 +357,10 @@ module Toys
331
357
  flags
332
358
  end
333
359
 
334
- def add_usage_flags(tool_definition)
335
- flags = resolve_flags_spec(@usage_flags, tool_definition, DEFAULT_USAGE_FLAGS)
360
+ def add_usage_flags(tool)
361
+ flags = resolve_flags_spec(@usage_flags, tool, DEFAULT_USAGE_FLAGS)
336
362
  unless flags.empty?
337
- tool_definition.add_flag(
363
+ tool.add_flag(
338
364
  SHOW_USAGE_KEY, flags,
339
365
  report_collisions: false,
340
366
  desc: "Display a brief usage string for this tool",
@@ -344,10 +370,10 @@ module Toys
344
370
  flags
345
371
  end
346
372
 
347
- def add_list_flags(tool_definition)
348
- flags = resolve_flags_spec(@list_flags, tool_definition, DEFAULT_LIST_FLAGS)
373
+ def add_list_flags(tool)
374
+ flags = resolve_flags_spec(@list_flags, tool, DEFAULT_LIST_FLAGS)
349
375
  unless flags.empty?
350
- tool_definition.add_flag(
376
+ tool.add_flag(
351
377
  SHOW_LIST_KEY, flags,
352
378
  report_collisions: false,
353
379
  desc: "List the subtools under this tool",
@@ -357,12 +383,12 @@ module Toys
357
383
  flags
358
384
  end
359
385
 
360
- def add_recursive_flags(tool_definition)
361
- flags = resolve_flags_spec(@recursive_flags, tool_definition, DEFAULT_RECURSIVE_FLAGS)
386
+ def add_recursive_flags(tool)
387
+ flags = resolve_flags_spec(@recursive_flags, tool, DEFAULT_RECURSIVE_FLAGS)
362
388
  if flags.empty?
363
- tool_definition.default_data[RECURSIVE_SUBTOOLS_KEY] = @default_recursive
389
+ tool.default_data[RECURSIVE_SUBTOOLS_KEY] = @default_recursive
364
390
  else
365
- tool_definition.add_flag(
391
+ tool.add_flag(
366
392
  RECURSIVE_SUBTOOLS_KEY, flags,
367
393
  report_collisions: false, default: @default_recursive,
368
394
  desc: "List all subtools recursively when displaying help" \
@@ -373,10 +399,10 @@ module Toys
373
399
  flags
374
400
  end
375
401
 
376
- def add_search_flags(tool_definition)
377
- flags = resolve_flags_spec(@search_flags, tool_definition, DEFAULT_SEARCH_FLAGS)
402
+ def add_search_flags(tool)
403
+ flags = resolve_flags_spec(@search_flags, tool, DEFAULT_SEARCH_FLAGS)
378
404
  unless flags.empty?
379
- tool_definition.add_flag(
405
+ tool.add_flag(
380
406
  SEARCH_STRING_KEY, flags,
381
407
  report_collisions: false,
382
408
  desc: "Search subtools for the given regular expression when displaying help",
@@ -386,13 +412,12 @@ module Toys
386
412
  flags
387
413
  end
388
414
 
389
- def add_show_all_subtools_flags(tool_definition)
390
- flags = resolve_flags_spec(@show_all_subtools_flags, tool_definition,
391
- DEFAULT_SHOW_ALL_SUBTOOLS_FLAGS)
415
+ def add_show_all_subtools_flags(tool)
416
+ flags = resolve_flags_spec(@show_all_subtools_flags, tool, DEFAULT_SHOW_ALL_SUBTOOLS_FLAGS)
392
417
  if flags.empty?
393
- tool_definition.default_data[SHOW_ALL_SUBTOOLS_KEY] = @default_show_all_subtools
418
+ tool.default_data[SHOW_ALL_SUBTOOLS_KEY] = @default_show_all_subtools
394
419
  else
395
- tool_definition.add_flag(
420
+ tool.add_flag(
396
421
  SHOW_ALL_SUBTOOLS_KEY, flags,
397
422
  report_collisions: false, default: @default_show_all_subtools,
398
423
  desc: "List all subtools including hidden subtools and namespaces" \
@@ -403,16 +428,8 @@ module Toys
403
428
  flags
404
429
  end
405
430
 
406
- def add_root_args(tool_definition)
407
- if @allow_root_args && tool_definition.root? && tool_definition.arg_definitions.empty?
408
- tool_definition.set_remaining_args(TOOL_NAME_KEY,
409
- display_name: "TOOL_NAME",
410
- desc: "The tool for which to display help")
411
- end
412
- end
413
-
414
- def resolve_flags_spec(flags, tool_definition, defaults)
415
- flags = flags.call(tool_definition) if flags.respond_to?(:call)
431
+ def resolve_flags_spec(flags, tool, defaults)
432
+ flags = flags.call(tool) if flags.respond_to?(:call)
416
433
  case flags
417
434
  when true, :default
418
435
  Array(defaults)
@@ -1,32 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2018 Daniel Azuma
3
+ # Copyright 2019 Daniel Azuma
4
4
  #
5
- # All rights reserved.
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
6
11
  #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
9
14
  #
10
- # * Redistributions of source code must retain the above copyright notice,
11
- # this list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the copyright holder, nor the names of any other
16
- # contributors to this software, may be used to endorse or promote products
17
- # derived from this software without specific prior written permission.
18
- #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
- # POSSIBILITY OF SUCH DAMAGE.
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ # IN THE SOFTWARE.
30
22
  ;
31
23
 
32
24
  module Toys
@@ -59,11 +51,11 @@ module Toys
59
51
  ##
60
52
  # Create a ShowVersion middleware
61
53
  #
62
- # @param [String] version_string The string that should be displayed.
63
- # @param [Array<String>] version_flags A list of flags that should
54
+ # @param version_string [String] The string that should be displayed.
55
+ # @param version_flags [Array<String>] A list of flags that should
64
56
  # trigger displaying the version. Default is
65
57
  # {DEFAULT_VERSION_FLAGS}.
66
- # @param [IO] stream Output stream to write to. Default is stdout.
58
+ # @param stream [IO] Output stream to write to. Default is stdout.
67
59
  #
68
60
  def initialize(version_string: nil,
69
61
  version_flags: DEFAULT_VERSION_FLAGS,
@@ -72,26 +64,28 @@ module Toys
72
64
  @version_string = version_string
73
65
  @version_flags = version_flags
74
66
  @version_flag_desc = version_flag_desc
75
- @terminal = Utils::Terminal.new(output: stream)
67
+ @output = stream
76
68
  end
77
69
 
78
70
  ##
79
71
  # Adds the version flag if requested.
72
+ # @private
80
73
  #
81
- def config(tool_definition, _loader)
82
- if @version_string && tool_definition.root?
83
- tool_definition.add_flag(SHOW_VERSION_KEY, @version_flags,
84
- report_collisions: false, desc: @version_flag_desc)
74
+ def config(tool, _loader)
75
+ if @version_string && tool.root?
76
+ tool.add_flag(SHOW_VERSION_KEY, @version_flags,
77
+ report_collisions: false, desc: @version_flag_desc)
85
78
  end
86
79
  yield
87
80
  end
88
81
 
89
82
  ##
90
83
  # This middleware displays the version.
84
+ # @private
91
85
  #
92
- def run(tool)
93
- if tool[SHOW_VERSION_KEY]
94
- @terminal.puts(@version_string)
86
+ def run(context)
87
+ if context[SHOW_VERSION_KEY]
88
+ @output.puts(@version_string)
95
89
  else
96
90
  yield
97
91
  end