Class: Toys::Context
- Inherits:
-
Object
- Object
- Toys::Context
- Defined in:
- core-docs/toys/context.rb
Overview
This is the base class for tool execution. It represents self when your
tool's methods (such as run) are called, and it defines the methods that
can be called by your tool (such as #logger and #exit.)
This class also manages the "data" available to your tool when it runs. This data is a hash of key-value pairs. It consists of values set by flags and arguments defined by the tool, plus some "well-known" values such as the logger and verbosity level.
You can obtain a value from the data using the #get method.
Additionally, convenience methods are provided for many of the well-known
keys. For instance, you can call #verbosity to obtain the
value for the key Key::VERBOSITY. Finally, flags and
positional arguments that store their data here will also typically
generate convenience methods. For example, an argument with key :abc will
add a method called abc that you can call to get the value.
By convention, flags and arguments defined by your tool should use strings or symbols as keys. Keys that are not strings or symbols should either be well-known keys such as Key::VERBOSITY, or should be used for internal private information needed by middleware and mixins. The module Key defines a number of well-known keys as constants.
Defined in the toys-core gem
Direct Known Subclasses
Defined Under Namespace
Modules: Key
Class Method Summary collapse
-
.exit(code = 0) ⇒ void
Exit immediately with the given status code.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get, #__get)
Fetch an option or other piece of data by key.
-
#[]=(key, value) ⇒ Object
Set an option or other piece of context data by key.
-
#__exit ⇒ void
Exit immediately with the given status code.
-
#args ⇒ Array<String>
(also: #__args)
The raw arguments passed to the tool, as an array of strings.
-
#cli ⇒ Toys::CLI?
(also: #__cli)
The currently running CLI.
-
#context_directory ⇒ String
(also: #__context_directory)
Return the effective context directory for this tool run.
-
#exit(code = 0) ⇒ void
Exit immediately with the given status code.
-
#find_data(path, type: nil) ⇒ String?
(also: #__find_data)
Find the given data file or directory in this tool's search path.
-
#loader ⇒ Toys::Loader
(also: #__loader)
The loader that loaded the tool being executed.
-
#logger ⇒ Logger
(also: #__logger)
The logger for this execution.
-
#options ⇒ Hash
(also: #__options)
The subset of the context that uses string or symbol keys.
-
#runner ⇒ Toys::Runner
(also: #__runner)
The runner that is running the tool.
-
#set(key, value = nil) ⇒ self
(also: #__set)
Set one or more options or other context data by key.
-
#tool_name ⇒ Array<String>
(also: #__tool_name)
The full name of the tool being executed, as an array of strings.
-
#tool_source ⇒ Toys::SourceInfo
(also: #__tool_source)
The source of the tool being executed.
-
#usage_errors ⇒ Array<Toys::ArgParser::UsageError>
(also: #__usage_errors)
The (possibly empty) array of errors detected during argument parsing.
-
#verbosity ⇒ Integer
(also: #__verbosity)
The current verbosity setting as an integer.
Class Method Details
.exit(code = 0) ⇒ void
This method returns an undefined value.
Exit immediately with the given status code. This class method can be called if the instance method is or could be replaced by the tool.
430 431 432 |
# File 'core-docs/toys/context.rb', line 430 def self.exit(code = 0) # Source available in the toys-core gem end |
Instance Method Details
#[](key) ⇒ Object Also known as: get, __get
Fetch an option or other piece of data by key.
If the get method is overridden by the tool, you can still access it
using the name __get or the [] operator.
332 333 334 |
# File 'core-docs/toys/context.rb', line 332 def [](key) # Source available in the toys-core gem end |
#[]=(key, value) ⇒ Object
Set an option or other piece of context data by key.
344 345 346 |
# File 'core-docs/toys/context.rb', line 344 def []=(key, value) # Source available in the toys-core gem end |
#__exit ⇒ void
This method returns an undefined value.
Exit immediately with the given status code.
If the exit method is overridden by the tool, you can still access it
using the name __exit or by calling exit.
Source available in the toys-core gem
419 420 421 |
# File 'core-docs/toys/context.rb', line 419 def exit(code = 0) # Source available in the toys-core gem end |
#args ⇒ Array<String> Also known as: __args
The raw arguments passed to the tool, as an array of strings. This does not include the tool name itself.
This is a convenience getter for Toys::Context::Key::ARGS.
If the args method is overridden by the tool, you can still access it
using the name __args.
176 177 178 |
# File 'core-docs/toys/context.rb', line 176 def args # Source available in the toys-core gem end |
#cli ⇒ Toys::CLI? Also known as: __cli
The currently running CLI.
This is a convenience getter for Toys::Context::Key::CLI. Note the value could be nil if no CLI is present during the execution.
If the cli method is overridden by the tool, you can still access it
using the name __cli.
192 193 194 |
# File 'core-docs/toys/context.rb', line 192 def cli # Source available in the toys-core gem end |
#context_directory ⇒ String Also known as: __context_directory
Return the effective context directory for this tool run. Generally, this is set to the directory containing the toys tool directory structure being read, or it may have been set by the tool definition itself. If a context directory has not been set explicitly, returns the current working directory. Will not return nil.
This is a convenience getter for Toys::Context::Key::CONTEXT_DIRECTORY.
If the context_directory method is overridden by the tool, you can
still access it using the name __context_directory.
211 212 213 |
# File 'core-docs/toys/context.rb', line 211 def context_directory # Source available in the toys-core gem end |
#exit(code = 0) ⇒ void
This method returns an undefined value.
Exit immediately with the given status code.
If the exit method is overridden by the tool, you can still access it
using the name __exit or by calling exit.
416 417 418 |
# File 'core-docs/toys/context.rb', line 416 def exit(code = 0) # Source available in the toys-core gem end |
#find_data(path, type: nil) ⇒ String? Also known as: __find_data
Find the given data file or directory in this tool's search path.
If the find_data method is overridden by the tool, you can still access
it using the name __find_data.
401 402 403 |
# File 'core-docs/toys/context.rb', line 401 def find_data(path, type: nil) # Source available in the toys-core gem end |
#loader ⇒ Toys::Loader Also known as: __loader
The loader that loaded the tool being executed. It can be used to look up and load other tools.
This is a convenience getter for Toys::Context::Key::LOADER.
If the loader method is overridden by the tool, you can still access it
using the name __loader.
227 228 229 |
# File 'core-docs/toys/context.rb', line 227 def loader # Source available in the toys-core gem end |
#logger ⇒ Logger Also known as: __logger
The logger for this execution.
This is a convenience getter for Toys::Context::Key::LOGGER.
If the logger method is overridden by the tool, you can still access it
using the name __logger.
242 243 244 |
# File 'core-docs/toys/context.rb', line 242 def logger # Source available in the toys-core gem end |
#options ⇒ Hash Also known as: __options
The subset of the context that uses string or symbol keys. By convention, this includes keys that are set by tool flags and arguments, but does not include well-known context values such as verbosity or private context values used by middleware or mixins.
If the options method is overridden by the tool, you can still access
it using the name __options.
383 384 385 |
# File 'core-docs/toys/context.rb', line 383 def # Source available in the toys-core gem end |
#runner ⇒ Toys::Runner Also known as: __runner
The runner that is running the tool. It can be used to run other tools in the same process.
This is a convenience getter for Toys::Context::Key::RUNNER.
If the runner method is overridden by the tool, you can still access it
using the name __runner.
258 259 260 |
# File 'core-docs/toys/context.rb', line 258 def runner # Source available in the toys-core gem end |
#set(key, value) ⇒ self #set(hash) ⇒ self Also known as: __set
Set one or more options or other context data by key.
If the set method is overridden by the tool, you can still access it
using the name __set.
367 368 369 |
# File 'core-docs/toys/context.rb', line 367 def set(key, value = nil) # Source available in the toys-core gem end |
#tool_name ⇒ Array<String> Also known as: __tool_name
The full name of the tool being executed, as an array of strings.
This is a convenience getter for Toys::Context::Key::TOOL_NAME.
If the tool_name method is overridden by the tool, you can still access
it using the name __tool_name.
273 274 275 |
# File 'core-docs/toys/context.rb', line 273 def tool_name # Source available in the toys-core gem end |
#tool_source ⇒ Toys::SourceInfo Also known as: __tool_source
The source of the tool being executed.
This is a convenience getter for Toys::Context::Key::TOOL_SOURCE.
If the tool_source method is overridden by the tool, you can still
access it using the name __tool_source.
288 289 290 |
# File 'core-docs/toys/context.rb', line 288 def tool_source # Source available in the toys-core gem end |
#usage_errors ⇒ Array<Toys::ArgParser::UsageError> Also known as: __usage_errors
The (possibly empty) array of errors detected during argument parsing.
This is a convenience getter for Toys::Context::Key::USAGE_ERRORS.
If the usage_errors method is overridden by the tool, you can still
access it using the name __usage_errors.
303 304 305 |
# File 'core-docs/toys/context.rb', line 303 def usage_errors # Source available in the toys-core gem end |
#verbosity ⇒ Integer Also known as: __verbosity
The current verbosity setting as an integer.
This is a convenience getter for Toys::Context::Key::VERBOSITY.
If the verbosity method is overridden by the tool, you can still access
it using the name __verbosity.
318 319 320 |
# File 'core-docs/toys/context.rb', line 318 def verbosity # Source available in the toys-core gem end |