toys-core 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +98 -0
  3. data/LICENSE.md +16 -24
  4. data/README.md +307 -59
  5. data/docs/guide.md +44 -4
  6. data/lib/toys-core.rb +58 -49
  7. data/lib/toys/acceptor.rb +672 -0
  8. data/lib/toys/alias.rb +106 -0
  9. data/lib/toys/arg_parser.rb +624 -0
  10. data/lib/toys/cli.rb +422 -181
  11. data/lib/toys/compat.rb +83 -0
  12. data/lib/toys/completion.rb +442 -0
  13. data/lib/toys/context.rb +354 -0
  14. data/lib/toys/core_version.rb +18 -26
  15. data/lib/toys/dsl/flag.rb +213 -56
  16. data/lib/toys/dsl/flag_group.rb +237 -51
  17. data/lib/toys/dsl/positional_arg.rb +210 -0
  18. data/lib/toys/dsl/tool.rb +968 -317
  19. data/lib/toys/errors.rb +46 -28
  20. data/lib/toys/flag.rb +821 -0
  21. data/lib/toys/flag_group.rb +282 -0
  22. data/lib/toys/input_file.rb +18 -26
  23. data/lib/toys/loader.rb +110 -100
  24. data/lib/toys/middleware.rb +24 -31
  25. data/lib/toys/mixin.rb +90 -59
  26. data/lib/toys/module_lookup.rb +125 -0
  27. data/lib/toys/positional_arg.rb +184 -0
  28. data/lib/toys/source_info.rb +192 -0
  29. data/lib/toys/standard_middleware/add_verbosity_flags.rb +38 -43
  30. data/lib/toys/standard_middleware/handle_usage_errors.rb +39 -40
  31. data/lib/toys/standard_middleware/set_default_descriptions.rb +111 -89
  32. data/lib/toys/standard_middleware/show_help.rb +130 -113
  33. data/lib/toys/standard_middleware/show_root_version.rb +29 -35
  34. data/lib/toys/standard_mixins/exec.rb +116 -78
  35. data/lib/toys/standard_mixins/fileutils.rb +16 -24
  36. data/lib/toys/standard_mixins/gems.rb +29 -30
  37. data/lib/toys/standard_mixins/highline.rb +34 -41
  38. data/lib/toys/standard_mixins/terminal.rb +72 -26
  39. data/lib/toys/template.rb +51 -35
  40. data/lib/toys/tool.rb +1161 -206
  41. data/lib/toys/utils/completion_engine.rb +171 -0
  42. data/lib/toys/utils/exec.rb +279 -182
  43. data/lib/toys/utils/gems.rb +58 -49
  44. data/lib/toys/utils/help_text.rb +117 -111
  45. data/lib/toys/utils/terminal.rb +69 -62
  46. data/lib/toys/wrappable_string.rb +162 -0
  47. metadata +24 -22
  48. data/lib/toys/definition/acceptor.rb +0 -191
  49. data/lib/toys/definition/alias.rb +0 -112
  50. data/lib/toys/definition/arg.rb +0 -140
  51. data/lib/toys/definition/flag.rb +0 -370
  52. data/lib/toys/definition/flag_group.rb +0 -205
  53. data/lib/toys/definition/source_info.rb +0 -190
  54. data/lib/toys/definition/tool.rb +0 -842
  55. data/lib/toys/dsl/arg.rb +0 -132
  56. data/lib/toys/runner.rb +0 -188
  57. data/lib/toys/standard_middleware.rb +0 -47
  58. data/lib/toys/utils/module_lookup.rb +0 -135
  59. data/lib/toys/utils/wrappable_string.rb +0 -165
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2018 Daniel Azuma
4
- #
5
- # All rights reserved.
6
- #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
9
- #
10
- # * Redistributions of source code must retain the above copyright notice,
11
- # this list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the copyright holder, nor the names of any other
16
- # contributors to this software, may be used to endorse or promote products
17
- # derived from this software without specific prior written permission.
18
- #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
- # POSSIBILITY OF SUCH DAMAGE.
30
- ;
31
-
32
- module Toys
33
- module Utils
34
- ##
35
- # A string intended to be wrapped.
36
- #
37
- class WrappableString
38
- ##
39
- # Create a wrapped string.
40
- # @param [String,Array<String>] string The string or array of string
41
- # fragments
42
- #
43
- def initialize(string = "")
44
- @fragments = string.is_a?(::Array) ? string.map(&:to_s) : string.to_s.split
45
- end
46
-
47
- ##
48
- # Returns the fragments.
49
- # @return [Array<String>]
50
- #
51
- attr_reader :fragments
52
-
53
- ##
54
- # Concatenates this WrappableString with another WrappableString
55
- # @param [WrappableString] other
56
- #
57
- def +(other)
58
- other = WrappableString.new(other) unless other.is_a?(WrappableString)
59
- WrappableString.new(fragments + other.fragments)
60
- end
61
-
62
- ##
63
- # Returns true if the string is empty (i.e. has no fragments)
64
- # @return [String]
65
- #
66
- def empty?
67
- @fragments.empty?
68
- end
69
-
70
- ##
71
- # Returns the string without any wrapping
72
- # @return [String]
73
- #
74
- def to_s
75
- @fragments.join(" ")
76
- end
77
- alias string to_s
78
-
79
- ## @private
80
- def ==(other)
81
- return false unless other.is_a?(WrappableString)
82
- other.fragments == fragments
83
- end
84
- alias eql? ==
85
-
86
- ## @private
87
- def hash
88
- fragments.hash
89
- end
90
-
91
- ##
92
- # Wraps the string to the given width.
93
- #
94
- # @param [Integer,nil] width Width in characters, or `nil` for infinite.
95
- # @param [Integer,nil] width2 Width in characters for the second and
96
- # subsequent lines, or `nil` to use the same as width.
97
- # @return [Array<String>] Wrapped lines
98
- #
99
- def wrap(width, width2 = nil)
100
- lines = []
101
- line = ""
102
- line_len = 0
103
- fragments.each do |frag|
104
- frag_len = Utils::Terminal.remove_style_escapes(frag).size
105
- if line_len.zero?
106
- line = frag
107
- line_len = frag_len
108
- elsif width && line_len + 1 + frag_len > width
109
- lines << line
110
- line = frag
111
- line_len = frag_len
112
- width = width2 if width2
113
- else
114
- line_len += frag_len + 1
115
- line = "#{line} #{frag}"
116
- end
117
- end
118
- lines << line if line_len.positive?
119
- lines
120
- end
121
-
122
- ##
123
- # Wraps an array of lines to the given width.
124
- #
125
- # @param [Array<WrappableString>] strs Array of strings to wrap.
126
- # @param [Integer,nil] width Width in characters, or `nil` for infinite.
127
- # @param [Integer,nil] width2 Width in characters for the second and
128
- # subsequent lines, or `nil` to use the same as width.
129
- # @return [Array<String>] Wrapped lines
130
- #
131
- def self.wrap_lines(strs, width, width2 = nil)
132
- result = Array(strs).map do |s|
133
- lines = s.empty? ? [""] : s.wrap(width, width2)
134
- width = width2 if width2
135
- lines
136
- end.flatten
137
- result = [] if result.all?(&:empty?)
138
- result
139
- end
140
-
141
- ##
142
- # Make the given object a WrappableString.
143
- # If the object is already a WrappableString, return it. Otherwise,
144
- # treat it as a string or an array of strings and wrap it in a
145
- # WrappableString.
146
- #
147
- # @param [Toys::Utils::WrappableString,String,Array<String>] obj
148
- # @return [Toys::Utils::WrappableString]
149
- #
150
- def self.make(obj)
151
- obj.is_a?(Utils::WrappableString) ? obj : Utils::WrappableString.new(obj)
152
- end
153
-
154
- ##
155
- # Make the given object an array of WrappableString.
156
- #
157
- # @param [Array<Toys::Utils::WrappableString,String,Array<String>>] objs
158
- # @return [Array<Toys::Utils::WrappableString>]
159
- #
160
- def self.make_array(objs)
161
- Array(objs).map { |obj| make(obj) }
162
- end
163
- end
164
- end
165
- end