Class: Cliqr::Command::CommandContext Private
- Inherits:
-
Object
- Object
- Cliqr::Command::CommandContext
- Includes:
- Color
- Defined in:
- lib/cliqr/command/command_context.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Manages things like arguments and input/output for a command
Instance Attribute Summary collapse
-
#action_name ⇒ String
private
Name of the current action.
-
#arguments ⇒ Array<String>
private
Command arguments.
-
#base_command ⇒ String
private
Base command name.
-
#command ⇒ String
private
Command name.
-
#environment ⇒ String
readonly
private
Environment type.
Class Method Summary collapse
-
.build(config, parsed_input, options, &executor) ⇒ Cliqr::Command::CommandContext
private
Build a instance of command context based on the parsed set of arguments.
Instance Method Summary collapse
-
#action?(name) ⇒ Boolean
private
Check whether a action is valid in current context.
-
#action_type? ⇒ Boolean
private
Check if the current context if for a action.
-
#ask(question, options) ⇒ Object
private
Run the [Sawaal] selector on a set of options.
-
#bash? ⇒ Boolean
private
Check if running in a bash environment.
-
#forward(args, options = {}) ⇒ Integer
private
Forward a command to the executor.
-
#get_or_check_option(name) ⇒ Object
private
Get option value or check if it exists.
-
#invoke(event_name, *args) ⇒ Boolean
private
Invoke an event.
-
#method_missing(name, *_args, &_block) ⇒ Object
private
Handle the case when a method is invoked to get an option value.
-
#option(name) ⇒ Cliqr::Command::CommandOption
private
Get an option by name.
-
#option?(name) ⇒ Boolean
private
Check if an option with a specified name has been passed.
-
#options ⇒ Array<Cliqr::Command::CommandOption>
private
List of parsed options.
-
#puts(message) ⇒ Nothing
private
Override the default puts implementation to enable output buffering.
-
#root(environment_type = nil) ⇒ Cliqr::Command::CommandContext
private
Transform this context to the root context.
Methods included from Color
#bg_black, #bg_blue, #bg_cyan, #bg_gray, #bg_green, #bg_magenta, #bg_red, #bg_yellow, #black, #blue, #bold, #cyan, #disable_color, #gray, #green, #magenta, #red, #reverse_color, #yellow
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *_args, &_block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Handle the case when a method is invoked to get an option value
141 142 143 |
# File 'lib/cliqr/command/command_context.rb', line 141 def method_missing(name, *_args, &_block) get_or_check_option(name) end |
Instance Attribute Details
#action_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Name of the current action
38 39 40 |
# File 'lib/cliqr/command/command_context.rb', line 38 def action_name @action_name end |
#arguments ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Command arguments
33 34 35 |
# File 'lib/cliqr/command/command_context.rb', line 33 def arguments @arguments end |
#base_command ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Base command name
23 24 25 |
# File 'lib/cliqr/command/command_context.rb', line 23 def base_command @base_command end |
#command ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Command name
28 29 30 |
# File 'lib/cliqr/command/command_context.rb', line 28 def command @command end |
#environment ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Environment type
43 44 45 |
# File 'lib/cliqr/command/command_context.rb', line 43 def environment @environment end |
Class Method Details
.build(config, parsed_input, options, &executor) ⇒ Cliqr::Command::CommandContext
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build a instance of command context based on the parsed set of arguments
53 54 55 |
# File 'lib/cliqr/command/command_context.rb', line 53 def self.build(config, parsed_input, , &executor) CommandContextBuilder.new(config, parsed_input, , executor).build end |
Instance Method Details
#action?(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check whether a action is valid in current context
108 109 110 |
# File 'lib/cliqr/command/command_context.rb', line 108 def action?(name) @config.action?(name) end |
#action_type? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the current context if for a action
113 114 115 |
# File 'lib/cliqr/command/command_context.rb', line 113 def action_type? @config.parent? end |
#ask(question, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run the [Sawaal] selector on a set of options
127 128 129 |
# File 'lib/cliqr/command/command_context.rb', line 127 def ask(question, ) Sawaal.select(question, ) end |
#bash? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if running in a bash environment
166 167 168 |
# File 'lib/cliqr/command/command_context.rb', line 166 def bash? @environment == :cli end |
#forward(args, options = {}) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Forward a command to the executor
120 121 122 |
# File 'lib/cliqr/command/command_context.rb', line 120 def forward(args, = {}) @executor.call(args, ) end |
#get_or_check_option(name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get option value or check if it exists
148 149 150 151 152 153 |
# File 'lib/cliqr/command/command_context.rb', line 148 def get_or_check_option(name) option_name = name.to_s.chomp('?') existence_check = name.to_s.end_with?('?') existence_check ? option?(option_name) : option(option_name) \ if @config.option?(option_name) end |
#invoke(event_name, *args) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Invoke an event
134 135 136 |
# File 'lib/cliqr/command/command_context.rb', line 134 def invoke(event_name, *args) @event_invoker.invoke(event_name, nil, *args) end |
#option(name) ⇒ Cliqr::Command::CommandOption
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get an option by name
89 90 91 92 |
# File 'lib/cliqr/command/command_context.rb', line 89 def option(name) return [name] if option?(name) default(name) end |
#option?(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if an option with a specified name has been passed
99 100 101 |
# File 'lib/cliqr/command/command_context.rb', line 99 def option?(name) .key?(name) end |
#options ⇒ Array<Cliqr::Command::CommandOption>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
List of parsed options
80 81 82 |
# File 'lib/cliqr/command/command_context.rb', line 80 def .values end |
#puts(message) ⇒ Nothing
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override the default puts implementation to enable output buffering
183 184 185 |
# File 'lib/cliqr/command/command_context.rb', line 183 def puts() @output_stream.write() end |
#root(environment_type = nil) ⇒ Cliqr::Command::CommandContext
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Transform this context to the root context
175 176 177 178 |
# File 'lib/cliqr/command/command_context.rb', line 175 def root(environment_type = nil) environment_type = @environment if environment_type.nil? CommandContext.new(@config.root, [], [], environment_type, @executor, @output_stream) end |