Class: HTTY::CLI::Command

Inherits:
Object
  • Object
show all
Extended by:
Display
Defined in:
lib/htty/cli/command.rb

Overview

A base class for all HTTY::CLI::Commands.

Direct Known Subclasses

HTTY::CLI::Commands::Address, HTTY::CLI::Commands::BodyClear, HTTY::CLI::Commands::BodyEdit, HTTY::CLI::Commands::BodyRequest, HTTY::CLI::Commands::BodyRequestOpen, HTTY::CLI::Commands::BodyResponse, HTTY::CLI::Commands::BodyResponseOpen, HTTY::CLI::Commands::BodySet, HTTY::CLI::Commands::BodyUnset, HTTY::CLI::Commands::Cd, HTTY::CLI::Commands::CookieAdd, HTTY::CLI::Commands::CookieRemove, HTTY::CLI::Commands::Cookies, HTTY::CLI::Commands::CookiesAdd, HTTY::CLI::Commands::CookiesClear, HTTY::CLI::Commands::CookiesRemove, HTTY::CLI::Commands::CookiesRemoveAll, HTTY::CLI::Commands::CookiesUse, HTTY::CLI::Commands::Delete, HTTY::CLI::Commands::Exit, HTTY::CLI::Commands::Follow, HTTY::CLI::Commands::Form, HTTY::CLI::Commands::FormAdd, HTTY::CLI::Commands::FormClear, HTTY::CLI::Commands::FormRemove, HTTY::CLI::Commands::FormRemoveAll, HTTY::CLI::Commands::FragmentClear, HTTY::CLI::Commands::FragmentSet, HTTY::CLI::Commands::FragmentUnset, HTTY::CLI::Commands::Get, HTTY::CLI::Commands::HeaderSet, HTTY::CLI::Commands::HeaderUnset, HTTY::CLI::Commands::HeadersClear, HTTY::CLI::Commands::HeadersRequest, HTTY::CLI::Commands::HeadersResponse, HTTY::CLI::Commands::HeadersSet, HTTY::CLI::Commands::HeadersUnset, HTTY::CLI::Commands::HeadersUnsetAll, HTTY::CLI::Commands::Help, HTTY::CLI::Commands::History, HTTY::CLI::Commands::HistoryVerbose, HTTY::CLI::Commands::HostSet, HTTY::CLI::Commands::HttpDelete, HTTY::CLI::Commands::HttpGet, HTTY::CLI::Commands::HttpHead, HTTY::CLI::Commands::HttpOptions, HTTY::CLI::Commands::HttpPatch, HTTY::CLI::Commands::HttpPost, HTTY::CLI::Commands::HttpPut, HTTY::CLI::Commands::HttpTrace, HTTY::CLI::Commands::Patch, HTTY::CLI::Commands::PathSet, HTTY::CLI::Commands::PortSet, HTTY::CLI::Commands::Post, HTTY::CLI::Commands::Put, HTTY::CLI::Commands::QueryAdd, HTTY::CLI::Commands::QueryClear, HTTY::CLI::Commands::QueryRemove, HTTY::CLI::Commands::QuerySet, HTTY::CLI::Commands::QueryUnset, HTTY::CLI::Commands::QueryUnsetAll, HTTY::CLI::Commands::Quit, HTTY::CLI::Commands::Reuse, HTTY::CLI::Commands::SchemeSet, HTTY::CLI::Commands::SslVerification, HTTY::CLI::Commands::SslVerificationOff, HTTY::CLI::Commands::SslVerificationOn, HTTY::CLI::Commands::Status, HTTY::CLI::Commands::Undo, HTTY::CLI::Commands::UserinfoClear, HTTY::CLI::Commands::UserinfoSet, HTTY::CLI::Commands::UserinfoUnset

Constant Summary

Constants included from Display

Display::FORMATS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Display

break, format, formatted_prompt_for, indent, logotype, normal, notice, pluralize, rescuing_from, say, say_goodbye, say_header, say_hello, show_headers, show_request, show_response, strong, word_wrap, word_wrap_indented

Constructor Details

#initialize(attributes = {}) ⇒ Command

Initializes a new HTTY::CLI::Command with attribute values specified in the attributes hash.

Valid attributes keys include:

  • :arguments

  • :session



189
190
191
192
# File 'lib/htty/cli/command.rb', line 189

def initialize(attributes={})
  @arguments = Array(attributes[:arguments])
  @session   = attributes[:session]
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the arguments provided to the command.



177
178
179
# File 'lib/htty/cli/command.rb', line 177

def arguments
  @arguments
end

#sessionObject (readonly)

Returns the session within which the command operates.



180
181
182
# File 'lib/htty/cli/command.rb', line 180

def session
  @session
end

Class Method Details

.alias_forObject

Returns a command that the command is an alias for, otherwise nil.



15
# File 'lib/htty/cli/command.rb', line 15

def self.alias_for; end

.aliasesObject

Returns command classes that are aliases for the command.



18
19
20
21
22
# File 'lib/htty/cli/command.rb', line 18

def self.aliases
  namespace_siblings.select do |s|
    s.alias_for == self
  end
end

.build_for(command_line, attributes = {}) ⇒ Object

Returns a new HTTY::CLI::Commands::Command if the specified command_line references it, otherwise nil. If an attributes hash is specified, it is used to initialize the command.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/htty/cli/command.rb', line 27

def self.build_for(command_line, attributes={})
  command_line_regexp = make_command_line_regexp
  if (match = (command_line_regexp.match(command_line)))
    begin
      if (arguments = match.captures[0])
        attributes = attributes.merge(
          :arguments => sanitize_arguments(arguments.strip.shellsplit)
        )
      end
    rescue ArgumentError
      :unclosed_quote
    else
      new(attributes)
    end
  end
end

.categoryObject

Returns the name of a category under which help for the command should appear.



46
47
48
49
# File 'lib/htty/cli/command.rb', line 46

def self.category
  return nil unless alias_for
  alias_for.category
end

.command_lineObject

Returns the string used to invoke the command from the command line. Its abbreviation is calculated to avoid collision with other commands in the same module.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/htty/cli/command.rb', line 54

def self.command_line
  my_command_line = command_line_for_class_name(name)
  other_command_lines = namespace_siblings.collect do |s|
    if s.method_defined?(:command_line)
      s.command_line
    else
      command_line_for_class_name s.name
    end
  end
  all_command_lines = [my_command_line] + other_command_lines
  all_abbrevs = Abbrev.abbrev(all_command_lines).group_by do |abbrev,
                                                              command_line|
    command_line
  end.collect do |command_line, abbrevs|
    abbrevs.sort_by do |command_line, abbrev|
      command_line
    end.first
  end
  my_abbrev = all_abbrevs.detect do |abbrev, command_line|
    command_line == my_command_line
  end.first
  my_abbrev_regexp = Regexp.new("^(#{Regexp.escape my_abbrev})(.*)$")
  my_command_line.gsub my_abbrev_regexp do
    $2.empty? ? $1 : "#{$1}[#{$2}]"
  end
end

.command_line_argumentsObject

Returns the arguments for the command-line usage of the command.



82
83
84
85
# File 'lib/htty/cli/command.rb', line 82

def self.command_line_arguments
  return alias_for.command_line_arguments if alias_for
  nil
end

.complete_for?(command_line) ⇒ Boolean

Returns true if the specified command_line can be autocompleted to the command.

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/htty/cli/command.rb', line 89

def self.complete_for?(command_line)
  command_name = command_line_for_class_name(name)
  command_name[0...command_line.length] == command_line
end

.helpObject

Returns the help text for the command.



95
96
97
98
# File 'lib/htty/cli/command.rb', line 95

def self.help
  return "Alias for #{strong alias_for.command_line}" if alias_for
  "(Help for #{strong command_line} is not available)"
end

.help_extendedObject

Returns the extended help text for the get command.



101
102
103
104
# File 'lib/htty/cli/command.rb', line 101

def self.help_extended
  return "(Extended help for #{command_line} is not available.)" unless help
  "#{help}."
end

.notify_if_cookies_cleared(request) ⇒ Object (protected)

Displays a notice if cookies are cleared on the specified request in the course of the block.



127
128
129
130
131
# File 'lib/htty/cli/command.rb', line 127

def self.notify_if_cookies_cleared(request)
  changed_request = yield
  puts notice('Cookies cleared') if request.cookies? && !changed_request.cookies?
  changed_request
end

.raw_nameObject

Returns the full name of the command as it appears on the command line, without abbreviations.



108
109
110
# File 'lib/htty/cli/command.rb', line 108

def self.raw_name
  command_line_for_class_name name
end

.sanitize_arguments(arguments) ⇒ Object

Escape, split, chop, … arguments before they are used to build the command



119
120
121
# File 'lib/htty/cli/command.rb', line 119

def self.sanitize_arguments(arguments)
  arguments
end

.see_also_commandsObject

Returns related command classes for the command.



113
114
115
# File 'lib/htty/cli/command.rb', line 113

def self.see_also_commands
  Array(alias_for)
end

Instance Method Details

#add_request_if_newObject (protected)

Yields the last request in #session. If the block returns a different request, it is added to the requests of #session.



206
207
208
209
210
211
212
213
# File 'lib/htty/cli/command.rb', line 206

def add_request_if_new
  last_request = session.requests.last
  maybe_next_request = yield(last_request)
  session.requests << maybe_next_request unless (
    maybe_next_request.nil? or maybe_next_request.equal?(last_request)
  )
  self
end

#performObject

Performs the command, or raises NotImplementedError.



195
196
197
198
199
200
# File 'lib/htty/cli/command.rb', line 195

def perform
  unless (alias_for = self.class.alias_for)
    raise NotImplementedError, 'not implemented yet'
  end
  alias_for.new(:arguments => arguments, :session => session).perform
end