toys 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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -0
  3. data/CHANGELOG.md +35 -0
  4. data/LICENSE.md +1 -1
  5. data/README.md +7 -4
  6. data/builtins/system/git-cache.rb +238 -0
  7. data/builtins/system/test.rb +37 -2
  8. data/core-docs/toys/acceptor.rb +432 -0
  9. data/core-docs/toys/arg_parser.rb +397 -0
  10. data/core-docs/toys/cli.rb +493 -0
  11. data/core-docs/toys/compat.rb +2 -0
  12. data/core-docs/toys/completion.rb +329 -0
  13. data/core-docs/toys/context.rb +321 -0
  14. data/core-docs/toys/core.rb +14 -0
  15. data/core-docs/toys/dsl/base.rb +56 -0
  16. data/core-docs/toys/dsl/flag.rb +261 -0
  17. data/core-docs/toys/dsl/flag_group.rb +259 -0
  18. data/core-docs/toys/dsl/internal.rb +4 -0
  19. data/core-docs/toys/dsl/positional_arg.rb +139 -0
  20. data/core-docs/toys/dsl/tool.rb +1530 -0
  21. data/core-docs/toys/errors.rb +93 -0
  22. data/core-docs/toys/flag.rb +549 -0
  23. data/core-docs/toys/flag_group.rb +186 -0
  24. data/core-docs/toys/input_file.rb +8 -0
  25. data/core-docs/toys/loader.rb +222 -0
  26. data/core-docs/toys/middleware.rb +295 -0
  27. data/core-docs/toys/mixin.rb +142 -0
  28. data/core-docs/toys/module_lookup.rb +75 -0
  29. data/core-docs/toys/positional_arg.rb +145 -0
  30. data/core-docs/toys/settings.rb +507 -0
  31. data/core-docs/toys/source_info.rb +127 -0
  32. data/core-docs/toys/standard_middleware/add_verbosity_flags.rb +49 -0
  33. data/core-docs/toys/standard_middleware/apply_config.rb +24 -0
  34. data/core-docs/toys/standard_middleware/handle_usage_errors.rb +33 -0
  35. data/core-docs/toys/standard_middleware/set_default_descriptions.rb +222 -0
  36. data/core-docs/toys/standard_middleware/show_help.rb +190 -0
  37. data/core-docs/toys/standard_middleware/show_root_version.rb +45 -0
  38. data/core-docs/toys/standard_mixins/bundler.rb +83 -0
  39. data/core-docs/toys/standard_mixins/exec.rb +645 -0
  40. data/core-docs/toys/standard_mixins/fileutils.rb +18 -0
  41. data/core-docs/toys/standard_mixins/gems.rb +48 -0
  42. data/core-docs/toys/standard_mixins/git_cache.rb +41 -0
  43. data/core-docs/toys/standard_mixins/highline.rb +133 -0
  44. data/core-docs/toys/standard_mixins/terminal.rb +135 -0
  45. data/core-docs/toys/standard_mixins/xdg.rb +49 -0
  46. data/core-docs/toys/template.rb +112 -0
  47. data/core-docs/toys/tool_definition.rb +926 -0
  48. data/core-docs/toys/utils/completion_engine.rb +49 -0
  49. data/core-docs/toys/utils/exec.rb +721 -0
  50. data/core-docs/toys/utils/gems.rb +185 -0
  51. data/core-docs/toys/utils/git_cache.rb +353 -0
  52. data/core-docs/toys/utils/help_text.rb +134 -0
  53. data/core-docs/toys/utils/terminal.rb +310 -0
  54. data/core-docs/toys/utils/xdg.rb +253 -0
  55. data/core-docs/toys/wrappable_string.rb +120 -0
  56. data/core-docs/toys-core.rb +63 -0
  57. data/docs/guide.md +497 -156
  58. data/lib/toys/standard_cli.rb +50 -36
  59. data/lib/toys/templates/clean.rb +18 -0
  60. data/lib/toys/templates/gem_build.rb +24 -0
  61. data/lib/toys/templates/minitest.rb +21 -0
  62. data/lib/toys/templates/rake.rb +23 -3
  63. data/lib/toys/templates/rdoc.rb +29 -0
  64. data/lib/toys/templates/rspec.rb +32 -4
  65. data/lib/toys/templates/rubocop.rb +14 -1
  66. data/lib/toys/templates/yardoc.rb +55 -0
  67. data/lib/toys/testing.rb +186 -109
  68. data/lib/toys/version.rb +1 -1
  69. data/lib/toys.rb +4 -2
  70. metadata +56 -6
@@ -0,0 +1,432 @@
1
+ module Toys
2
+ ##
3
+ # **_Defined in the toys-core gem_**
4
+ #
5
+ # An Acceptor validates and converts arguments. It is designed to be
6
+ # compatible with the OptionParser accept mechanism.
7
+ #
8
+ # First, an acceptor validates the argument via its
9
+ # {Toys::Acceptor::Base#match} method. This method should determine whether
10
+ # the argument is valid, and return information that will help with
11
+ # conversion of the argument.
12
+ #
13
+ # Second, an acceptor converts the argument to its final form via the
14
+ # {Toys::Acceptor::Base#convert} method.
15
+ #
16
+ # Finally, an acceptor has a name that may appear in help text for flags and
17
+ # arguments that use it.
18
+ #
19
+ module Acceptor
20
+ ##
21
+ # A sentinel that may be returned from a function-based acceptor to
22
+ # indicate invalid input.
23
+ # @return [Object]
24
+ #
25
+ REJECT = ::Object.new.freeze
26
+
27
+ ##
28
+ # The default type description.
29
+ # @return [String]
30
+ #
31
+ DEFAULT_TYPE_DESC = "string"
32
+
33
+ ##
34
+ # **_Defined in the toys-core gem_**
35
+ #
36
+ # A base class for acceptors.
37
+ #
38
+ # The base acceptor does not do any validation (i.e. it accepts all
39
+ # arguments) or conversion (i.e. it returns the original string). You can
40
+ # subclass this base class and override the {#match} and {#convert} methods
41
+ # to implement an acceptor.
42
+ #
43
+ class Base
44
+ ##
45
+ # Create a base acceptor.
46
+ #
47
+ # @param type_desc [String] Type description string, shown in help.
48
+ # Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
49
+ # @param well_known_spec [Object] The well-known acceptor spec associated
50
+ # with this acceptor, or `nil` for none.
51
+ #
52
+ def initialize(type_desc: nil, well_known_spec: nil)
53
+ # Source available in the toys-core gem
54
+ end
55
+
56
+ ##
57
+ # Type description string, shown in help.
58
+ # @return [String]
59
+ #
60
+ attr_reader :type_desc
61
+
62
+ ##
63
+ # The well-known acceptor spec associated with this acceptor, if any.
64
+ # This generally identifies an OptionParser-compatible acceptor spec. For
65
+ # example, the acceptor object that corresponds to `Integer` will return
66
+ # `Integer` from this attribute.
67
+ #
68
+ # @return [Object] the well-known acceptor
69
+ # @return [nil] if there is no corresponding well-known acceptor
70
+ #
71
+ attr_reader :well_known_spec
72
+
73
+ ##
74
+ # Type description string, shown in help.
75
+ # @return [String]
76
+ #
77
+ def to_s
78
+ # Source available in the toys-core gem
79
+ end
80
+
81
+ ##
82
+ # Validate the given input.
83
+ #
84
+ # When given a valid input, return an array in which the first element is
85
+ # the original input string, and the remaining elements (which may be
86
+ # empty) comprise any additional information that may be useful during
87
+ # conversion. If there is no additional information, you may return the
88
+ # original input string by itself without wrapping in an array.
89
+ #
90
+ # When given an invalid input, return a falsy value such as `nil`.
91
+ #
92
+ # Note that a `MatchData` object is a legitimate return value because it
93
+ # duck-types the appropriate array.
94
+ #
95
+ # This default implementation simply returns the original input string,
96
+ # as the only array element, indicating all inputs are valid. You can
97
+ # override this method to provide a different validation function.
98
+ #
99
+ # @param str [String,nil] The input argument string. May be `nil` if the
100
+ # value is optional and not provided.
101
+ # @return [String,Array,nil]
102
+ #
103
+ def match(str)
104
+ # Source available in the toys-core gem
105
+ end
106
+
107
+ ##
108
+ # Convert the given input.
109
+ #
110
+ # This method is passed the results of a successful match, including the
111
+ # original input string and any other values returned from {#match}. It
112
+ # must return the final converted value to use.
113
+ #
114
+ # @param str [String,nil] Original argument string. May be `nil` if the
115
+ # value is optional and not provided.
116
+ # @param extra [Object...] Zero or more additional arguments comprising
117
+ # additional elements returned from the match function.
118
+ # @return [Object] The converted argument as it should be stored in the
119
+ # context data.
120
+ #
121
+ def convert(str, *extra)
122
+ # Source available in the toys-core gem
123
+ end
124
+
125
+ ##
126
+ # Return suggestions for a given non-matching string.
127
+ #
128
+ # This method may be called when a match fails. It should return a
129
+ # (possibly empty) array of suggestions that could be displayed to the
130
+ # user as "did you mean..."
131
+ #
132
+ # The default implementation returns the empty list.
133
+ #
134
+ # @param str [String] A string that failed matching.
135
+ # @return [Array<String>] A possibly empty array of alternative
136
+ # suggestions that could be displayed with "did you mean..."
137
+ #
138
+ def suggestions(str)
139
+ # Source available in the toys-core gem
140
+ end
141
+ end
142
+
143
+ ##
144
+ # The default acceptor. Corresponds to the well-known acceptor for
145
+ # `Object`.
146
+ # @return [Toys::Acceptor::Base]
147
+ #
148
+ DEFAULT = Base.new(type_desc: "string", well_known_spec: ::Object)
149
+
150
+ ##
151
+ # **_Defined in the toys-core gem_**
152
+ #
153
+ # An acceptor that uses a simple function to validate and convert input.
154
+ # The function must take the input string as its argument, and either
155
+ # return the converted object to indicate success, or raise an exception or
156
+ # return the sentinel {Toys::Acceptor::REJECT} to indicate invalid input.
157
+ #
158
+ class Simple < Base
159
+ ##
160
+ # Create a simple acceptor.
161
+ #
162
+ # You should provide an acceptor function, either as a proc in the
163
+ # `function` argument, or as a block. The function must take as its one
164
+ # argument the input string. If the string is valid, the function must
165
+ # return the value to store in the tool's data. If the string is invalid,
166
+ # the function may either raise an exception (which must descend from
167
+ # `StandardError`) or return {Toys::Acceptor::REJECT}.
168
+ #
169
+ # @param function [Proc] The acceptor function
170
+ # @param type_desc [String] Type description string, shown in help.
171
+ # Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
172
+ # @param well_known_spec [Object] The well-known acceptor spec associated
173
+ # with this acceptor, or `nil` for none.
174
+ # @param block [Proc] The acceptor function, if not provided as a normal
175
+ # parameter.
176
+ #
177
+ def initialize(function = nil, type_desc: nil, well_known_spec: nil, &block)
178
+ # Source available in the toys-core gem
179
+ end
180
+ end
181
+
182
+ ##
183
+ # **_Defined in the toys-core gem_**
184
+ #
185
+ # An acceptor that uses a regex to validate input. It also supports a
186
+ # custom conversion function that generates the final value from the match
187
+ # results.
188
+ #
189
+ class Pattern < Base
190
+ ##
191
+ # Create a pattern acceptor.
192
+ #
193
+ # You must provide a regular expression (or any object that duck-types
194
+ # `Regexp#match`) as a validator.
195
+ #
196
+ # You may also optionally provide a converter, either as a proc or a
197
+ # block. A converter must take as its arguments the values in the
198
+ # `MatchData` returned from a successful regex match. That is, the first
199
+ # argument is the original input string, and the remaining arguments are
200
+ # the captures. The converter must return the final converted value.
201
+ # If no converter is provided, no conversion is done and the input string
202
+ # is returned.
203
+ #
204
+ # @param regex [Regexp] Regular expression defining value values.
205
+ # @param converter [Proc] An optional converter function. May also be
206
+ # given as a block. Note that the converter will be passed all
207
+ # elements of the `MatchData`.
208
+ # @param type_desc [String] Type description string, shown in help.
209
+ # Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
210
+ # @param well_known_spec [Object] The well-known acceptor spec associated
211
+ # with this acceptor, or `nil` for none.
212
+ # @param block [Proc] A converter function, if not provided as a normal
213
+ # parameter.
214
+ #
215
+ def initialize(regex, converter = nil, type_desc: nil, well_known_spec: nil, &block)
216
+ # Source available in the toys-core gem
217
+ end
218
+ end
219
+
220
+ ##
221
+ # **_Defined in the toys-core gem_**
222
+ #
223
+ # An acceptor that recognizes a fixed set of values.
224
+ #
225
+ # You provide a list of valid values. The input argument string will be
226
+ # matched against the string forms of these valid values. If it matches,
227
+ # the converter will return the actual value from the valid list.
228
+ #
229
+ # For example, you could pass `[:one, :two, 3]` as the set of values. If
230
+ # an argument of `"two"` is passed in, the converter will yield a final
231
+ # value of the symbol `:two`. If an argument of "3" is passed in, the
232
+ # converter will yield the integer `3`. If an argument of "three" is
233
+ # passed in, the match will fail.
234
+ #
235
+ class Enum < Base
236
+ ##
237
+ # Create an acceptor.
238
+ #
239
+ # @param values [Array<Object>] Valid values.
240
+ # @param type_desc [String] Type description string, shown in help.
241
+ # Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
242
+ # @param well_known_spec [Object] The well-known acceptor spec associated
243
+ # with this acceptor, or `nil` for none.
244
+ #
245
+ def initialize(values, type_desc: nil, well_known_spec: nil)
246
+ # Source available in the toys-core gem
247
+ end
248
+
249
+ ##
250
+ # The array of enum values.
251
+ # @return [Array<Object>]
252
+ #
253
+ attr_reader :values
254
+ end
255
+
256
+ ##
257
+ # **_Defined in the toys-core gem_**
258
+ #
259
+ # An acceptor that recognizes a range of values.
260
+ #
261
+ # The input argument is matched against the given range. For example, you
262
+ # can match against the integers from 1 to 10 by passing the range
263
+ # `(1..10)`.
264
+ #
265
+ # You can also provide a conversion function that takes the input string
266
+ # and converts it an object that can be compared by the range. If you do
267
+ # not provide a converter, a default converter will be provided depending
268
+ # on the types of objects serving as the range limits. Specifically:
269
+ #
270
+ # * If the range beginning and end are both `Integer`, then input strings
271
+ # are likewise converted to `Integer` when matched against the range.
272
+ # Accepted values are returned as integers.
273
+ # * If the range beginning and end are both `Float`, then input strings
274
+ # are likewise converted to `Float`.
275
+ # * If the range beginning and end are both `Rational`, then input
276
+ # strings are likewise converted to `Rational`.
277
+ # * If the range beginning and end are both `Numeric` types but different
278
+ # subtypes (e.g. an `Integer` and a `Float`), then any type of numeric
279
+ # input (integer, float, rational) is accepted and matched against the
280
+ # range.
281
+ # * If the range beginning and/or end are not numeric types, then no
282
+ # conversion is done by default.
283
+ #
284
+ class Range < Simple
285
+ ##
286
+ # Create an acceptor.
287
+ #
288
+ # @param range [Range] The range of acceptable values
289
+ # @param converter [Proc] A converter proc that takes an input string and
290
+ # attempts to convert it to a type comparable by the range. For
291
+ # numeric ranges, this can be omitted because one is provided by
292
+ # default. You should provide a converter for other types of ranges.
293
+ # You can also pass the converter as a block.
294
+ # @param type_desc [String] Type description string, shown in help.
295
+ # Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
296
+ # @param well_known_spec [Object] The well-known acceptor spec associated
297
+ # with this acceptor, or `nil` for none.
298
+ # @param block [Proc] Converter function, if not provided as a normal
299
+ # parameter.
300
+ #
301
+ def initialize(range, converter = nil, type_desc: nil, well_known_spec: nil, &block)
302
+ # Source available in the toys-core gem
303
+ end
304
+
305
+ ##
306
+ # The range being checked.
307
+ # @return [Range]
308
+ #
309
+ attr_reader :range
310
+ end
311
+
312
+ ##
313
+ # A converter proc that handles integers. Useful in Simple and Range
314
+ # acceptors.
315
+ # @return [Proc]
316
+ #
317
+ INTEGER_CONVERTER = proc { |s| s.nil? ? nil : Integer(s) }
318
+
319
+ ##
320
+ # A converter proc that handles floats. Useful in Simple and Range
321
+ # acceptors.
322
+ # @return [Proc]
323
+ #
324
+ FLOAT_CONVERTER = proc { |s| s.nil? ? nil : Float(s) }
325
+
326
+ ##
327
+ # A converter proc that handles rationals. Useful in Simple and Range
328
+ # acceptors.
329
+ # @return [Proc]
330
+ #
331
+ RATIONAL_CONVERTER = proc { |s| s.nil? ? nil : Rational(s) }
332
+
333
+ ##
334
+ # A converter proc that handles any numeric value. Useful in Simple and
335
+ # Range acceptors.
336
+ # @return [Proc]
337
+ #
338
+ NUMERIC_CONVERTER =
339
+ proc do |s|
340
+ if s.nil?
341
+ nil
342
+ elsif s.include?("/")
343
+ Rational(s)
344
+ elsif s.include?(".") || (s.include?("e") && s !~ /\A-?0x/)
345
+ Float(s)
346
+ else
347
+ Integer(s)
348
+ end
349
+ end
350
+
351
+ class << self
352
+ ##
353
+ # Lookup a standard acceptor name recognized by OptionParser.
354
+ #
355
+ # @param spec [Object] A well-known acceptor specification, such as
356
+ # `String`, `Integer`, `Array`, `OptionParser::DecimalInteger`, etc.
357
+ # @return [Toys::Acceptor::Base] The corresponding Acceptor object
358
+ # @return [nil] if the given standard acceptor was not recognized.
359
+ #
360
+ def lookup_well_known(spec)
361
+ # Source available in the toys-core gem
362
+ end
363
+
364
+ ##
365
+ # Create an acceptor from a variety of specification formats. The
366
+ # acceptor is constructed from the given specification object and/or the
367
+ # given block. Additionally, some acceptors can take an optional type
368
+ # description string used to describe the type of data in online help.
369
+ #
370
+ # Recognized specs include:
371
+ #
372
+ # * Any well-known acceptor recognized by OptionParser, such as
373
+ # `Integer`, `Array`, or `OptionParser::DecimalInteger`. Any block
374
+ # and type description you provide are ignored.
375
+ #
376
+ # * Any **regular expression**. The returned acceptor validates only if
377
+ # the regex matches the *entire string parameter*.
378
+ #
379
+ # You can also provide an optional conversion function as a block. If
380
+ # provided, the block must take a variable number of arguments, the
381
+ # first being the matched string and the remainder being the captures
382
+ # from the regular expression. It should return the converted object
383
+ # that will be stored in the context data. If you do not provide a
384
+ # block, no conversion takes place, and the original string is used.
385
+ #
386
+ # * An **array** of possible values. The acceptor validates if the
387
+ # string parameter matches the *string form* of one of the array
388
+ # elements (i.e. the results of calling `to_s` on the element.)
389
+ #
390
+ # An array acceptor automatically converts the string parameter to
391
+ # the actual array element that it matched. For example, if the
392
+ # symbol `:foo` is in the array, it will match the string `"foo"`,
393
+ # and then store the symbol `:foo` in the tool data. You may not
394
+ # further customize the conversion function; any block is ignored.
395
+ #
396
+ # * A **range** of possible values. The acceptor validates if the
397
+ # string parameter, after conversion to the range type, lies within
398
+ # the range. The final value stored in context data is the converted
399
+ # value. For numeric ranges, conversion is provided, but if the range
400
+ # has a different type, you must provide the conversion function as
401
+ # a block.
402
+ #
403
+ # * A **function** as a Proc (where the block is ignored) or a block
404
+ # (if the spec is nil). This function performs *both* validation and
405
+ # conversion. It should take the string parameter as its argument,
406
+ # and it must either return the object that should be stored in the
407
+ # tool data, or raise an exception (descended from `StandardError`)
408
+ # to indicate that the string parameter is invalid. You may also
409
+ # return the sentinel value {Toys::Acceptor::REJECT} to indicate that
410
+ # the string is invalid.
411
+ #
412
+ # * The value `nil` or `:default` with no block, to indicate the
413
+ # default pass-through acceptor {Toys::Acceptor::DEFAULT}. Any type
414
+ # description you provide is ignored.
415
+ #
416
+ # Additional options:
417
+ #
418
+ # * `:type_desc` (String) The type description for interpolating into
419
+ # help text. Ignored if the spec indicates the default acceptor or a
420
+ # well-known acceptor.
421
+ #
422
+ # @param spec [Object] See the description for recognized values.
423
+ # @param options [Hash] Additional options to pass to the acceptor.
424
+ # @param block [Proc] See the description for recognized forms.
425
+ # @return [Toys::Acceptor::Base,Proc]
426
+ #
427
+ def create(spec = nil, **options, &block)
428
+ # Source available in the toys-core gem
429
+ end
430
+ end
431
+ end
432
+ end