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::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

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

Constructor Details

- (Command) initialize(attributes = {})

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

Valid attributes keys include:

  • :arguments

  • :session



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

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

Instance Attribute Details

- (Object) arguments (readonly)

Returns the arguments provided to the command.



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

def arguments
  @arguments
end

- (Object) session (readonly)

Returns the session within which the command operates.



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

def session
  @session
end

Class Method Details

+ (Object) alias_for

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

+ (Object) aliases

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

+ (Object) build_for(command_line, attributes = {})

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)))
    all_attributes = attributes
    if (arguments = match.captures[0])
      begin
        split_arguments = arguments.strip.shellsplit
      rescue ArgumentError
        return :unclosed_quote
      end
      return new(attributes.merge(:arguments => split_arguments))
    end
    return new(attributes)
  end
  nil
end

+ (Object) category

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

+ (Object) command_line

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

+ (Object) command_line_arguments

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

+ (Boolean) complete_for?(command_line)

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

+ (Object) help

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

+ (Object) help_extended

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

+ (Object) notify_if_cookies_cleared(request) (protected)

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



121
122
123
124
125
126
# File 'lib/htty/cli/command.rb', line 121

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

+ (Object) raw_name

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

+ (Object) see_also_commands

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

- (Object) add_request_if_new (protected)

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



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

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

- (Object) perform

Performs the command, or raises NotImplementedError.



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

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