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,1639 +0,0 @@
1
- module Toys
2
- module DSL
3
- ##
4
- # **_Defined in the toys-core gem_**
5
- #
6
- # This module defines the DSL for a Toys configuration file.
7
- #
8
- # A Toys configuration defines one or more named tools. It provides syntax
9
- # for setting the description, defining flags and arguments, specifying
10
- # how to execute the tool, and requesting mixin modules and other services.
11
- # It also lets you define subtools, nested arbitrarily deep, using blocks.
12
- #
13
- # ### Simple example
14
- #
15
- # Create a file called `.toys.rb` in the current directory, with the
16
- # following contents:
17
- #
18
- # tool "greet" do
19
- # desc "Prints a simple greeting"
20
- #
21
- # optional_arg :recipient, default: "world"
22
- #
23
- # def run
24
- # puts "Hello, #{recipient}!"
25
- # end
26
- # end
27
- #
28
- # The DSL directives `tool`, `desc`, `optional_arg`, and others are defined
29
- # in this module.
30
- #
31
- # Now you can execute it using:
32
- #
33
- # toys greet
34
- #
35
- # or try:
36
- #
37
- # toys greet rubyists
38
- #
39
- module Tool
40
- ##
41
- # Create a named acceptor that can be referenced by name from any flag or
42
- # positional argument in this tool or its subtools.
43
- #
44
- # An acceptor validates the string parameter passed to a flag or
45
- # positional argument. It also optionally converts the string to a
46
- # different object before storing it in your tool's data.
47
- #
48
- # Acceptors can be defined in one of four ways.
49
- #
50
- # * You can provide a **regular expression**. This acceptor validates
51
- # only if the regex matches the *entire string parameter*.
52
- #
53
- # You can also provide an optional conversion function as a block. If
54
- # provided, function must take a variable number of arguments, the
55
- # first being the matched string and the remainder being the captures
56
- # from the regular expression. It should return the converted object
57
- # that will be stored in the context data. If you do not provide a
58
- # block, the original string will be used.
59
- #
60
- # * You can provide an **array** of possible values. The acceptor
61
- # validates if the string parameter matches the *string form* of one
62
- # of the array elements (i.e. the results of calling `to_s` on the
63
- # array elements.)
64
- #
65
- # An array acceptor automatically converts the string parameter to
66
- # the actual array element that it matched. For example, if the
67
- # symbol `:foo` is in the array, it will match the string `"foo"`,
68
- # and then store the symbol `:foo` in the tool data.
69
- #
70
- # * You can provide a **range** of possible values, along with a
71
- # conversion function that converts a string parameter to a type
72
- # comparable by the range. (See the "function" spec below for a
73
- # detailed description of conversion functions.) If the range has
74
- # numeric endpoints, the conversion function is optional because a
75
- # default will be provided.
76
- #
77
- # * You can provide a **function** by passing it as a proc or a block.
78
- # This function performs *both* validation and conversion. It should
79
- # take the string parameter as its argument, and it must either
80
- # return the object that should be stored in the tool data, or raise
81
- # an exception (descended from `StandardError`) to indicate that the
82
- # string parameter is invalid.
83
- #
84
- # ### Example
85
- #
86
- # The following example creates an acceptor named "hex" that is defined
87
- # via a regular expression. It uses the acceptor to validate values
88
- # passed to a flag.
89
- #
90
- # tool "example" do
91
- # acceptor "hex", /[0-9a-fA-F]+/, type_desc: "hex numbers"
92
- # flag :number, accept: "hex"
93
- # def run
94
- # puts "number was #{number}"
95
- # end
96
- # end
97
- #
98
- # @param name [String] The acceptor name.
99
- # @param spec [Object] See the description for recognized values.
100
- # @param type_desc [String] Type description string, shown in help.
101
- # Defaults to the acceptor name.
102
- # @param block [Proc] See the description for recognized forms.
103
- # @return [self]
104
- #
105
- def acceptor(name, spec = nil, type_desc: nil, &block)
106
- # Source available in the toys-core gem
107
- end
108
-
109
- ##
110
- # Create a named mixin module that can be included by name from this tool
111
- # or its subtools.
112
- #
113
- # A mixin is a module that defines methods that can be called from a
114
- # tool. It is commonly used to provide "utility" methods, implementing
115
- # common functionality and allowing tools to share code.
116
- #
117
- # Normally you provide a block and define the mixin's methods in that
118
- # block. Alternatively, you can create a module separately and pass it
119
- # directly to this directive.
120
- #
121
- # ### Example
122
- #
123
- # The following example creates a named mixin and uses it in a tool.
124
- #
125
- # mixin "error-reporter" do
126
- # def error message
127
- # logger.error "An error occurred: #{message}"
128
- # exit 1
129
- # end
130
- # end
131
- #
132
- # tool "build" do
133
- # include "error-reporter"
134
- # def run
135
- # puts "Building..."
136
- # error "Build failed!"
137
- # end
138
- # end
139
- #
140
- # @param name [String] Name of the mixin
141
- # @param mixin_module [Module] Module to use as the mixin. Optional.
142
- # Either pass a module here, *or* provide a block and define the
143
- # mixin within the block.
144
- # @param block [Proc] Defines the mixin module.
145
- # @return [self]
146
- #
147
- def mixin(name, mixin_module = nil, &block)
148
- # Source available in the toys-core gem
149
- end
150
-
151
- ##
152
- # Create a named template that can be expanded by name from this tool
153
- # or its subtools.
154
- #
155
- # A template is an object that generates DSL directives. You can use it
156
- # to build "prefabricated" tools, and then instantiate them in your Toys
157
- # files.
158
- #
159
- # A template is an object that defines an `expansion` procedure. This
160
- # procedure generates the DSL directives implemented by the template. The
161
- # template object typically also includes attributes that are used to
162
- # configure the expansion.
163
- #
164
- # The simplest way to define a template is to pass a block to the
165
- # {#template} directive. In the block, define an `initialize` method that
166
- # accepts any arguments that may be passed to the template when it is
167
- # instantiated and are used to configure the template. Define
168
- # `attr_reader`s or other methods to make this configuration accessible
169
- # from the object. Then define an `on_expand` block that implements the
170
- # template's expansion. The template object is passed as an object to the
171
- # `on_expand` block.
172
- #
173
- # Alternately, you can create a template class separately and pass it
174
- # directly. See {Toys::Template} for details on creating a template
175
- # class.
176
- #
177
- # ### Example
178
- #
179
- # The following example creates and uses a simple template. The template
180
- # defines a tool, with a configurable name, that simply prints out a
181
- # configurable message.
182
- #
183
- # template "hello-generator" do
184
- # def initialize(name, message)
185
- # @name = name
186
- # @message = message
187
- # end
188
- # attr_reader :name, :message
189
- # on_expand do |template|
190
- # tool template.name do
191
- # to_run do
192
- # puts template.message
193
- # end
194
- # end
195
- # end
196
- # end
197
- #
198
- # expand "hello-generator", "mytool", "mytool is running!"
199
- #
200
- # @param name [String] Name of the template
201
- # @param template_class [Class] Module to use as the mixin. Optional.
202
- # Either pass a module here, *or* provide a block and define the
203
- # mixin within the block.
204
- # @param block [Proc] Defines the template class.
205
- # @return [self]
206
- #
207
- def template(name, template_class = nil, &block)
208
- # Source available in the toys-core gem
209
- end
210
-
211
- ##
212
- # Create a named completion procedure that may be used by name by any
213
- # flag or positional arg in this tool or any subtool.
214
- #
215
- # A completion controls tab completion for the value of a flag or
216
- # positional argument. In general, it is a Ruby `Proc` that takes a
217
- # context object (of type {Toys::Completion::Context}) and returns an
218
- # array of completion candidate strings.
219
- #
220
- # Completions can be specified in one of three ways.
221
- #
222
- # * A Proc object itself, either passed directly to this directive or
223
- # provided as a block.
224
- # * A static array of strings, indicating the completion candidates
225
- # independent of context.
226
- # * The symbol `:file_system` which indicates that paths in the file
227
- # system should serve as completion candidates.
228
- #
229
- # ### Example
230
- #
231
- # The following example defines a completion that uses only the immediate
232
- # files in the current directory as candidates. (This is different from
233
- # the `:file_system` completion which will descend into subdirectories
234
- # similar to how bash completes most of its file system commands.)
235
- #
236
- # completion "local-files" do |_context|
237
- # `/bin/ls`.split("\n")
238
- # end
239
- # tool "example" do
240
- # flag :file, complete_values: "local-files"
241
- # def run
242
- # puts "selected file #{file}"
243
- # end
244
- # end
245
- #
246
- # @param name [String] Name of the completion
247
- # @param spec [Object] See the description for recognized values.
248
- # @param options [Hash] Additional options to pass to the completion.
249
- # @param block [Proc] See the description for recognized forms.
250
- # @return [self]
251
- #
252
- def completion(name, spec = nil, **options, &block)
253
- # Source available in the toys-core gem
254
- end
255
-
256
- ##
257
- # Create a subtool. You must provide a block defining the subtool.
258
- #
259
- # ### Example
260
- #
261
- # The following example defines a tool and two subtools within it.
262
- #
263
- # tool "build" do
264
- # tool "staging" do
265
- # def run
266
- # puts "Building staging"
267
- # end
268
- # end
269
- # tool "production" do
270
- # def run
271
- # puts "Building production"
272
- # end
273
- # end
274
- # end
275
- #
276
- # The following example uses `delegate_to` to define a tool that runs one
277
- # of its subtools.
278
- #
279
- # tool "test", delegate_to: ["test", "unit"] do
280
- # tool "unit" do
281
- # def run
282
- # puts "Running unit tests"
283
- # end
284
- # end
285
- # end
286
- #
287
- # @param words [String,Array<String>] The name of the subtool
288
- # @param if_defined [:combine,:reset,:ignore] What to do if a definition
289
- # already exists for this tool. Possible values are `:combine` (the
290
- # default) indicating the definition should be combined with the
291
- # existing definition, `:reset` indicating the earlier definition
292
- # should be reset and the new definition applied instead, or
293
- # `:ignore` indicating the new definition should be ignored.
294
- # @param delegate_to [String,Array<String>] Optional. This tool should
295
- # delegate to another tool, specified by the full path. This path may
296
- # be given as an array of strings, or a single string possibly
297
- # delimited by path separators.
298
- # @param delegate_relative [String,Array<String>] Optional. Similar to
299
- # delegate_to, but takes a delegate name relative to the context in
300
- # which this tool is being defined.
301
- # @param block [Proc] Defines the subtool.
302
- # @return [self]
303
- #
304
- def tool(words, if_defined: :combine, delegate_to: nil, delegate_relative: nil, &block)
305
- # Source available in the toys-core gem
306
- end
307
-
308
- ##
309
- # Create an alias, representing an "alternate name" for a tool.
310
- #
311
- # Note: This is functionally equivalent to creating a tool with the
312
- # `:delegate_relative` option. As such, `alias_tool` is considered
313
- # deprecated.
314
- #
315
- # ### Example
316
- #
317
- # This example defines a tool and an alias pointing to it. Both the tool
318
- # name `test` and the alias `t` will then refer to the same tool.
319
- #
320
- # tool "test" do
321
- # def run
322
- # puts "Running tests..."
323
- # end
324
- # end
325
- # alias_tool "t", "test"
326
- # # Note: the following is preferred over alias_tool:
327
- # # tool "t", delegate_relative: "test"
328
- #
329
- # @param word [String] The name of the alias
330
- # @param target [String,Array<String>] Relative path to the target of the
331
- # alias. This path may be given as an array of strings, or a single
332
- # string possibly delimited by path separators.
333
- # @return [self]
334
- # @deprecated Use {#tool} and pass `:delegate_relative` instead
335
- #
336
- def alias_tool(word, target)
337
- # Source available in the toys-core gem
338
- end
339
-
340
- ##
341
- # Causes the current tool to delegate to another tool, specified by the
342
- # full tool name. When run, it simply invokes the target tool with the
343
- # same arguments.
344
- #
345
- # ### Example
346
- #
347
- # This example defines a tool that runs one of its subtools. Running the
348
- # `test` tool will have the same effect (and recognize the same args) as
349
- # the subtool `test unit`.
350
- #
351
- # tool "test" do
352
- # tool "unit" do
353
- # flag :faster
354
- # def run
355
- # puts "running tests..."
356
- # end
357
- # end
358
- # delegate_to "test:unit"
359
- # end
360
- #
361
- # @param target [String,Array<String>] The full path to the delegate
362
- # tool. This path may be given as an array of strings, or a single
363
- # string possibly delimited by path separators.
364
- # @return [self]
365
- #
366
- def delegate_to(target)
367
- # Source available in the toys-core gem
368
- end
369
-
370
- ##
371
- # Load another config file or directory, as if its contents were inserted
372
- # at the current location.
373
- #
374
- # @param path [String] The file or directory to load.
375
- # @param as [String] Load into the given tool/namespace. If omitted,
376
- # configuration will be loaded into the current namespace.
377
- #
378
- # @return [self]
379
- #
380
- def load(path, as: nil)
381
- # Source available in the toys-core gem
382
- end
383
-
384
- ##
385
- # Load configuration from a public git repository, as if its contents
386
- # were inserted at the current location.
387
- #
388
- # @param remote [String] The URL of the git repository. Defaults to the
389
- # current repository if already loading from git.
390
- # @param path [String] The path within the repo to the file or directory
391
- # to load. Defaults to the root of the repo.
392
- # @param commit [String] The commit branch, tag, or sha. Defaults to the
393
- # current commit if already loading from git, or to `HEAD`.
394
- # @param as [String] Load into the given tool/namespace. If omitted,
395
- # configuration will be loaded into the current namespace.
396
- # @param update [Boolean] Force-fetch from the remote (unless the commit
397
- # is a SHA). This will ensure that symbolic commits, such as branch
398
- # names, are up to date. Default is false.
399
- #
400
- # @return [self]
401
- #
402
- def load_git(remote: nil, path: nil, commit: nil, as: nil, update: false)
403
- # Source available in the toys-core gem
404
- end
405
-
406
- ##
407
- # Expand the given template in the current location.
408
- #
409
- # The template may be specified as a class or a well-known template name.
410
- # You may also provide arguments to pass to the template.
411
- #
412
- # ### Example
413
- #
414
- # The following example creates and uses a simple template.
415
- #
416
- # template "hello-generator" do
417
- # def initialize(name, message)
418
- # @name = name
419
- # @message = message
420
- # end
421
- # attr_reader :name, :message
422
- # expansion do |template|
423
- # tool template.name do
424
- # to_run do
425
- # puts template.message
426
- # end
427
- # end
428
- # end
429
- # end
430
- #
431
- # expand "hello-generator", "mytool", "mytool is running!"
432
- #
433
- # @param template_class [Class,String,Symbol] The template, either as a
434
- # class or a well-known name.
435
- # @param args [Object...] Template arguments
436
- # @return [self]
437
- #
438
- def expand(template_class, *args, **kwargs)
439
- # Source available in the toys-core gem
440
- end
441
-
442
- ##
443
- # Set the short description for the current tool. The short description
444
- # is displayed with the tool in a subtool list. You may also use the
445
- # equivalent method `short_desc`.
446
- #
447
- # The description is a {Toys::WrappableString}, which may be word-wrapped
448
- # when displayed in a help screen. You may pass a {Toys::WrappableString}
449
- # directly to this method, or you may pass any input that can be used to
450
- # construct a wrappable string:
451
- #
452
- # * If you pass a String, its whitespace will be compacted (i.e. tabs,
453
- # newlines, and multiple consecutive whitespace will be turned into a
454
- # single space), and it will be word-wrapped on whitespace.
455
- # * If you pass an Array of Strings, each string will be considered a
456
- # literal word that cannot be broken, and wrapping will be done
457
- # across the strings in the array. In this case, whitespace is not
458
- # compacted.
459
- #
460
- # ### Examples
461
- #
462
- # If you pass in a sentence as a simple string, it may be word wrapped
463
- # when displayed:
464
- #
465
- # desc "This sentence may be wrapped."
466
- #
467
- # To specify a sentence that should never be word-wrapped, pass it as the
468
- # sole element of a string array:
469
- #
470
- # desc ["This sentence will not be wrapped."]
471
- #
472
- # @param str [Toys::WrappableString,String,Array<String>]
473
- # @return [self]
474
- #
475
- def desc(str)
476
- # Source available in the toys-core gem
477
- end
478
- alias short_desc desc
479
-
480
- ##
481
- # Add to the long description for the current tool. The long description
482
- # is displayed in the usage documentation for the tool itself. This
483
- # directive may be given multiple times, and the results are cumulative.
484
- #
485
- # A long description is a series of descriptions, which are generally
486
- # displayed in a series of lines/paragraphs. Each individual description
487
- # uses the form described in the {#desc} documentation, and may be
488
- # word-wrapped when displayed. To insert a blank line, include an empty
489
- # string as one of the descriptions.
490
- #
491
- # ### Example
492
- #
493
- # long_desc "This initial paragraph might get word wrapped.",
494
- # "This next paragraph is followed by a blank line.",
495
- # "",
496
- # ["This line will not be wrapped."],
497
- # [" This indent is preserved."]
498
- # long_desc "This line is appended to the description."
499
- #
500
- # @param strs [Toys::WrappableString,String,Array<String>...]
501
- # @param file [String] Optional. Read the description from the given file
502
- # provided relative to the current toys file. The file must be a
503
- # plain text file whose suffix is `.txt`.
504
- # @param data [String] Optional. Read the description from the given data
505
- # file. The file must be a plain text file whose suffix is `.txt`.
506
- # @return [self]
507
- #
508
- def long_desc(*strs, file: nil, data: nil)
509
- # Source available in the toys-core gem
510
- end
511
-
512
- ##
513
- # Create a flag group. If a block is given, flags defined in the block
514
- # belong to the group. The flags in the group are listed together in
515
- # help screens.
516
- #
517
- # ### Example
518
- #
519
- # The following example creates a flag group in which all flags are
520
- # optional.
521
- #
522
- # tool "execute" do
523
- # flag_group desc: "Debug Flags" do
524
- # flag :debug, "-D", desc: "Enable debugger"
525
- # flag :warnings, "-W[VAL]", desc: "Enable warnings"
526
- # end
527
- # # ...
528
- # end
529
- #
530
- # @param type [Symbol] The type of group. Allowed values: `:required`,
531
- # `:optional`, `:exactly_one`, `:at_most_one`, `:at_least_one`.
532
- # Default is `:optional`.
533
- # @param desc [String,Array<String>,Toys::WrappableString] Short
534
- # description for the group. See {Toys::DSL::Tool#desc} for a
535
- # description of allowed formats. Defaults to `"Flags"`.
536
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
537
- # Long description for the flag group. See
538
- # {Toys::DSL::Tool#long_desc} for a description of allowed formats.
539
- # Defaults to the empty array.
540
- # @param name [String,Symbol,nil] The name of the group, or nil for no
541
- # name.
542
- # @param report_collisions [Boolean] If `true`, raise an exception if a
543
- # the given name is already taken. If `false`, ignore. Default is
544
- # `true`.
545
- # @param prepend [Boolean] If `true`, prepend rather than append the
546
- # group to the list. Default is `false`.
547
- # @param block [Proc] Adds flags to the group. See {Toys::DSL::FlagGroup}
548
- # for the directives that can be called in this block.
549
- # @return [self]
550
- #
551
- def flag_group(type: :optional, desc: nil, long_desc: nil, name: nil,
552
- report_collisions: true, prepend: false, &block)
553
- # Source available in the toys-core gem
554
- end
555
-
556
- ##
557
- # Create a flag group of type `:required`. If a block is given, flags
558
- # defined in the block belong to the group. All flags in this group are
559
- # required.
560
- #
561
- # ### Example
562
- #
563
- # The following example creates a group of required flags.
564
- #
565
- # tool "login" do
566
- # all_required do
567
- # flag :username, "--username=VAL", desc: "Set username (required)"
568
- # flag :password, "--password=VAL", desc: "Set password (required)"
569
- # end
570
- # # ...
571
- # end
572
- #
573
- # @param desc [String,Array<String>,Toys::WrappableString] Short
574
- # description for the group. See {Toys::DSL::Tool#desc} for a
575
- # description of allowed formats. Defaults to `"Flags"`.
576
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
577
- # Long description for the flag group. See
578
- # {Toys::DSL::Tool#long_desc} for a description of allowed formats.
579
- # Defaults to the empty array.
580
- # @param name [String,Symbol,nil] The name of the group, or nil for no
581
- # name.
582
- # @param report_collisions [Boolean] If `true`, raise an exception if a
583
- # the given name is already taken. If `false`, ignore. Default is
584
- # `true`.
585
- # @param prepend [Boolean] If `true`, prepend rather than append the
586
- # group to the list. Default is `false`.
587
- # @param block [Proc] Adds flags to the group. See {Toys::DSL::FlagGroup}
588
- # for the directives that can be called in this block.
589
- # @return [self]
590
- #
591
- def all_required(desc: nil, long_desc: nil, name: nil, report_collisions: true,
592
- prepend: false, &block)
593
- # Source available in the toys-core gem
594
- end
595
-
596
- ##
597
- # Create a flag group of type `:at_most_one`. If a block is given, flags
598
- # defined in the block belong to the group. At most one flag in this
599
- # group must be provided on the command line.
600
- #
601
- # ### Example
602
- #
603
- # The following example creates a group of flags in which either one or
604
- # none may be set, but not more than one.
605
- #
606
- # tool "provision-server" do
607
- # at_most_one do
608
- # flag :restore_from_backup, "--restore-from-backup=VAL"
609
- # flag :restore_from_image, "--restore-from-image=VAL"
610
- # flag :clone_existing, "--clone-existing=VAL"
611
- # end
612
- # # ...
613
- # end
614
- #
615
- # @param desc [String,Array<String>,Toys::WrappableString] Short
616
- # description for the group. See {Toys::DSL::Tool#desc} for a
617
- # description of allowed formats. Defaults to `"Flags"`.
618
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
619
- # Long description for the flag group. See
620
- # {Toys::DSL::Tool#long_desc} for a description of allowed formats.
621
- # Defaults to the empty array.
622
- # @param name [String,Symbol,nil] The name of the group, or nil for no
623
- # name.
624
- # @param report_collisions [Boolean] If `true`, raise an exception if a
625
- # the given name is already taken. If `false`, ignore. Default is
626
- # `true`.
627
- # @param prepend [Boolean] If `true`, prepend rather than append the
628
- # group to the list. Default is `false`.
629
- # @param block [Proc] Adds flags to the group. See {Toys::DSL::FlagGroup}
630
- # for the directives that can be called in this block.
631
- # @return [self]
632
- #
633
- def at_most_one(desc: nil, long_desc: nil, name: nil, report_collisions: true,
634
- prepend: false, &block)
635
- # Source available in the toys-core gem
636
- end
637
- alias at_most_one_required at_most_one
638
-
639
- ##
640
- # Create a flag group of type `:at_least_one`. If a block is given, flags
641
- # defined in the block belong to the group. At least one flag in this
642
- # group must be provided on the command line.
643
- #
644
- # ### Example
645
- #
646
- # The following example creates a group of flags in which one or more
647
- # may be set.
648
- #
649
- # tool "run-tests" do
650
- # at_least_one do
651
- # flag :unit, desc: "Run unit tests"
652
- # flag :integration, desc: "Run integration tests"
653
- # flag :performance, desc: "Run performance tests"
654
- # end
655
- # # ...
656
- # end
657
- #
658
- # @param desc [String,Array<String>,Toys::WrappableString] Short
659
- # description for the group. See {Toys::DSL::Tool#desc} for a
660
- # description of allowed formats. Defaults to `"Flags"`.
661
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
662
- # Long description for the flag group. See
663
- # {Toys::DSL::Tool#long_desc} for a description of allowed formats.
664
- # Defaults to the empty array.
665
- # @param name [String,Symbol,nil] The name of the group, or nil for no
666
- # name.
667
- # @param report_collisions [Boolean] If `true`, raise an exception if a
668
- # the given name is already taken. If `false`, ignore. Default is
669
- # `true`.
670
- # @param prepend [Boolean] If `true`, prepend rather than append the
671
- # group to the list. Default is `false`.
672
- # @param block [Proc] Adds flags to the group. See {Toys::DSL::FlagGroup}
673
- # for the directives that can be called in this block.
674
- # @return [self]
675
- #
676
- def at_least_one(desc: nil, long_desc: nil, name: nil, report_collisions: true,
677
- prepend: false, &block)
678
- # Source available in the toys-core gem
679
- end
680
- alias at_least_one_required at_least_one
681
-
682
- ##
683
- # Create a flag group of type `:exactly_one`. If a block is given, flags
684
- # defined in the block belong to the group. Exactly one flag in this
685
- # group must be provided on the command line.
686
- #
687
- # ### Example
688
- #
689
- # The following example creates a group of flags in which exactly one
690
- # must be set.
691
- #
692
- # tool "deploy" do
693
- # exactly_one do
694
- # flag :server, "--server=IP_ADDR", desc: "Deploy to server"
695
- # flag :vm, "--vm=ID", desc: "Deploy to a VM"
696
- # flag :container, "--container=ID", desc: "Deploy to a container"
697
- # end
698
- # # ...
699
- # end
700
- #
701
- # @param desc [String,Array<String>,Toys::WrappableString] Short
702
- # description for the group. See {Toys::DSL::Tool#desc} for a
703
- # description of allowed formats. Defaults to `"Flags"`.
704
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
705
- # Long description for the flag group. See
706
- # {Toys::DSL::Tool#long_desc} for a description of allowed formats.
707
- # Defaults to the empty array.
708
- # @param name [String,Symbol,nil] The name of the group, or nil for no
709
- # name.
710
- # @param report_collisions [Boolean] If `true`, raise an exception if a
711
- # the given name is already taken. If `false`, ignore. Default is
712
- # `true`.
713
- # @param prepend [Boolean] If `true`, prepend rather than append the
714
- # group to the list. Default is `false`.
715
- # @param block [Proc] Adds flags to the group. See {Toys::DSL::FlagGroup}
716
- # for the directives that can be called in this block.
717
- # @return [self]
718
- #
719
- def exactly_one(desc: nil, long_desc: nil, name: nil, report_collisions: true,
720
- prepend: false, &block)
721
- # Source available in the toys-core gem
722
- end
723
- alias exactly_one_required exactly_one
724
-
725
- ##
726
- # Add a flag to the current tool. Each flag must specify a key which
727
- # the script may use to obtain the flag value from the context.
728
- # You may then provide the flags themselves in OptionParser form.
729
- #
730
- # If the given key is a symbol representing a valid method name, then a
731
- # helper method is automatically added to retrieve the value. Otherwise,
732
- # if the key is a string or does not represent a valid method name, the
733
- # tool can retrieve the value by calling {Toys::Context#get}.
734
- #
735
- # Attributes of the flag may be passed in as arguments to this method, or
736
- # set in a block passed to this method. If you provide a block, you can
737
- # use directives in {Toys::DSL::Flag} within the block.
738
- #
739
- # ### Flag syntax
740
- #
741
- # The flags themselves should be provided in OptionParser form. Following
742
- # are examples of valid syntax.
743
- #
744
- # * `-a` : A short boolean switch. When this appears as an argument,
745
- # the value is set to `true`.
746
- # * `--abc` : A long boolean switch. When this appears as an argument,
747
- # the value is set to `true`.
748
- # * `-aVAL` or `-a VAL` : A short flag that takes a required value.
749
- # These two forms are treated identically. If this argument appears
750
- # with a value attached (e.g. `-afoo`), the attached string (e.g.
751
- # `"foo"`) is taken as the value. Otherwise, the following argument
752
- # is taken as the value (e.g. for `-a foo`, the value is set to
753
- # `"foo"`.) The following argument is treated as the value even if it
754
- # looks like a flag (e.g. `-a -a` causes the string `"-a"` to be
755
- # taken as the value.)
756
- # * `-a[VAL]` : A short flag that takes an optional value. If this
757
- # argument appears with a value attached (e.g. `-afoo`), the attached
758
- # string (e.g. `"foo"`) is taken as the value. Otherwise, the value
759
- # is set to `true`. The following argument is never interpreted as
760
- # the value. (Compare with `-a [VAL]`.)
761
- # * `-a [VAL]` : A short flag that takes an optional value. If this
762
- # argument appears with a value attached (e.g. `-afoo`), the attached
763
- # string (e.g. `"foo"`) is taken as the value. Otherwise, if the
764
- # following argument does not look like a flag (i.e. it does not
765
- # begin with a hyphen), it is taken as the value. (e.g. `-a foo`
766
- # causes the string `"foo"` to be taken as the value.). If there is
767
- # no following argument, or the following argument looks like a flag,
768
- # the value is set to `true`. (Compare with `-a[VAL]`.)
769
- # * `--abc=VAL` or `--abc VAL` : A long flag that takes a required
770
- # value. These two forms are treated identically. If this argument
771
- # appears with a value attached (e.g. `--abc=foo`), the attached
772
- # string (e.g. `"foo"`) is taken as the value. Otherwise, the
773
- # following argument is taken as the value (e.g. for `--abc foo`, the
774
- # value is set to `"foo"`.) The following argument is treated as the
775
- # value even if it looks like a flag (e.g. `--abc --def` causes the
776
- # string `"--def"` to be taken as the value.)
777
- # * `--abc[=VAL]` : A long flag that takes an optional value. If this
778
- # argument appears with a value attached (e.g. `--abc=foo`), the
779
- # attached string (e.g. `"foo"`) is taken as the value. Otherwise,
780
- # the value is set to `true`. The following argument is never
781
- # interpreted as the value. (Compare with `--abc [VAL]`.)
782
- # * `--abc [VAL]` : A long flag that takes an optional value. If this
783
- # argument appears with a value attached (e.g. `--abc=foo`), the
784
- # attached string (e.g. `"foo"`) is taken as the value. Otherwise, if
785
- # the following argument does not look like a flag (i.e. it does not
786
- # begin with a hyphen), it is taken as the value. (e.g. `--abc foo`
787
- # causes the string `"foo"` to be taken as the value.). If there is
788
- # no following argument, or the following argument looks like a flag,
789
- # the value is set to `true`. (Compare with `--abc=[VAL]`.)
790
- # * `--[no-]abc` : A long boolean switch that can be turned either on
791
- # or off. This effectively creates two flags, `--abc` which sets the
792
- # value to `true`, and `--no-abc` which sets the falue to `false`.
793
- #
794
- # ### Default flag syntax
795
- #
796
- # If no flag syntax strings are provided, a default syntax will be
797
- # inferred based on the key and other options.
798
- #
799
- # Specifically, if the key has one character, then that character will be
800
- # chosen as a short flag. If the key has multiple characters, a long flag
801
- # will be generated.
802
- #
803
- # Furthermore, if a custom completion, a non-boolean acceptor, or a
804
- # non-boolean default value is provided in the options, then the flag
805
- # will be considered to take a value. Otherwise, it will be considered to
806
- # be a boolean switch.
807
- #
808
- # For example, the following pairs of flags are identical:
809
- #
810
- # flag :a
811
- # flag :a, "-a"
812
- #
813
- # flag :abc_def
814
- # flag :abc_def, "--abc-def"
815
- #
816
- # flag :number, accept: Integer
817
- # flag :number, "--number=VAL", accept: Integer
818
- #
819
- # ### More examples
820
- #
821
- # A flag that sets its value to the number of times it appears on the
822
- # command line:
823
- #
824
- # flag :verbose, "-v", "--verbose",
825
- # default: 0, handler: ->(_val, count) { count + 1 }
826
- #
827
- # An example using block form:
828
- #
829
- # flag :shout do
830
- # flags "-s", "--shout"
831
- # default false
832
- # desc "Say it louder"
833
- # long_desc "This flag says it lowder.",
834
- # "You might use this when people can't hear you.",
835
- # "",
836
- # "Example:",
837
- # [" toys say --shout hello"]
838
- # end
839
- #
840
- # @param key [String,Symbol] The key to use to retrieve the value from
841
- # the execution context.
842
- # @param flags [String...] The flags in OptionParser format.
843
- # @param accept [Object] An acceptor that validates and/or converts the
844
- # value. You may provide either the name of an acceptor you have
845
- # defined, one of the default acceptors provided by OptionParser, or
846
- # any other specification recognized by {Toys::Acceptor.create}.
847
- # Optional. If not specified, accepts any value as a string.
848
- # @param default [Object] The default value. This is the value that will
849
- # be set in the context if this flag is not provided on the command
850
- # line. Defaults to `nil`.
851
- # @param handler [Proc,nil,:set,:push] An optional handler for
852
- # setting/updating the value. A handler is a proc taking two
853
- # arguments, the given value and the previous value, returning the
854
- # new value that should be set. You may also specify a predefined
855
- # named handler. The `:set` handler (the default) replaces the
856
- # previous value (effectively `-> (val, _prev) { val }`). The
857
- # `:push` handler expects the previous value to be an array and
858
- # pushes the given value onto it; it should be combined with setting
859
- # `default: []` and is intended for "multi-valued" flags.
860
- # @param complete_flags [Object] A specifier for shell tab completion
861
- # for flag names associated with this flag. By default, a
862
- # {Toys::Flag::DefaultCompletion} is used, which provides the flag's
863
- # names as completion candidates. To customize completion, set this
864
- # to the name of a previously defined completion, a hash of options
865
- # to pass to the constructor for {Toys::Flag::DefaultCompletion}, or
866
- # any other spec recognized by {Toys::Completion.create}.
867
- # @param complete_values [Object] A specifier for shell tab completion
868
- # for flag values associated with this flag. This is the empty
869
- # completion by default. To customize completion, set this to the
870
- # name of a previously defined completion, or any spec recognized by
871
- # {Toys::Completion.create}.
872
- # @param report_collisions [Boolean] Raise an exception if a flag is
873
- # requested that is already in use or marked as unusable. Default is
874
- # true.
875
- # @param group [Toys::FlagGroup,String,Symbol,nil] Group for this flag.
876
- # You may provide a group name, a FlagGroup object, or `nil` which
877
- # denotes the default group.
878
- # @param desc [String,Array<String>,Toys::WrappableString] Short
879
- # description for the flag. See {Toys::DSL::Tool#desc} for a
880
- # description of the allowed formats. Defaults to the empty string.
881
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
882
- # Long description for the flag. See {Toys::DSL::Tool#long_desc} for
883
- # a description of the allowed formats. (But note that this param
884
- # takes an Array of description lines, rather than a series of
885
- # arguments.) Defaults to the empty array.
886
- # @param display_name [String] A display name for this flag, used in help
887
- # text and error messages.
888
- # @param add_method [true,false,nil] Whether to add a method for this
889
- # flag. If omitted or set to nil, uses the default behavior, which
890
- # adds the method if the key is a symbol representing a legal method
891
- # name that starts with a letter and does not override any public
892
- # method in the Ruby Object class or collide with any method directly
893
- # defined in the tool class.
894
- # @param block [Proc] Configures the flag. See {Toys::DSL::Flag} for the
895
- # directives that can be called in this block.
896
- # @return [self]
897
- #
898
- def flag(key, *flags,
899
- accept: nil, default: nil, handler: nil,
900
- complete_flags: nil, complete_values: nil,
901
- report_collisions: true, group: nil,
902
- desc: nil, long_desc: nil, display_name: nil, add_method: nil,
903
- &block)
904
- # Source available in the toys-core gem
905
- end
906
-
907
- ##
908
- # Add a required positional argument to the current tool. You must
909
- # specify a key which the script may use to obtain the argument value
910
- # from the context.
911
- #
912
- # If the given key is a symbol representing a valid method name, then a
913
- # helper method is automatically added to retrieve the value. Otherwise,
914
- # if the key is a string or does not represent a valid method name, the
915
- # tool can retrieve the value by calling {Toys::Context#get}.
916
- #
917
- # Attributes of the arg may be passed in as arguments to this method, or
918
- # set in a block passed to this method. If you provide a block, you can
919
- # use directives in {Toys::DSL::PositionalArg} within the block.
920
- #
921
- # ### Example
922
- #
923
- # This tool "moves" something from a source to destination, and takes two
924
- # required arguments:
925
- #
926
- # tool "mv" do
927
- # required_arg :source
928
- # required_arg :dest
929
- # def run
930
- # puts "moving from #{source} to #{dest}..."
931
- # end
932
- # end
933
- #
934
- # @param key [String,Symbol] The key to use to retrieve the value from
935
- # the execution context.
936
- # @param accept [Object] An acceptor that validates and/or converts the
937
- # value. You may provide either the name of an acceptor you have
938
- # defined, one of the default acceptors provided by OptionParser, or
939
- # any other specification recognized by {Toys::Acceptor.create}.
940
- # Optional. If not specified, accepts any value as a string.
941
- # @param complete [Object] A specifier for shell tab completion for
942
- # values of this arg. This is the empty completion by default. To
943
- # customize completion, set this to the name of a previously defined
944
- # completion, or any spec recognized by {Toys::Completion.create}.
945
- # @param display_name [String] A name to use for display (in help text
946
- # and error reports). Defaults to the key in upper case.
947
- # @param desc [String,Array<String>,Toys::WrappableString] Short
948
- # description for the flag. See {Toys::DSL::Tool#desc} for a
949
- # description of the allowed formats. Defaults to the empty string.
950
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
951
- # Long description for the flag. See {Toys::DSL::Tool#long_desc} for
952
- # a description of the allowed formats. (But note that this param
953
- # takes an Array of description lines, rather than a series of
954
- # arguments.) Defaults to the empty array.
955
- # @param add_method [true,false,nil] Whether to add a method for this
956
- # argument. If omitted or set to nil, uses the default behavior,
957
- # which adds the method if the key is a symbol representing a legal
958
- # method name that starts with a letter and does not override any
959
- # public method in the Ruby Object class or collide with any method
960
- # directly defined in the tool class.
961
- # @param block [Proc] Configures the positional argument. See
962
- # {Toys::DSL::PositionalArg} for the directives that can be called in
963
- # this block.
964
- # @return [self]
965
- #
966
- def required_arg(key,
967
- accept: nil, complete: nil, display_name: nil,
968
- desc: nil, long_desc: nil, add_method: nil,
969
- &block)
970
- # Source available in the toys-core gem
971
- end
972
- alias required required_arg
973
-
974
- ##
975
- # Add an optional positional argument to the current tool. You must
976
- # specify a key which the script may use to obtain the argument value
977
- # from the context. If an optional argument is not given on the command
978
- # line, the value is set to the given default.
979
- #
980
- # If the given key is a symbol representing a valid method name, then a
981
- # helper method is automatically added to retrieve the value. Otherwise,
982
- # if the key is a string or does not represent a valid method name, the
983
- # tool can retrieve the value by calling {Toys::Context#get}.
984
- #
985
- # Attributes of the arg may be passed in as arguments to this method, or
986
- # set in a block passed to this method. If you provide a block, you can
987
- # use directives in {Toys::DSL::PositionalArg} within the block.
988
- #
989
- # ### Example
990
- #
991
- # This tool creates a "link" to a given target. The link location is
992
- # optional; if it is not given, it is inferred from the target.
993
- #
994
- # tool "ln" do
995
- # required_arg :target
996
- # optional_arg :location
997
- # def run
998
- # loc = location || File.basename(target)
999
- # puts "linking to #{target} from #{loc}..."
1000
- # end
1001
- # end
1002
- #
1003
- # @param key [String,Symbol] The key to use to retrieve the value from
1004
- # the execution context.
1005
- # @param default [Object] The default value. This is the value that will
1006
- # be set in the context if this argument is not provided on the
1007
- # command line. Defaults to `nil`.
1008
- # @param accept [Object] An acceptor that validates and/or converts the
1009
- # value. You may provide either the name of an acceptor you have
1010
- # defined, one of the default acceptors provided by OptionParser, or
1011
- # any other specification recognized by {Toys::Acceptor.create}.
1012
- # Optional. If not specified, accepts any value as a string.
1013
- # @param complete [Object] A specifier for shell tab completion for
1014
- # values of this arg. This is the empty completion by default. To
1015
- # customize completion, set this to the name of a previously defined
1016
- # completion, or any spec recognized by {Toys::Completion.create}.
1017
- # @param display_name [String] A name to use for display (in help text
1018
- # and error reports). Defaults to the key in upper case.
1019
- # @param desc [String,Array<String>,Toys::WrappableString] Short
1020
- # description for the flag. See {Toys::DSL::Tool#desc} for a
1021
- # description of the allowed formats. Defaults to the empty string.
1022
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
1023
- # Long description for the flag. See {Toys::DSL::Tool#long_desc} for
1024
- # a description of the allowed formats. (But note that this param
1025
- # takes an Array of description lines, rather than a series of
1026
- # arguments.) Defaults to the empty array.
1027
- # @param add_method [true,false,nil] Whether to add a method for this
1028
- # argument. If omitted or set to nil, uses the default behavior,
1029
- # which adds the method if the key is a symbol representing a legal
1030
- # method name that starts with a letter and does not override any
1031
- # public method in the Ruby Object class or collide with any method
1032
- # directly defined in the tool class.
1033
- # @param block [Proc] Configures the positional argument. See
1034
- # {Toys::DSL::PositionalArg} for the directives that can be called in
1035
- # this block.
1036
- # @return [self]
1037
- #
1038
- def optional_arg(key,
1039
- default: nil, accept: nil, complete: nil, display_name: nil,
1040
- desc: nil, long_desc: nil, add_method: nil,
1041
- &block)
1042
- # Source available in the toys-core gem
1043
- end
1044
- alias optional optional_arg
1045
-
1046
- ##
1047
- # Specify what should be done with unmatched positional arguments. You
1048
- # must specify a key which the script may use to obtain the remaining
1049
- # args from the context.
1050
- #
1051
- # If the given key is a symbol representing a valid method name, then a
1052
- # helper method is automatically added to retrieve the value. Otherwise,
1053
- # if the key is a string or does not represent a valid method name, the
1054
- # tool can retrieve the value by calling {Toys::Context#get}.
1055
- #
1056
- # Attributes of the arg may be passed in as arguments to this method, or
1057
- # set in a block passed to this method. If you provide a block, you can
1058
- # use directives in {Toys::DSL::PositionalArg} within the block.
1059
- #
1060
- # ### Example
1061
- #
1062
- # This tool displays a "list" of the given directories. If no directories
1063
- # ar given, lists the current directory.
1064
- #
1065
- # tool "ln" do
1066
- # remaining_args :directories
1067
- # def run
1068
- # dirs = directories.empty? ? [Dir.pwd] : directories
1069
- # dirs.each do |dir|
1070
- # puts "Listing directory #{dir}..."
1071
- # end
1072
- # end
1073
- # end
1074
- #
1075
- # @param key [String,Symbol] The key to use to retrieve the value from
1076
- # the execution context.
1077
- # @param default [Object] The default value. This is the value that will
1078
- # be set in the context if no unmatched arguments are provided on the
1079
- # command line. Defaults to the empty array `[]`.
1080
- # @param accept [Object] An acceptor that validates and/or converts the
1081
- # value. You may provide either the name of an acceptor you have
1082
- # defined, one of the default acceptors provided by OptionParser, or
1083
- # any other specification recognized by {Toys::Acceptor.create}.
1084
- # Optional. If not specified, accepts any value as a string.
1085
- # @param complete [Object] A specifier for shell tab completion for
1086
- # values of this arg. This is the empty completion by default. To
1087
- # customize completion, set this to the name of a previously defined
1088
- # completion, or any spec recognized by {Toys::Completion.create}.
1089
- # @param display_name [String] A name to use for display (in help text
1090
- # and error reports). Defaults to the key in upper case.
1091
- # @param desc [String,Array<String>,Toys::WrappableString] Short
1092
- # description for the flag. See {Toys::DSL::Tool#desc} for a
1093
- # description of the allowed formats. Defaults to the empty string.
1094
- # @param long_desc [Array<String,Array<String>,Toys::WrappableString>]
1095
- # Long description for the flag. See {Toys::DSL::Tool#long_desc} for
1096
- # a description of the allowed formats. (But note that this param
1097
- # takes an Array of description lines, rather than a series of
1098
- # arguments.) Defaults to the empty array.
1099
- # @param add_method [true,false,nil] Whether to add a method for these
1100
- # arguments. If omitted or set to nil, uses the default behavior,
1101
- # which adds the method if the key is a symbol representing a legal
1102
- # method name that starts with a letter and does not override any
1103
- # public method in the Ruby Object class or collide with any method
1104
- # directly defined in the tool class.
1105
- # @param block [Proc] Configures the positional argument. See
1106
- # {Toys::DSL::PositionalArg} for the directives that can be called in
1107
- # this block.
1108
- # @return [self]
1109
- #
1110
- def remaining_args(key,
1111
- default: [], accept: nil, complete: nil, display_name: nil,
1112
- desc: nil, long_desc: nil, add_method: nil,
1113
- &block)
1114
- # Source available in the toys-core gem
1115
- end
1116
- alias remaining remaining_args
1117
-
1118
- ##
1119
- # Set option values statically and create helper methods.
1120
- #
1121
- # If any given key is a symbol representing a valid method name, then a
1122
- # helper method is automatically added to retrieve the value. Otherwise,
1123
- # if the key is a string or does not represent a valid method name, the
1124
- # tool can retrieve the value by calling {Toys::Context#get}.
1125
- #
1126
- # ### Example
1127
- #
1128
- # tool "hello" do
1129
- # static :greeting, "Hi there"
1130
- # def run
1131
- # puts "#{greeting}, world!"
1132
- # end
1133
- # end
1134
- #
1135
- # @overload static(key, value)
1136
- # Set a single value by key.
1137
- # @param key [String,Symbol] The key to use to retrieve the value from
1138
- # the execution context.
1139
- # @param value [Object] The value to set.
1140
- # @return [self]
1141
- #
1142
- # @overload static(hash)
1143
- # Set multiple keys and values
1144
- # @param hash [Hash] The keys and values to set
1145
- # @return [self]
1146
- #
1147
- def static(key, value = nil)
1148
- # Source available in the toys-core gem
1149
- end
1150
-
1151
- ##
1152
- # Set option values statically without creating helper methods.
1153
- #
1154
- # ### Example
1155
- #
1156
- # tool "hello" do
1157
- # set :greeting, "Hi there"
1158
- # def run
1159
- # puts "#{get(:greeting)}, world!"
1160
- # end
1161
- # end
1162
- #
1163
- # @overload set(key, value)
1164
- # Set a single value by key.
1165
- # @param key [String,Symbol] The key to use to retrieve the value from
1166
- # the execution context.
1167
- # @param value [Object] The value to set.
1168
- # @return [self]
1169
- #
1170
- # @overload set(hash)
1171
- # Set multiple keys and values
1172
- # @param hash [Hash] The keys and values to set
1173
- # @return [self]
1174
- #
1175
- def set(key, value = nil)
1176
- # Source available in the toys-core gem
1177
- end
1178
-
1179
- ##
1180
- # Enforce that all flags must be provided before any positional args.
1181
- # That is, as soon as the first positional arg appears in the command
1182
- # line arguments, flag parsing is disabled as if `--` had appeared.
1183
- #
1184
- # Issuing this directive by itself turns on enforcement. You may turn it
1185
- # off by passsing `false` as the parameter.
1186
- #
1187
- # @param state [Boolean]
1188
- # @return [self]
1189
- #
1190
- def enforce_flags_before_args(state = true)
1191
- # Source available in the toys-core gem
1192
- end
1193
-
1194
- ##
1195
- # Require that flags must match exactly. That is, flags must appear in
1196
- # their entirety on the command line. (If false, substrings of flags are
1197
- # accepted as long as they are unambiguous.)
1198
- #
1199
- # Issuing this directive by itself turns on exact match. You may turn it
1200
- # off by passsing `false` as the parameter.
1201
- #
1202
- # @param state [Boolean]
1203
- # @return [self]
1204
- #
1205
- def require_exact_flag_match(state = true)
1206
- # Source available in the toys-core gem
1207
- end
1208
-
1209
- ##
1210
- # Disable argument parsing for this tool. Arguments will not be parsed
1211
- # and the options will not be populated. Instead, tools can retrieve the
1212
- # full unparsed argument list by calling {Toys::Context#args}.
1213
- #
1214
- # This directive is mutually exclusive with any of the directives that
1215
- # declare arguments or flags.
1216
- #
1217
- # ### Example
1218
- #
1219
- # tool "mytool" do
1220
- # disable_argument_parsing
1221
- # def run
1222
- # puts "Arguments passed: #{args}"
1223
- # end
1224
- # end
1225
- #
1226
- # @return [self]
1227
- #
1228
- def disable_argument_parsing
1229
- # Source available in the toys-core gem
1230
- end
1231
-
1232
- ##
1233
- # Mark one or more flags as disabled, preventing their use by any
1234
- # subsequent flag definition. This can be used to prevent middleware from
1235
- # defining a particular flag.
1236
- #
1237
- # ### Example
1238
- #
1239
- # This tool does not support the `-v` and `-q` short forms for the two
1240
- # verbosity flags (although it still supports the long forms `--verbose`
1241
- # and `--quiet`.)
1242
- #
1243
- # tool "mytool" do
1244
- # disable_flag "-v", "-q"
1245
- # def run
1246
- # # ...
1247
- # end
1248
- # end
1249
- #
1250
- # @param flags [String...] The flags to disable
1251
- # @return [self]
1252
- #
1253
- def disable_flag(*flags)
1254
- # Source available in the toys-core gem
1255
- end
1256
-
1257
- ##
1258
- # Set the shell completion strategy for this tool's arguments.
1259
- # You can pass one of the following:
1260
- #
1261
- # * The string name of a completion defined in this tool or any of its
1262
- # its ancestors.
1263
- # * A hash of options to pass to the constructor of
1264
- # {Toys::ToolDefinition::DefaultCompletion}.
1265
- # * `nil` or `:default` to select the standard completion strategy
1266
- # (which is {Toys::ToolDefinition::DefaultCompletion} with no extra
1267
- # options).
1268
- # * Any other specification recognized by {Toys::Completion.create}.
1269
- #
1270
- # ### Example
1271
- #
1272
- # The namespace "foo" supports completion only of subtool names. It does
1273
- # not complete the standard flags (like --help).
1274
- #
1275
- # tool "foo" do
1276
- # complete_tool_args complete_args: false, complete_flags: false,
1277
- # complete_flag_values: false
1278
- # tool "bar" do
1279
- # def run
1280
- # puts "in foo bar"
1281
- # end
1282
- # end
1283
- # end
1284
- #
1285
- # @param spec [Object]
1286
- # @param options [Hash]
1287
- # @param block [Proc]
1288
- # @return [self]
1289
- #
1290
- def complete_tool_args(spec = nil, **options, &block)
1291
- # Source available in the toys-core gem
1292
- end
1293
-
1294
- ##
1295
- # Specify how to run this tool.
1296
- #
1297
- # Typically the entrypoint for a tool is a method named `run`. However,
1298
- # you can change this by passing a different method name, as a symbol, to
1299
- # {#to_run}.
1300
- #
1301
- # You can also alternatively pass a block to {#to_run}. You might do this
1302
- # if your method needs access to local variables in the lexical scope.
1303
- # However, it is often more convenient to use {#static} to set those
1304
- # values in the context.
1305
- #
1306
- # ### Examples
1307
- #
1308
- # # Set a different method name as the entrypoint:
1309
- #
1310
- # tool "foo" do
1311
- # to_run :foo
1312
- # def foo
1313
- # puts "The foo tool ran!"
1314
- # end
1315
- # end
1316
- #
1317
- # # Use a block to retain access to the enclosing lexical scope from
1318
- # # the run method:
1319
- #
1320
- # tool "foo" do
1321
- # cur_time = Time.now
1322
- # to_run do
1323
- # puts "The time at tool definition was #{cur_time}"
1324
- # end
1325
- # end
1326
- #
1327
- # # But the following is approximately equivalent:
1328
- #
1329
- # tool "foo" do
1330
- # static :cur_time, Time.now
1331
- # def run
1332
- # puts "The time at tool definition was #{cur_time}"
1333
- # end
1334
- # end
1335
- #
1336
- # @param handler [Proc,Symbol,nil] The run handler as a method name
1337
- # symbol or a proc, or nil to explicitly set as non-runnable.
1338
- # @param block [Proc] The run handler as a block.
1339
- # @return [self]
1340
- #
1341
- def to_run(handler = nil, &block)
1342
- # Source available in the toys-core gem
1343
- end
1344
- alias on_run to_run
1345
-
1346
- ##
1347
- # Specify how to handle interrupts.
1348
- #
1349
- # You can provide either a block to be called, a Proc to be called, or
1350
- # the name of a method to be called. In each case, the block, Proc, or
1351
- # method can optionally take one argument, the Interrupt exception that
1352
- # was raised.
1353
- #
1354
- # Note: this is equivalent to `on_signal("SIGINT")`.
1355
- #
1356
- # ### Example
1357
- #
1358
- # tool "foo" do
1359
- # def run
1360
- # sleep 10
1361
- # end
1362
- # on_interrupt do
1363
- # puts "I was interrupted."
1364
- # end
1365
- # end
1366
- #
1367
- # @param handler [Proc,Symbol,nil] The interrupt callback proc or method
1368
- # name. Pass nil to disable interrupt handling.
1369
- # @param block [Proc] The interrupt callback as a block.
1370
- # @return [self]
1371
- #
1372
- def on_interrupt(handler = nil, &block)
1373
- # Source available in the toys-core gem
1374
- end
1375
-
1376
- ##
1377
- # Specify how to handle the given signal.
1378
- #
1379
- # You can provide either a block to be called, a Proc to be called, or
1380
- # the name of a method to be called. In each case, the block, Proc, or
1381
- # method can optionally take one argument, the SignalException that was
1382
- # raised.
1383
- #
1384
- # ### Example
1385
- #
1386
- # tool "foo" do
1387
- # def run
1388
- # sleep 10
1389
- # end
1390
- # on_signal("QUIT") do |e|
1391
- # puts "Signal caught: #{e.signm}."
1392
- # end
1393
- # end
1394
- #
1395
- # @param signal [Integer,String,Symbol] The signal name or number
1396
- # @param handler [Proc,Symbol,nil] The signal callback proc or method
1397
- # name. Pass nil to disable signal handling.
1398
- # @param block [Proc] The signal callback as a block.
1399
- # @return [self]
1400
- #
1401
- def on_signal(signal, handler = nil, &block)
1402
- # Source available in the toys-core gem
1403
- end
1404
-
1405
- ##
1406
- # Specify how to handle usage errors.
1407
- #
1408
- # You can provide either a block to be called, a Proc to be called, or
1409
- # the name of a method to be called. In each case, the block, Proc, or
1410
- # method can optionally take one argument, the array of usage errors
1411
- # reported.
1412
- #
1413
- # ### Example
1414
- #
1415
- # This tool runs even if a usage error is encountered, by setting the
1416
- # `run` method as the usage error handler.
1417
- #
1418
- # tool "foo" do
1419
- # def run
1420
- # puts "Errors: #{usage_errors.join("\n")}"
1421
- # end
1422
- # on_usage_error :run
1423
- # end
1424
- #
1425
- # @param handler [Proc,Symbol,nil] The interrupt callback proc or method
1426
- # name. Pass nil to disable interrupt handling.
1427
- # @param block [Proc] The interrupt callback as a block.
1428
- # @return [self]
1429
- #
1430
- def on_usage_error(handler = nil, &block)
1431
- # Source available in the toys-core gem
1432
- end
1433
-
1434
- ##
1435
- # Specify that the given module should be mixed into this tool, and its
1436
- # methods made available when running the tool.
1437
- #
1438
- # You can provide either a module, the string name of a mixin that you
1439
- # have defined in this tool or one of its ancestors, or the symbol name
1440
- # of a well-known mixin.
1441
- #
1442
- # ### Example
1443
- #
1444
- # Include the well-known mixin `:terminal` and perform some terminal
1445
- # magic.
1446
- #
1447
- # tool "spin" do
1448
- # include :terminal
1449
- # def run
1450
- # # The spinner method is defined by the :terminal mixin.
1451
- # spinner(leading_text: "Waiting...", final_text: "\n") do
1452
- # sleep 5
1453
- # end
1454
- # end
1455
- # end
1456
- #
1457
- # @param mixin [Module,Symbol,String] Module or module name.
1458
- # @param args [Object...] Arguments to pass to the initializer
1459
- # @param kwargs [keywords] Keyword arguments to pass to the initializer
1460
- # @return [self]
1461
- #
1462
- def include(mixin, *args, **kwargs)
1463
- # Source available in the toys-core gem
1464
- end
1465
-
1466
- ##
1467
- # Determine if the given module/mixin has already been included.
1468
- #
1469
- # You can provide either a module, the string name of a mixin that you
1470
- # have defined in this tool or one of its ancestors, or the symbol name
1471
- # of a well-known mixin.
1472
- #
1473
- # @param mod [Module,Symbol,String] Module or module name.
1474
- #
1475
- # @return [Boolean] Whether the mixin is included
1476
- # @return [nil] if the current tool is not active.
1477
- #
1478
- def include?(mod)
1479
- # Source available in the toys-core gem
1480
- end
1481
-
1482
- ##
1483
- # Return the current source info object.
1484
- #
1485
- # @return [Toys::SourceInfo] Source info.
1486
- #
1487
- def source_info
1488
- # Source available in the toys-core gem
1489
- end
1490
-
1491
- ##
1492
- # Find the given data path (file or directory).
1493
- #
1494
- # Data directories are a convenient place to put images, archives, keys,
1495
- # or other such static data needed by your tools. Data files are located
1496
- # in a directory called `.data` inside a Toys directory. This directive
1497
- # locates a data file during tool definition.
1498
- #
1499
- # ### Example
1500
- #
1501
- # This tool reads its description from a text file in the `.data`
1502
- # directory.
1503
- #
1504
- # tool "mytool" do
1505
- # path = find_data("mytool-desc.txt", type: :file)
1506
- # desc IO.read(path) if path
1507
- # def run
1508
- # # ...
1509
- # end
1510
- # end
1511
- #
1512
- # @param path [String] The path to find
1513
- # @param type [nil,:file,:directory] Type of file system object to find.
1514
- # Default is `nil`, indicating any type.
1515
- #
1516
- # @return [String] Absolute path of the data.
1517
- # @return [nil] if the given data path is not found.
1518
- #
1519
- def find_data(path, type: nil)
1520
- # Source available in the toys-core gem
1521
- end
1522
-
1523
- ##
1524
- # Return the context directory for this tool. Generally, this defaults
1525
- # to the directory containing the toys config directory structure being
1526
- # read, but it may be changed by setting a different context directory
1527
- # for the tool.
1528
- #
1529
- # @return [String] Context directory path
1530
- # @return [nil] if there is no context.
1531
- #
1532
- def context_directory
1533
- # Source available in the toys-core gem
1534
- end
1535
-
1536
- ##
1537
- # Return the current tool config. This object can be queried to determine
1538
- # such information as the name, but it should not be altered.
1539
- #
1540
- # @return [Toys::ToolDefinition]
1541
- #
1542
- def current_tool
1543
- # Source available in the toys-core gem
1544
- end
1545
-
1546
- ##
1547
- # Set a custom context directory for this tool.
1548
- #
1549
- # @param dir [String] Context directory
1550
- # @return [self]
1551
- #
1552
- def set_context_directory(dir)
1553
- # Source available in the toys-core gem
1554
- end
1555
-
1556
- ##
1557
- # Applies the given block to all subtools, recursively. Effectively, the
1558
- # given block is run at the *end* of every tool block. This can be used,
1559
- # for example, to provide some shared configuration for all tools.
1560
- #
1561
- # The block is applied only to subtools defined *after* the block
1562
- # appears. Subtools defined before the block appears are not affected.
1563
- #
1564
- # ### Example
1565
- #
1566
- # It is common for tools to use the `:exec` mixin to invoke external
1567
- # programs. This example automatically includes the exec mixin in all
1568
- # subtools, recursively, so you do not have to repeat the `include`
1569
- # directive in every tool.
1570
- #
1571
- # # .toys.rb
1572
- #
1573
- # subtool_apply do
1574
- # # Include the mixin only if the tool hasn't already done so
1575
- # unless include?(:exec)
1576
- # include :exec, exit_on_nonzero_status: true
1577
- # end
1578
- # end
1579
- #
1580
- # tool "foo" do
1581
- # def run
1582
- # # This tool has access to methods defined by the :exec mixin
1583
- # # because the above block is applied to the tool.
1584
- # sh "echo hello"
1585
- # end
1586
- # end
1587
- #
1588
- def subtool_apply(&block)
1589
- # Source available in the toys-core gem
1590
- end
1591
-
1592
- ##
1593
- # Remove lower-priority sources from the load path. This prevents lower-
1594
- # priority sources (such as Toys files from parent or global directories)
1595
- # from executing or defining tools.
1596
- #
1597
- # This works only if no such sources have already loaded yet.
1598
- #
1599
- # @raise [Toys::ToolDefinitionError] if any lower-priority tools have
1600
- # already been loaded.
1601
- #
1602
- def truncate_load_path!
1603
- # Source available in the toys-core gem
1604
- end
1605
-
1606
- ##
1607
- # Get the settings for this tool.
1608
- #
1609
- # @return [Toys::ToolDefinition::Settings] Tool-specific settings.
1610
- #
1611
- def settings
1612
- # Source available in the toys-core gem
1613
- end
1614
-
1615
- ##
1616
- # Determines whether the current Toys version satisfies the given
1617
- # requirements.
1618
- #
1619
- # @return [Boolean] whether or not the requirements are satisfied
1620
- #
1621
- def toys_version?(*requirements)
1622
- # Source available in the toys-core gem
1623
- end
1624
-
1625
- ##
1626
- # Asserts that the current Toys version against the given requirements,
1627
- # raising an exception if not.
1628
- #
1629
- # @return [self]
1630
- #
1631
- # @raise [Toys::ToolDefinitionError] if the current Toys version does not
1632
- # satisfy the requirements.
1633
- #
1634
- def toys_version!(*requirements)
1635
- # Source available in the toys-core gem
1636
- end
1637
- end
1638
- end
1639
- end