toys-core 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +4 -1
  5. data/docs/guide.md +1 -1
  6. data/lib/toys/acceptor.rb +10 -1
  7. data/lib/toys/arg_parser.rb +1 -0
  8. data/lib/toys/cli.rb +127 -107
  9. data/lib/toys/compat.rb +54 -3
  10. data/lib/toys/completion.rb +15 -5
  11. data/lib/toys/context.rb +22 -20
  12. data/lib/toys/core.rb +6 -2
  13. data/lib/toys/dsl/base.rb +2 -0
  14. data/lib/toys/dsl/flag.rb +23 -17
  15. data/lib/toys/dsl/flag_group.rb +11 -7
  16. data/lib/toys/dsl/positional_arg.rb +23 -13
  17. data/lib/toys/dsl/tool.rb +10 -6
  18. data/lib/toys/errors.rb +63 -8
  19. data/lib/toys/flag.rb +660 -651
  20. data/lib/toys/flag_group.rb +19 -6
  21. data/lib/toys/input_file.rb +9 -3
  22. data/lib/toys/loader.rb +129 -115
  23. data/lib/toys/middleware.rb +45 -21
  24. data/lib/toys/mixin.rb +8 -6
  25. data/lib/toys/positional_arg.rb +18 -17
  26. data/lib/toys/settings.rb +81 -67
  27. data/lib/toys/source_info.rb +33 -24
  28. data/lib/toys/standard_middleware/add_verbosity_flags.rb +2 -0
  29. data/lib/toys/standard_middleware/apply_config.rb +1 -0
  30. data/lib/toys/standard_middleware/handle_usage_errors.rb +1 -0
  31. data/lib/toys/standard_middleware/set_default_descriptions.rb +1 -0
  32. data/lib/toys/standard_middleware/show_help.rb +2 -0
  33. data/lib/toys/standard_middleware/show_root_version.rb +2 -0
  34. data/lib/toys/standard_mixins/bundler.rb +22 -14
  35. data/lib/toys/standard_mixins/exec.rb +31 -20
  36. data/lib/toys/standard_mixins/fileutils.rb +3 -1
  37. data/lib/toys/standard_mixins/gems.rb +21 -17
  38. data/lib/toys/standard_mixins/git_cache.rb +5 -7
  39. data/lib/toys/standard_mixins/highline.rb +8 -8
  40. data/lib/toys/standard_mixins/terminal.rb +5 -5
  41. data/lib/toys/standard_mixins/xdg.rb +5 -5
  42. data/lib/toys/template.rb +9 -7
  43. data/lib/toys/tool_definition.rb +209 -202
  44. data/lib/toys/utils/completion_engine.rb +7 -2
  45. data/lib/toys/utils/exec.rb +158 -127
  46. data/lib/toys/utils/gems.rb +81 -57
  47. data/lib/toys/utils/git_cache.rb +674 -45
  48. data/lib/toys/utils/help_text.rb +27 -3
  49. data/lib/toys/utils/terminal.rb +10 -2
  50. data/lib/toys/wrappable_string.rb +9 -2
  51. data/lib/toys-core.rb +14 -5
  52. metadata +4 -4
data/lib/toys/dsl/flag.rb CHANGED
@@ -22,22 +22,6 @@ module Toys
22
22
  # end
23
23
  #
24
24
  class Flag
25
- ## @private
26
- def initialize(flags, acceptor, default, handler, flag_completion, value_completion,
27
- report_collisions, group, desc, long_desc, display_name)
28
- @flags = flags
29
- @default = default
30
- @handler = handler
31
- @report_collisions = report_collisions
32
- @group = group
33
- @desc = desc
34
- @long_desc = long_desc || []
35
- @display_name = display_name
36
- accept(acceptor)
37
- complete_flags(flag_completion, **{})
38
- complete_values(value_completion, **{})
39
- end
40
-
41
25
  ##
42
26
  # Add flags in OptionParser format. This may be called multiple times,
43
27
  # and the results are cumulative.
@@ -284,7 +268,29 @@ module Toys
284
268
  self
285
269
  end
286
270
 
287
- ## @private
271
+ ##
272
+ # Called only from DSL::Tool
273
+ #
274
+ # @private
275
+ #
276
+ def initialize(flags, acceptor, default, handler, flag_completion, value_completion,
277
+ report_collisions, group, desc, long_desc, display_name)
278
+ @flags = flags
279
+ @default = default
280
+ @handler = handler
281
+ @report_collisions = report_collisions
282
+ @group = group
283
+ @desc = desc
284
+ @long_desc = long_desc || []
285
+ @display_name = display_name
286
+ accept(acceptor)
287
+ complete_flags(flag_completion, **{})
288
+ complete_values(value_completion, **{})
289
+ end
290
+
291
+ ##
292
+ # @private
293
+ #
288
294
  def _add_to(tool, key)
289
295
  tool.add_flag(key, @flags,
290
296
  accept: @acceptor, default: @default, handler: @handler,
@@ -22,13 +22,6 @@ module Toys
22
22
  # end
23
23
  #
24
24
  class FlagGroup
25
- ## @private
26
- def initialize(tool_dsl, tool, flag_group)
27
- @tool_dsl = tool_dsl
28
- @tool = tool
29
- @flag_group = flag_group
30
- end
31
-
32
25
  ##
33
26
  # Add a flag to the current group. Each flag must specify a key which
34
27
  # the script may use to obtain the flag value from the context.
@@ -268,6 +261,17 @@ module Toys
268
261
  @flag_group.append_long_desc(long_desc)
269
262
  self
270
263
  end
264
+
265
+ ##
266
+ # Called only from DSL::Tool.
267
+ #
268
+ # @private
269
+ #
270
+ def initialize(tool_dsl, tool, flag_group)
271
+ @tool_dsl = tool_dsl
272
+ @tool = tool
273
+ @flag_group = flag_group
274
+ end
271
275
  end
272
276
  end
273
277
  end
@@ -22,16 +22,6 @@ module Toys
22
22
  # end
23
23
  #
24
24
  class PositionalArg
25
- ## @private
26
- def initialize(acceptor, default, completion, display_name, desc, long_desc)
27
- @default = default
28
- @display_name = display_name
29
- @desc = desc
30
- @long_desc = long_desc || []
31
- accept(acceptor, **{})
32
- complete(completion, **{})
33
- end
34
-
35
25
  ##
36
26
  # Set the acceptor for this argument's values.
37
27
  # You can pass either the string name of an acceptor defined in this tool
@@ -151,21 +141,41 @@ module Toys
151
141
  self
152
142
  end
153
143
 
154
- ## @private
144
+ ##
145
+ # Called only from DSL::Tool
146
+ #
147
+ # @private
148
+ #
149
+ def initialize(acceptor, default, completion, display_name, desc, long_desc)
150
+ @default = default
151
+ @display_name = display_name
152
+ @desc = desc
153
+ @long_desc = long_desc || []
154
+ accept(acceptor, **{})
155
+ complete(completion, **{})
156
+ end
157
+
158
+ ##
159
+ # @private
160
+ #
155
161
  def _add_required_to(tool, key)
156
162
  tool.add_required_arg(key,
157
163
  accept: @acceptor, complete: @completion,
158
164
  display_name: @display_name, desc: @desc, long_desc: @long_desc)
159
165
  end
160
166
 
161
- ## @private
167
+ ##
168
+ # @private
169
+ #
162
170
  def _add_optional_to(tool, key)
163
171
  tool.add_optional_arg(key,
164
172
  accept: @acceptor, default: @default, complete: @completion,
165
173
  display_name: @display_name, desc: @desc, long_desc: @long_desc)
166
174
  end
167
175
 
168
- ## @private
176
+ ##
177
+ # @private
178
+ #
169
179
  def _set_remaining_on(tool, key)
170
180
  tool.set_remaining_args(key,
171
181
  accept: @acceptor, default: @default, complete: @completion,
data/lib/toys/dsl/tool.rb CHANGED
@@ -34,12 +34,6 @@ module Toys
34
34
  # toys greet rubyists
35
35
  #
36
36
  module Tool
37
- ## @private
38
- def method_added(_meth)
39
- super
40
- DSL::Internal.current_tool(self, true)&.check_definition_state(is_method: true)
41
- end
42
-
43
37
  ##
44
38
  # Create a named acceptor that can be referenced by name from any flag or
45
39
  # positional argument in this tool or its subtools.
@@ -1717,6 +1711,16 @@ module Toys
1717
1711
  end
1718
1712
  self
1719
1713
  end
1714
+
1715
+ ##
1716
+ # Notify the tool definition when a method is defined in this tool class.
1717
+ #
1718
+ # @private
1719
+ #
1720
+ def method_added(_meth)
1721
+ super
1722
+ DSL::Internal.current_tool(self, true)&.check_definition_state(is_method: true)
1723
+ end
1720
1724
  end
1721
1725
  end
1722
1726
  end
data/lib/toys/errors.rb CHANGED
@@ -40,10 +40,13 @@ module Toys
40
40
  end
41
41
 
42
42
  ##
43
- # A wrapper exception used to provide user-oriented context for an exception
43
+ # A wrapper exception used to provide user-oriented context for an error
44
+ # thrown during tool execution.
44
45
  #
45
46
  class ContextualError < ::StandardError
46
- ## @private
47
+ ##
48
+ # @private
49
+ #
47
50
  def initialize(cause, banner,
48
51
  config_path: nil, config_line: nil,
49
52
  tool_name: nil, tool_args: nil)
@@ -57,16 +60,66 @@ module Toys
57
60
  set_backtrace(cause.backtrace)
58
61
  end
59
62
 
63
+ ##
64
+ # The underlying exception
65
+ # @return [::StandardError]
66
+ #
60
67
  attr_reader :cause
68
+
69
+ ##
70
+ # An overall banner message
71
+ # @return [String]
72
+ #
61
73
  attr_reader :banner
62
74
 
63
- attr_accessor :config_path
64
- attr_accessor :config_line
65
- attr_accessor :tool_name
66
- attr_accessor :tool_args
75
+ ##
76
+ # The path to the toys config file in which the error was detected
77
+ # @return [String]
78
+ #
79
+ attr_reader :config_path
80
+
81
+ ##
82
+ # The line number in the toys config file in which the error was detected
83
+ # @return [Integer]
84
+ #
85
+ attr_reader :config_line
86
+
87
+ ##
88
+ # The full name of the tool that was running when the error occurred
89
+ # @return [Array<String>]
90
+ #
91
+ attr_reader :tool_name
92
+
93
+ ##
94
+ # The arguments passed to the tool that was running when the error occurred
95
+ # @return [Array<String>]
96
+ #
97
+ attr_reader :tool_args
98
+
99
+ ##
100
+ # @private
101
+ #
102
+ attr_writer :config_path
103
+
104
+ ##
105
+ # @private
106
+ #
107
+ attr_writer :config_line
108
+
109
+ ##
110
+ # @private
111
+ #
112
+ attr_writer :tool_name
113
+
114
+ ##
115
+ # @private
116
+ #
117
+ attr_writer :tool_args
67
118
 
68
119
  class << self
69
- ## @private
120
+ ##
121
+ # @private
122
+ #
70
123
  def capture_path(banner, path, **opts)
71
124
  yield
72
125
  rescue ContextualError => e
@@ -86,7 +139,9 @@ module Toys
86
139
  raise e
87
140
  end
88
141
 
89
- ## @private
142
+ ##
143
+ # @private
144
+ #
90
145
  def capture(banner, **opts)
91
146
  yield
92
147
  rescue ContextualError => e