Class: Cliqr::Command::CommandContext Private

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Object)

    Option’s 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_nameString

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

Returns:

  • (String)


38
39
40
# File 'lib/cliqr/command/command_context.rb', line 38

def action_name
  @action_name
end

#argumentsArray<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

Returns:

  • (Array<String>)

    List of arguments



33
34
35
# File 'lib/cliqr/command/command_context.rb', line 33

def arguments
  @arguments
end

#base_commandString

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

Returns:

  • (String)


23
24
25
# File 'lib/cliqr/command/command_context.rb', line 23

def base_command
  @base_command
end

#commandString

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

Returns:

  • (String)


28
29
30
# File 'lib/cliqr/command/command_context.rb', line 28

def command
  @command
end

#environmentString (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

Returns:

  • (String)


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

Parameters:

Returns:



53
54
55
# File 'lib/cliqr/command/command_context.rb', line 53

def self.build(config, parsed_input, options, &executor)
  CommandContextBuilder.new(config, parsed_input, options, 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

Parameters:

  • name (String)

    Name of the action to check

Returns:

  • (Boolean)

    true if this context has the requested action



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

Returns:

  • (Boolean)


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

Returns:

  • (Object)

    Selected key



127
128
129
# File 'lib/cliqr/command/command_context.rb', line 127

def ask(question, options)
  Sawaal.select(question, options)
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

Returns:

  • (Boolean)


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

Returns:

  • (Integer)

    Exit code



120
121
122
# File 'lib/cliqr/command/command_context.rb', line 120

def forward(args, options = {})
  @executor.call(args, options)
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

Returns:

  • (Object)


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

Returns:

  • (Boolean)

    true if the event was handled by any event handler



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

Parameters:

  • name (String)

    Name of the option

Returns:



89
90
91
92
# File 'lib/cliqr/command/command_context.rb', line 89

def option(name)
  return @options[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

Parameters:

  • name (String)

    Name of the option

Returns:

  • (Boolean)

    true if the option has a argument value



99
100
101
# File 'lib/cliqr/command/command_context.rb', line 99

def option?(name)
  @options.key?(name)
end

#optionsArray<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

Returns:



80
81
82
# File 'lib/cliqr/command/command_context.rb', line 80

def options
  @options.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

Returns:

  • (Nothing)


183
184
185
# File 'lib/cliqr/command/command_context.rb', line 183

def puts(message)
  @output_stream.write(message)
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

Parameters:

  • environment_type (Symbol) (defaults to: nil)

    Optional environment type

Returns:



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