Class: HTTY::CLI

Inherits:
Object
  • Object
show all
Includes:
Display
Defined in:
lib/htty/cli.rb,
lib/htty/cli/command.rb,
lib/htty/cli/display.rb,
lib/htty/cli/commands.rb,
lib/htty/cli/commands/cd.rb,
lib/htty/cli/commands/put.rb,
lib/htty/cli/url_escaping.rb,
lib/htty/cli/commands/get.rb,
lib/htty/cli/commands/exit.rb,
lib/htty/cli/commands/post.rb,
lib/htty/cli/commands/form.rb,
lib/htty/cli/commands/undo.rb,
lib/htty/cli/commands/help.rb,
lib/htty/cli/commands/quit.rb,
lib/htty/cli/commands/patch.rb,
lib/htty/cli/commands/reuse.rb,
lib/htty/cli/commands/status.rb,
lib/htty/cli/commands/follow.rb,
lib/htty/cli/commands/delete.rb,
lib/htty/cli/commands/cookies.rb,
lib/htty/cli/commands/address.rb,
lib/htty/cli/commands/history.rb,
lib/htty/cli/commands/body_set.rb,
lib/htty/cli/commands/port_set.rb,
lib/htty/cli/commands/http_put.rb,
lib/htty/cli/body_open_command.rb,
lib/htty/cli/commands/http_get.rb,
lib/htty/cli/commands/path_set.rb,
lib/htty/cli/commands/form_add.rb,
lib/htty/cli/commands/host_set.rb,
lib/htty/cli/commands/http_post.rb,
lib/htty/cli/commands/http_head.rb,
lib/htty/cli/commands/query_set.rb,
lib/htty/cli/commands/query_add.rb,
lib/htty/cli/commands/body_clear.rb,
lib/htty/cli/commands/cookie_add.rb,
lib/htty/cli/commands/scheme_set.rb,
lib/htty/cli/commands/http_patch.rb,
lib/htty/cli/commands/header_set.rb,
lib/htty/cli/commands/body_unset.rb,
lib/htty/cli/commands/form_clear.rb,
lib/htty/cli/commands/http_trace.rb,
lib/htty/cli/http_method_command.rb,
lib/htty/cli/commands/cookies_add.rb,
lib/htty/cli/commands/http_delete.rb,
lib/htty/cli/commands/cookies_use.rb,
lib/htty/cli/commands/query_clear.rb,
lib/htty/cli/commands/form_remove.rb,
lib/htty/cli/commands/headers_set.rb,
lib/htty/cli/commands/query_unset.rb,
lib/htty/cli/commands/body_request.rb,
lib/htty/cli/commands/http_options.rb,
lib/htty/cli/commands/query_remove.rb,
lib/htty/cli/commands/header_unset.rb,
lib/htty/cli/commands/fragment_set.rb,
lib/htty/cli/commands/userinfo_set.rb,
lib/htty/cli/commands/cookie_remove.rb,
lib/htty/cli/commands/body_response.rb,
lib/htty/cli/commands/headers_unset.rb,
lib/htty/cli/commands/cookies_clear.rb,
lib/htty/cli/commands/headers_clear.rb,
lib/htty/cli/commands/userinfo_clear.rb,
lib/htty/cli/commands/cookies_remove.rb,
lib/htty/cli/commands/fragment_clear.rb,
lib/htty/cli/commands/userinfo_unset.rb,
lib/htty/cli/commands/fragment_unset.rb,
lib/htty/cli/commands/form_remove_all.rb,
lib/htty/cli/commands/headers_request.rb,
lib/htty/cli/commands/history_verbose.rb,
lib/htty/cli/commands/query_unset_all.rb,
lib/htty/cli/commands/ssl_verification.rb,
lib/htty/cli/commands/headers_response.rb,
lib/htty/cli/commands/headers_unset_all.rb,
lib/htty/cli/commands/body_request_open.rb,
lib/htty/cli/commands/cookies_remove_all.rb,
lib/htty/cli/commands/body_response_open.rb,
lib/htty/cli/commands/ssl_verification_on.rb,
lib/htty/cli/commands/ssl_verification_off.rb

Overview

Encapsulates the command-line interface to htty.

Defined Under Namespace

Modules: BodyOpenCommand, Commands, Display, HTTPMethodCommand, UrlEscaping Classes: Command

Constant Summary

Constant Summary

Constants included from Display

Display::FORMATS

Instance Attribute 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

- (CLI) initialize(command_line_arguments)

Instantiates a new HTTY::CLI with the specified command_line_arguments.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/htty/cli.rb', line 20

def initialize(command_line_arguments)
  if command_line_arguments.include?('--version')
    puts "v#{HTTY::VERSION}"
    exit
  end

  if command_line_arguments.include?('--help')
    HTTY::CLI::Commands::Help.new.perform
    exit
  end

  exit unless @session = rescuing_from(ArgumentError) do
    everything_but_options = command_line_arguments.reject do |a|
      a[0..0] == '-'
    end
    HTTY::Session.new(everything_but_options.first)
  end

  register_completion_proc
end

Instance Attribute Details

- (Object) session (readonly)

Returns the HTTY::Session created from command-line arguments.



17
18
19
# File 'lib/htty/cli.rb', line 17

def session
  @session
end

Instance Method Details

- (Object) run!

Takes over stdin, stdout, and stderr to expose #session to command-line interaction.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/htty/cli.rb', line 43

def run!
  say_hello

  catch :quit do
    loop do
      begin
        unless (command = prompt_for_command)
          $stderr.puts notice('Unrecognized command')
          puts notice('Try typing ' +
                      strong(HTTY::CLI::Commands::Help.command_line))
          next
        end

        if command == :unclosed_quote
          $stderr.puts notice('Unclosed quoted expression -- try again')
          next
        end

        if ARGV.include?('--debug')
          command.perform
        else
          rescuing_from Exception do
            command.perform
          end
        end
      rescue Interrupt
        puts
        throw :quit
      end
    end
  end

  say_goodbye
end