Class: Faraday::CLI::Option::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/cli/option/parser.rb

Instance Method Summary collapse

Instance Method Details

#get_stdin_contentObject



105
106
107
# File 'lib/faraday/cli/option/parser.rb', line 105

def get_stdin_content
  ($stdin.tty? ? nil : $stdin.read)
end

#parse!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/faraday/cli/option/parser.rb', line 4

def parse!
  options = {}
  merge_defaults(options)
  OptionParser.new do |o|

    o.banner.concat(' <url>')

    o.on('-V', '--version', 'Show version number and quit') { $stdout.puts(Faraday::CLI::VERSION); exit }

    o.on('-X', '--request COMMAND', 'Specify http request command to use') do |http_method|
      options[:http_method]= http_method.to_s.strip.downcase
    end

    o.on('-H', '--header HEADER:VALUE', 'Pass custom header LINE to server (H)') do |header|
      options[:http_headers].push(header.split(/:\s*/))
    end

    o.on('-q', '--query key=value', 'Pass Query key values to use in the request') do |raw_query_pair|
      separator = '='

      parts = raw_query_pair.split(separator)
      query_key = parts.shift
      query_value = parts.join(separator)

      options[:params].push([query_key,query_value])
    end

    text = get_stdin_content
    options[:body]= text unless text.nil?
    o.on('-d', '--data PAYLOAD_STRING', 'HTTP POST data (H)') { |payload| options[:body]= payload }

    o.on('--upload_file KEY=FILE_PATH[:CONTENT_TYPE]', 'Pass File upload io in the request pointing to the given file') do |payload_file_path|

      options[:flags] << :multipart

      parts = payload_file_path.split('=')
      raw_file_path = parts.pop
      key = parts.first

      file_path, content_type = raw_file_path.split(':')
      content_type ||= 'application/octet-stream'

      file_stream_io = Faraday::UploadIO.new(File.realpath(file_path), content_type)

      options[:body] = {key => file_stream_io}

    end

    o.on('-A', '--user-agent STRING', 'Send User-Agent STRING to server (H)') do |user_agent|
      options[:http_headers] << ['User-Agent', user_agent]
    end

    o.on('-o', '--output FILE_PATH', 'Write to FILE instead of stdout') do |out_file_path|
      $stdout.reopen(out_file_path, 'a+')
      $stderr.reopen(out_file_path, 'a+')
    end

    # --proxy-anyauth  Pick "any" proxy authentication method (H)
    # --proxy-basic   Use Basic authentication on the proxy (H)
    # --proxy-digest  Use Digest authentication on the proxy (H)
    # --proxy-negotiate  Use HTTP Negotiate (SPNEGO) authentication on the proxy (H)
    # --proxy-ntlm    Use NTLM authentication on the proxy (H)
    # --proxy-service-name NAME  SPNEGO proxy service name
    # -U, --proxy-user USER[:PASSWORD]  Proxy user and password
    o.on('-x', '--proxy [PROTOCOL://]HOST[:PORT]  Use proxy on given port') do |proxy_url|
      options[:proxy_url]= proxy_url.to_s
    end

    o.on('-v', '--verbose', 'Make the operation more talkative') do
      options[:flags] << :verbose
    end

    o.on('--super-verbose', 'Make the operation even more talkative') do
      options[:flags] << :super_verbose
    end

    o.on('-s', '--silent', "Silent mode (don't output anything)") do
      options[:flags] << :silent
      $stdout.reopen('/dev/null', 'a+')
      $stderr.reopen('/dev/null', 'a+')
    end

    o.on('-K', '--config FILE_PATH', 'File path to the .faraday.rb if you want use other than default') do |file_path|
      options[:config_file_paths] << File.absolute_path(file_path)
    end

    o.on('-M', '--middlewares', 'Show current middleware stack') do
      options[:flags] << :show_middlewares
    end

    o.on('-W', '--without_middlewares', 'Make request without consuming middleware file(s)') do
      options[:flags] << :without_middlewares
    end

    o.parse!

  end

  options
end