toys 0.3.1 → 0.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1dcbd6fabf227072fda60d4acebcc09ae1a8af3d10db580bcea3d4a56a6623fd
4
- data.tar.gz: bdc7a717793c190af3cb0c470cfbd97078779478e1528297397303c88af1b6a3
3
+ metadata.gz: 674d3b61ad2be8749dddafed99ab0ad70fbda515157024afd2fb7b3dc3c1108c
4
+ data.tar.gz: 83f446e5eb534360cbc50f1f032c758d6b457b97a280661887632f1016abd1f5
5
5
  SHA512:
6
- metadata.gz: 74b91bdd53c8ca7aec7ba1b1d5ed9d98e2f3a826846ea9d41975391d504e14047d25f6db9551318568fabb81194dd3bf896183316a50e2fe63f92a4e0811ee9c
7
- data.tar.gz: 81617b1a8aff51c05cae72cc79e4fffd5030af0176f50f389e3e235334161efbbb9f6ca60e5273f15a9a00cf76bbbfc675aec0a906c87110991e760c9ff1e2da
6
+ metadata.gz: f35911821abba0a114aabf8f6e4a1a5bd1d6727edb3ec8e8eb0f035fac8cb08fecb737ff38c561a9f09caddb5f697219c5a349fa6c4ace63db879df35fb33164
7
+ data.tar.gz: 6dafe20e17be9816913edd9c498856f01fc3bac19249c11e62836895fc2234aa7231d2ecd322adc825d58f8d1ab4d0c51c756110831e7ece22b0cdf8a155bff2
data/.yardopts CHANGED
@@ -1,9 +1,7 @@
1
1
  --no-private
2
2
  --title=Toys
3
3
  --markup=markdown
4
- --embed-mixin=ClassMethods
5
4
  --main=README.md
6
- --exclude=lib/toys/builtins
7
5
  ./lib/**/*.rb
8
6
  -
9
7
  README.md
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Release History
2
2
 
3
+ ### 0.3.2 / 2018-05-07
4
+
5
+ * CHANGED: Split core engine out into separate "toys-core" gem. See the
6
+ toys-core changelog for additional changes in core.
7
+ * CHANGED: Tools can no longer be alias_of. However, alias_as still works, and
8
+ it is now possible to create an alias using alias_tool.
9
+ * IMPROVED: The root tool now responds to the "--version" switch.
10
+ * IMPROVED: Group help can now "--search" for subcommands.
11
+ * IMPROVED: Help shows the config file path on "--verbose".
12
+ * IMPROVED: You can now run a sub-instance of toys from an executor.
13
+
14
+ ### 0.3.1 / 2018-05-02
15
+
16
+ * CHANGED: Subcommand display is now recursive by default.
17
+ * IMPROVED: Improved error messaging for bad switch syntax.
18
+ * FIXED: toys system update now reports experimental versions correctly.
19
+ * FIXED: Subtools of an overridden group are now properly deleted.
20
+ * DOCS: Completed a first pass on class and method documentation.
21
+ * INTERNAL: Adjusted naming of switch-related methods.
22
+
3
23
  ### 0.3.0 / 2018-04-30
4
24
 
5
25
  * Initial generally usable release
data/README.md CHANGED
@@ -1,21 +1,10 @@
1
1
  # Toys
2
2
 
3
- [![Travis-CI Build Status](https://travis-ci.org/dazuma/toys.svg)](https://travis-ci.org/dazuma/toys/)
3
+ Toys is a command line binary that lets you build your own suite of command
4
+ line tools (with commands and subcommands) using a Ruby DSL. Commands can be
5
+ defined globally or scoped to directories.
4
6
 
5
- Toys is a Ruby library and command line tool that lets you build your own
6
- command line suite of tools (with commands and subcommands) using a Ruby DSL.
7
- You can define commands globally or configure special commands scoped to
8
- individual directories.
9
-
10
- ## Installation
11
-
12
- This software is distributed as a Ruby gem and may be installed using:
13
-
14
- ```
15
- gem install toys
16
- ```
17
-
18
- ## Usage
7
+ ## Quick Start
19
8
 
20
9
  (TODO)
21
10
 
@@ -29,15 +18,6 @@ guidelines will be provided when the software stabilizes further.
29
18
  The source can be found on Github at
30
19
  [https://github.com/dazuma/toys](https://github.com/dazuma/toys)
31
20
 
32
- ### TODO items
33
-
34
- * Decide about highline integration
35
- * Output formats middleware
36
- * System paths tool
37
- * Search function in group help
38
- * Split out toys-core gem
39
- * Write overall documentation
40
-
41
21
  ## License
42
22
 
43
23
  Copyright 2018 Daniel Azuma
data/bin/toys CHANGED
@@ -32,4 +32,4 @@
32
32
  $LOAD_PATH.unshift(::File.absolute_path(::File.join(::File.dirname(__dir__), "lib")))
33
33
  require "toys"
34
34
 
35
- ::Toys::CLI.create_standard.run(::ARGV)
35
+ exit(::Toys::StandardCLI.create.run(::ARGV))
@@ -31,7 +31,7 @@ desc "Run multiple tools in order"
31
31
 
32
32
  switch(:delim, "-d", "--delim=VALUE", default: ",", doc: "Set the delimiter")
33
33
 
34
- remaining_args(:args)
34
+ remaining_args(:args, doc: "Tools to run")
35
35
 
36
36
  execute do
37
37
  delim = self[:delim]
File without changes
data/lib/toys.rb CHANGED
@@ -34,20 +34,15 @@
34
34
  # individual directories.
35
35
  #
36
36
  module Toys
37
- ##
38
- # Namespace for common utility classes.
39
- #
40
- module Utils; end
41
37
  end
42
38
 
43
- require "toys/cli"
44
- require "toys/config_dsl"
45
- require "toys/context"
46
- require "toys/errors"
47
- require "toys/helpers"
48
- require "toys/loader"
49
- require "toys/middleware"
50
- require "toys/template"
51
- require "toys/templates"
52
- require "toys/tool"
53
39
  require "toys/version"
40
+
41
+ if ::ENV["TOYS_CORE_LIB_PATH"]
42
+ $LOAD_PATH.unshift(::ENV["TOYS_CORE_LIB_PATH"])
43
+ else
44
+ gem "toys-core", "= #{::Toys::VERSION}"
45
+ end
46
+ require "toys-core"
47
+
48
+ require "toys/standard_cli"
@@ -0,0 +1,151 @@
1
+ # Copyright 2018 Daniel Azuma
2
+ #
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ # * Neither the name of the copyright holder, nor the names of any other
14
+ # contributors to this software, may be used to endorse or promote products
15
+ # derived from this software without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ # POSSIBILITY OF SUCH DAMAGE.
28
+ ;
29
+
30
+ require "logger"
31
+
32
+ require "toys/middleware/show_version"
33
+
34
+ module Toys
35
+ ##
36
+ # Helpers that configure the toys-core CLI with the behavior for the
37
+ # standard Toys binary.
38
+ #
39
+ module StandardCLI
40
+ ##
41
+ # Path to standard built-in tools
42
+ # @return [String]
43
+ #
44
+ BUILTINS_PATH = ::File.join(::File.dirname(::File.dirname(__dir__)), "builtins").freeze
45
+
46
+ ##
47
+ # Standard toys configuration directory name
48
+ # @return [String]
49
+ #
50
+ CONFIG_DIR_NAME = ".toys".freeze
51
+
52
+ ##
53
+ # Standard toys configuration file name
54
+ # @return [String]
55
+ #
56
+ CONFIG_FILE_NAME = ".toys.rb".freeze
57
+
58
+ ##
59
+ # Standard index file name in a toys configuration
60
+ # @return [String]
61
+ #
62
+ INDEX_FILE_NAME = ".toys.rb".freeze
63
+
64
+ ##
65
+ # Standard toys preload file name
66
+ # @return [String]
67
+ #
68
+ PRELOAD_FILE_NAME = ".preload.rb".freeze
69
+
70
+ ##
71
+ # Name of standard toys binary
72
+ # @return [String]
73
+ #
74
+ BINARY_NAME = "toys".freeze
75
+
76
+ ##
77
+ # Help text for the standard root tool
78
+ # @return [String]
79
+ #
80
+ DEFAULT_ROOT_DESC =
81
+ "Toys is your personal command line tool. You can add to the list of" \
82
+ " commands below by writing scripts in Ruby using a simple DSL, and" \
83
+ " toys will organize and document them, and make them available" \
84
+ " globally or scoped to specific directories that you choose." \
85
+ " For detailed information, see https://www.rubydoc.info/gems/toys".freeze
86
+
87
+ ##
88
+ # Create a standard CLI, configured with the appropriate paths and
89
+ # middleware.
90
+ #
91
+ # @param [String,nil] directory Starting search directory for configs.
92
+ # Defaults to the current working directory.
93
+ # @return [Toys::CLI]
94
+ #
95
+ def self.create(directory: nil)
96
+ cli = CLI.new(
97
+ binary_name: BINARY_NAME,
98
+ config_dir_name: CONFIG_DIR_NAME,
99
+ config_file_name: CONFIG_FILE_NAME,
100
+ index_file_name: INDEX_FILE_NAME,
101
+ preload_file_name: PRELOAD_FILE_NAME,
102
+ middleware_stack: default_middleware_stack
103
+ )
104
+ add_standard_paths(cli, directory: directory)
105
+ cli
106
+ end
107
+
108
+ ##
109
+ # Add paths for a toys standard CLI. Paths added, in order from high to
110
+ # low priority, are:
111
+ #
112
+ # * Search the current directory and all ancestors for config files and
113
+ # directories.
114
+ # * Read the `TOYS_PATH` environment variable and search for config files
115
+ # and directories in the given paths. If this variable is empty, use
116
+ # `$HOME:/etc` by default.
117
+ # * The builtins for the standard toys binary.
118
+ #
119
+ # @param [String,nil] directory Starting search directory for configs.
120
+ # Defaults to the current working directory.
121
+ # @param [Toys::CLI] cli Add paths to this CLI
122
+ #
123
+ def self.add_standard_paths(cli, directory: nil)
124
+ cli.add_search_path_hierarchy(start: directory)
125
+ paths = ::ENV["TOYS_PATH"].to_s.split(::File::PATH_SEPARATOR)
126
+ if paths.empty?
127
+ paths << ::ENV["HOME"] if ::ENV["HOME"]
128
+ paths << "/etc" if ::File.directory?("/etc") && ::File.readable?("/etc")
129
+ end
130
+ paths.each { |path| cli.add_search_path(path) }
131
+ cli.add_config_path(BUILTINS_PATH)
132
+ cli
133
+ end
134
+
135
+ ##
136
+ # Returns a the middleware for the standard Toys CLI.
137
+ #
138
+ # @return [Array]
139
+ #
140
+ def self.default_middleware_stack
141
+ version_displayer = Middleware::ShowVersion.root_version_displayer(::Toys::VERSION)
142
+ [
143
+ [:set_default_descriptions, default_root_desc: DEFAULT_ROOT_DESC],
144
+ [:handle_usage_errors],
145
+ [:show_version, version_displayer: version_displayer],
146
+ [:show_usage],
147
+ [:add_verbosity_switches]
148
+ ]
149
+ end
150
+ end
151
+ end
data/lib/toys/version.rb CHANGED
@@ -29,8 +29,8 @@
29
29
 
30
30
  module Toys
31
31
  ##
32
- # Current version of Toys
32
+ # Current version of the Toys command line binary
33
33
  # @return [String]
34
34
  #
35
- VERSION = "0.3.1".freeze
35
+ VERSION = "0.3.2".freeze
36
36
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-02 00:00:00.000000000 Z
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: toys-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: minitest
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -93,33 +107,10 @@ files:
93
107
  - LICENSE.md
94
108
  - README.md
95
109
  - bin/toys
110
+ - builtins/do.rb
111
+ - builtins/system.rb
96
112
  - lib/toys.rb
97
- - lib/toys/builtins/do.rb
98
- - lib/toys/builtins/system.rb
99
- - lib/toys/cli.rb
100
- - lib/toys/config_dsl.rb
101
- - lib/toys/context.rb
102
- - lib/toys/errors.rb
103
- - lib/toys/helpers.rb
104
- - lib/toys/helpers/exec.rb
105
- - lib/toys/helpers/file_utils.rb
106
- - lib/toys/loader.rb
107
- - lib/toys/middleware.rb
108
- - lib/toys/middleware/base.rb
109
- - lib/toys/middleware/set_verbosity.rb
110
- - lib/toys/middleware/show_group_usage.rb
111
- - lib/toys/middleware/show_tool_usage.rb
112
- - lib/toys/middleware/show_usage_errors.rb
113
- - lib/toys/template.rb
114
- - lib/toys/templates.rb
115
- - lib/toys/templates/clean.rb
116
- - lib/toys/templates/gem_build.rb
117
- - lib/toys/templates/minitest.rb
118
- - lib/toys/templates/rubocop.rb
119
- - lib/toys/templates/yardoc.rb
120
- - lib/toys/tool.rb
121
- - lib/toys/utils/module_lookup.rb
122
- - lib/toys/utils/usage.rb
113
+ - lib/toys/standard_cli.rb
123
114
  - lib/toys/version.rb
124
115
  homepage: https://github.com/dazuma/toys
125
116
  licenses:
data/lib/toys/cli.rb DELETED
@@ -1,271 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- require "logger"
31
-
32
- module Toys
33
- ##
34
- # A Toys-based CLI.
35
- #
36
- # Use this class to implement a CLI using Toys.
37
- #
38
- class CLI
39
- ##
40
- # Path to default builtins
41
- # @return [String]
42
- #
43
- BUILTINS_PATH = ::File.join(__dir__, "builtins").freeze
44
-
45
- ##
46
- # Default name of toys configuration directory
47
- # @return [String]
48
- #
49
- DEFAULT_DIR_NAME = ".toys".freeze
50
-
51
- ##
52
- # Default name of toys configuration file
53
- # @return [String]
54
- #
55
- DEFAULT_FILE_NAME = ".toys.rb".freeze
56
-
57
- ##
58
- # Default name of toys preload file
59
- # @return [String]
60
- #
61
- DEFAULT_PRELOAD_NAME = ".preload.rb".freeze
62
-
63
- ##
64
- # Default name of toys binary
65
- # @return [String]
66
- #
67
- DEFAULT_BINARY_NAME = "toys".freeze
68
-
69
- ##
70
- # Default help text for the root tool
71
- # @return [String]
72
- #
73
- DEFAULT_ROOT_DESC =
74
- "Toys is your personal command line tool. You can add to the list of" \
75
- " commands below by writing scripts in Ruby using a simple DSL, and" \
76
- " toys will organize and document them, and make them available" \
77
- " globally or scoped to specific directories that you choose." \
78
- " For detailed information, see https://www.rubydoc.info/gems/toys".freeze
79
-
80
- ##
81
- # Create a CLI
82
- #
83
- # @param [String,nil] binary_name The binary name displayed in help text.
84
- # Optional. Defaults to the ruby program name.
85
- # @param [Logger,nil] logger The logger to use. If not provided, a default
86
- # logger that writes to `STDERR` is used.
87
- # @param [String,nil] config_dir_name A directory with this name that
88
- # appears in the loader path, is treated as a configuration directory
89
- # whose contents are loaded into the toys configuration. Optional.
90
- # If not provided, toplevel configuration directories are disabled.
91
- # The default toys CLI sets this to `".toys"`.
92
- # @param [String,nil] config_file_name A file with this name that appears
93
- # in the loader path, is treated as a toplevel configuration file
94
- # whose contents are loaded into the toys configuration. Optional.
95
- # If not provided, toplevel configuration files are disabled.
96
- # The default toys CLI sets this to `".toys.rb"`.
97
- # @param [String,nil] index_file_name A file with this name that appears
98
- # in any configuration directory (not just a toplevel directory) is
99
- # loaded first as a standalone configuration file. If not provided,
100
- # standalone configuration files are disabled.
101
- # The default toys CLI sets this to `".toys.rb"`.
102
- # @param [String,nil] preload_file_name A file with this name that appears
103
- # in any configuration directory (not just a toplevel directory) is
104
- # loaded before any configuration files. It is not treated as a
105
- # configuration file in that the configuration DSL is not honored. You
106
- # may use such a file to define auxiliary Ruby modules and classes that
107
- # used by the tools defined in that directory.
108
- # @param [Array] middleware An array of middleware that will be used by
109
- # default for all tools loaded by this CLI.
110
- # @param [String] root_desc The description of the root tool.
111
- #
112
- def initialize(
113
- binary_name: nil,
114
- logger: nil,
115
- config_dir_name: nil,
116
- config_file_name: nil,
117
- index_file_name: nil,
118
- preload_file_name: nil,
119
- middleware: [],
120
- root_desc: nil
121
- )
122
- logger ||= self.class.default_logger
123
- @loader = Loader.new(
124
- config_dir_name: config_dir_name,
125
- config_file_name: config_file_name,
126
- index_file_name: index_file_name,
127
- preload_file_name: preload_file_name,
128
- middleware: middleware,
129
- root_desc: root_desc
130
- )
131
- @context_base = Context::Base.new(@loader, binary_name, logger)
132
- end
133
-
134
- ##
135
- # Add one or more configuration files/directories to the loader.
136
- #
137
- # If a CLI has a default tool set, it might use this to point to the
138
- # directory that defines those tools. For example, the default Toys CLI
139
- # uses this to load the builtin tools from the `builtins` directory.
140
- #
141
- # @param [String,Array<String>] paths One or more paths to add.
142
- #
143
- def add_config_paths(paths)
144
- @loader.add_config_paths(paths)
145
- self
146
- end
147
-
148
- ##
149
- # Add one or more path directories to the loader. These directories are
150
- # searched for config directories and config files. Typically a CLI may
151
- # include the current directory, or the user's home directory, `/etc` or
152
- # other configuration-centric directories here.
153
- #
154
- # @param [String,Array<String>] paths One or more paths to add.
155
- #
156
- def add_paths(paths)
157
- @loader.add_paths(paths)
158
- self
159
- end
160
-
161
- ##
162
- # Add the given path and all ancestor directories to the loader as paths.
163
- # You may optionally provide a stopping point using the `base` argument,
164
- # which, if present, will be the _last_ directory added.
165
- #
166
- # @param [String] path The first directory to add
167
- # @param [String] base The last directory to add. Defaults to `"/"`.
168
- #
169
- def add_path_hierarchy(path = nil, base = "/")
170
- path ||= ::Dir.pwd
171
- paths = []
172
- loop do
173
- paths << path
174
- break if !base || path == base
175
- next_path = ::File.dirname(path)
176
- break if next_path == path
177
- path = next_path
178
- end
179
- @loader.add_paths(paths)
180
- self
181
- end
182
-
183
- ##
184
- # Add a standard set of paths. This includes the contents of the
185
- # `TOYS_PATH` environment variable if present, the current user's home
186
- # directory, and any system configuration directories such as `/etc`.
187
- #
188
- def add_standard_paths
189
- toys_path = ::ENV["TOYS_PATH"].to_s.split(::File::PATH_SEPARATOR)
190
- if toys_path.empty?
191
- toys_path << ::ENV["HOME"] if ::ENV["HOME"]
192
- toys_path << "/etc" if ::File.directory?("/etc") && ::File.readable?("/etc")
193
- end
194
- @loader.add_paths(toys_path)
195
- self
196
- end
197
-
198
- ##
199
- # Run the CLI with the given command line arguments.
200
- #
201
- def run(*args)
202
- exit(@context_base.run(args.flatten, verbosity: 0))
203
- end
204
-
205
- class << self
206
- ##
207
- # Configure and create the standard Toys CLI.
208
- #
209
- # @return [Toys::CLI]
210
- #
211
- def create_standard
212
- cli = new(
213
- binary_name: DEFAULT_BINARY_NAME,
214
- config_dir_name: DEFAULT_DIR_NAME,
215
- config_file_name: DEFAULT_FILE_NAME,
216
- index_file_name: DEFAULT_FILE_NAME,
217
- preload_file_name: DEFAULT_PRELOAD_NAME,
218
- middleware: default_middleware_stack,
219
- root_desc: DEFAULT_ROOT_DESC
220
- )
221
- cli.add_path_hierarchy
222
- cli.add_standard_paths
223
- cli.add_config_paths(BUILTINS_PATH)
224
- cli
225
- end
226
-
227
- ##
228
- # Returns a default set of middleware used by the standard Toys CLI.
229
- # This middleware handles usage errors, provides a behavior for groups
230
- # that displays the group command list, provides a `--help` option for
231
- # showing individual tool documentation, and provides `--verbose` and
232
- # `--quiet` switches for setting the verbosity, which in turn controls
233
- # the logger level.
234
- #
235
- # @return [Array]
236
- #
237
- def default_middleware_stack
238
- [
239
- Middleware.lookup(:show_usage_errors).new,
240
- Middleware.lookup(:show_group_usage).new,
241
- Middleware.lookup(:show_tool_usage).new,
242
- Middleware.lookup(:set_verbosity).new
243
- ]
244
- end
245
-
246
- ##
247
- # Returns a default logger that logs to `STDERR`.
248
- #
249
- # @return [Logger]
250
- #
251
- def default_logger
252
- logger = ::Logger.new(::STDERR)
253
- logger.formatter = proc do |severity, time, _progname, msg|
254
- msg_str =
255
- case msg
256
- when ::String
257
- msg
258
- when ::Exception
259
- "#{msg.message} (#{msg.class})\n" << (msg.backtrace || []).join("\n")
260
- else
261
- msg.inspect
262
- end
263
- timestr = time.strftime("%Y-%m-%d %H:%M:%S")
264
- format("[%s %5s] %s\n", timestr, severity, msg_str)
265
- end
266
- logger.level = ::Logger::WARN
267
- logger
268
- end
269
- end
270
- end
271
- end