Class: Toys::Tool
- Inherits:
-
Object
- Object
- Toys::Tool
- Defined in:
- lib/toys/tool.rb
Overview
This class manages the object context in effect during the execution of a tool. The context is a hash of key-value pairs.
Flags and arguments defined by your tool normally report their values in the context, using keys that are strings or symbols.
Keys that are neither strings nor symbols are by convention used for other context information, including:
- Common information such as the Definition::Tool object being executed, the arguments originally passed to it, or the usage error string. These well-known keys can be accessed via constants in the Keys module.
- Common settings such as the verbosity level, and whether to exit immediately if a subprocess exits with a nonzero result. These keys are also present as Context constants.
- Private information used internally by middleware and mixins.
This class provides convenience accessors for common keys and settings, and you can retrieve argument-set keys using the #options hash.
Defined Under Namespace
Modules: Keys
Class Method Summary collapse
-
.exit(code = 0) ⇒ Object
Exit immediately with the given status code.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get)
Return an option or other piece of data by key.
-
#[]=(key, value) ⇒ Object
Set an option or other piece of context data by key.
-
#args ⇒ Array[String]
Return the raw arguments passed to the tool, as an array of strings.
-
#binary_name ⇒ String
Return the name of the binary that was executed.
-
#cli ⇒ Toys::CLI
Return the currently running CLI.
-
#exit(code = 0) ⇒ Object
Exit immediately with the given status code.
-
#loader ⇒ Toys::Loader
Return the active loader that can be used to get other tools.
-
#logger ⇒ Logger
Return the logger for this execution.
-
#options ⇒ Hash
Returns the subset of the context that uses string or symbol keys.
-
#set(key, value = nil) ⇒ Object
Set an option or other piece of context data by key.
-
#tool_definition ⇒ Toys::Definition::Tool
Return the tool being executed.
-
#tool_name ⇒ Array[String]
Return the name of the tool being executed, as an array of strings.
-
#usage_error ⇒ String?
Return any usage error detected during argument parsing, or
nilif no error was detected. -
#verbosity ⇒ Integer
Return the current verbosity setting as an integer.
Class Method Details
.exit(code = 0) ⇒ Object
Exit immediately with the given status code
278 279 280 |
# File 'lib/toys/tool.rb', line 278 def self.exit(code = 0) throw :result, code end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
Return an option or other piece of data by key.
218 219 220 |
# File 'lib/toys/tool.rb', line 218 def [](key) @__data[key] end |
#[]=(key, value) ⇒ Object
Set an option or other piece of context data by key.
229 230 231 |
# File 'lib/toys/tool.rb', line 229 def []=(key, value) @__data[key] = value end |
#args ⇒ Array[String]
Return the raw arguments passed to the tool, as an array of strings. This does not include the tool name itself.
175 176 177 |
# File 'lib/toys/tool.rb', line 175 def args @__data[Keys::ARGS] end |
#binary_name ⇒ String
Return the name of the binary that was executed.
208 209 210 |
# File 'lib/toys/tool.rb', line 208 def binary_name @__data[Keys::BINARY_NAME] end |
#cli ⇒ Toys::CLI
Return the currently running CLI.
142 143 144 |
# File 'lib/toys/tool.rb', line 142 def cli @__data[Keys::CLI] end |
#exit(code = 0) ⇒ Object
Exit immediately with the given status code
268 269 270 |
# File 'lib/toys/tool.rb', line 268 def exit(code = 0) throw :result, code end |
#loader ⇒ Toys::Loader
Return the active loader that can be used to get other tools.
200 201 202 |
# File 'lib/toys/tool.rb', line 200 def loader @__data[Keys::LOADER] end |
#logger ⇒ Logger
Return the logger for this execution.
192 193 194 |
# File 'lib/toys/tool.rb', line 192 def logger @__data[Keys::LOGGER] end |
#options ⇒ Hash
Returns 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.
256 257 258 259 260 |
# File 'lib/toys/tool.rb', line 256 def @__data.select do |k, _v| k.is_a?(::Symbol) || k.is_a?(::String) end end |
#set(key, value = nil) ⇒ Object
Set an option or other piece of context data by key.
239 240 241 242 243 244 245 246 |
# File 'lib/toys/tool.rb', line 239 def set(key, value = nil) if key.is_a?(::Hash) @__data.merge!(key) else @__data[key] = value end self end |
#tool_definition ⇒ Toys::Definition::Tool
Return the tool being executed.
158 159 160 |
# File 'lib/toys/tool.rb', line 158 def tool_definition @__data[Keys::TOOL_DEFINITION] end |
#tool_name ⇒ Array[String]
Return the name of the tool being executed, as an array of strings.
166 167 168 |
# File 'lib/toys/tool.rb', line 166 def tool_name @__data[Keys::TOOL_NAME] end |
#usage_error ⇒ String?
Return any usage error detected during argument parsing, or nil if
no error was detected.
184 185 186 |
# File 'lib/toys/tool.rb', line 184 def usage_error @__data[Keys::USAGE_ERROR] end |
#verbosity ⇒ Integer
Return the current verbosity setting as an integer.
150 151 152 |
# File 'lib/toys/tool.rb', line 150 def verbosity @__data[Keys::VERBOSITY] end |