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