Class: HTTY::CLI::Command
- Inherits:
-
Object
- Object
- HTTY::CLI::Command
- 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
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the arguments provided to the command.
-
#session ⇒ Object
readonly
Returns the session within which the command operates.
Class Method Summary collapse
-
.alias_for ⇒ Object
Returns a command that the command is an alias for, otherwise
nil
. -
.aliases ⇒ Object
Returns command classes that are aliases for the command.
-
.build_for(command_line, attributes = {}) ⇒ Object
Returns a new HTTY::CLI::Commands::Command if the specified command_line references it, otherwise
nil
. -
.category ⇒ Object
Returns the name of a category under which help for the command should appear.
-
.command_line ⇒ Object
Returns the string used to invoke the command from the command line.
-
.command_line_arguments ⇒ Object
Returns the arguments for the command-line usage of the command.
-
.complete_for?(command_line) ⇒ Boolean
Returns
true
if the specified command_line can be autocompleted to the command. -
.help ⇒ Object
Returns the help text for the command.
-
.help_extended ⇒ Object
Returns the extended help text for the get command.
-
.notify_if_cookies_cleared(request) ⇒ Object
protected
Displays a notice if cookies are cleared on the specified request in the course of the block.
-
.raw_name ⇒ Object
Returns the full name of the command as it appears on the command line, without abbreviations.
-
.sanitize_arguments(arguments) ⇒ Object
Escape, split, chop, …
-
.see_also_commands ⇒ Object
Returns related command classes for the command.
Instance Method Summary collapse
-
#add_request_if_new ⇒ Object
protected
Yields the last request in #session.
-
#initialize(attributes = {}) ⇒ Command
constructor
Initializes a new HTTY::CLI::Command with attribute values specified in the attributes hash.
-
#perform ⇒ Object
Performs the command, or raises NotImplementedError.
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
181 182 183 184 |
# File 'lib/htty/cli/command.rb', line 181 def initialize(attributes={}) @arguments = Array(attributes[:arguments]) @session = attributes[:session] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the arguments provided to the command.
169 170 171 |
# File 'lib/htty/cli/command.rb', line 169 def arguments @arguments end |
#session ⇒ Object (readonly)
Returns the session within which the command operates.
172 173 174 |
# File 'lib/htty/cli/command.rb', line 172 def session @session end |
Class Method Details
.alias_for ⇒ Object
Returns a command that the command is an alias for, otherwise nil
.
11 |
# File 'lib/htty/cli/command.rb', line 11 def self.alias_for; end |
.aliases ⇒ Object
Returns command classes that are aliases for the command.
14 15 16 17 18 |
# File 'lib/htty/cli/command.rb', line 14 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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/htty/cli/command.rb', line 23 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 |
.category ⇒ Object
Returns the name of a category under which help for the command should appear.
40 41 42 43 |
# File 'lib/htty/cli/command.rb', line 40 def self.category return nil unless alias_for alias_for.category end |
.command_line ⇒ Object
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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/htty/cli/command.rb', line 48 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 { |abbrev, command_line| command_line }.collect do |command_line, abbrevs| abbrevs.sort_by { |command_line, abbrev| command_line }.first end my_abbrev = all_abbrevs.detect { |abbrev, command_line| command_line == my_command_line }.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_arguments ⇒ Object
Returns the arguments for the command-line usage of the command.
74 75 76 77 |
# File 'lib/htty/cli/command.rb', line 74 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.
81 82 83 84 |
# File 'lib/htty/cli/command.rb', line 81 def self.complete_for?(command_line) command_name = command_line_for_class_name(name) command_name[0...command_line.length] == command_line end |
.help ⇒ Object
Returns the help text for the command.
87 88 89 90 |
# File 'lib/htty/cli/command.rb', line 87 def self.help return "Alias for #{strong alias_for.command_line}" if alias_for "(Help for #{strong command_line} is not available)" end |
.help_extended ⇒ Object
Returns the extended help text for the get command.
93 94 95 96 |
# File 'lib/htty/cli/command.rb', line 93 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.
119 120 121 122 123 |
# File 'lib/htty/cli/command.rb', line 119 def self.(request) changed_request = yield puts notice('Cookies cleared') if request. && !changed_request. changed_request end |
.raw_name ⇒ Object
Returns the full name of the command as it appears on the command line, without abbreviations.
100 101 102 |
# File 'lib/htty/cli/command.rb', line 100 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
111 112 113 |
# File 'lib/htty/cli/command.rb', line 111 def self.sanitize_arguments(arguments) arguments end |
.see_also_commands ⇒ Object
Returns related command classes for the command.
105 106 107 |
# File 'lib/htty/cli/command.rb', line 105 def self.see_also_commands Array(alias_for) end |
Instance Method Details
#add_request_if_new ⇒ Object (protected)
Yields the last request in #session. If the block returns a different request, it is added to the requests of #session.
198 199 200 201 202 203 204 205 |
# File 'lib/htty/cli/command.rb', line 198 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 |
#perform ⇒ Object
Performs the command, or raises NotImplementedError.
187 188 189 190 191 192 |
# File 'lib/htty/cli/command.rb', line 187 def perform unless (alias_for = self.class.alias_for) raise NotImplementedError, 'not implemented yet' end alias_for.new(:arguments => arguments, :session => session).perform end |