Class: Tojour::OptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tojour/opt_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ OptParser

Returns a new instance of OptParser.



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
# File 'lib/tojour/opt_parser.rb', line 7

def initialize(argv)
  options = {
    is_send_file: false,
    is_receive_file: false,
    is_input: false,
    is_output: false,
    is_list_file_clients: false,
    is_list: false,
    send_filename: nil,
    port: 9876,

    key_path: ENV['KEY_PATH'] || %[#{File.dirname(__FILE__)}/../../keys/server.key],
    csr_path: ENV['CSR_PATH'] || %[#{File.dirname(__FILE__)}/../../keys/server.csr],
    crt_path: ENV['CRT_PATH'] || %[#{File.dirname(__FILE__)}/../../keys/server.crt]
  }
  methods = [
    :send_file,
    :receive_file,
    :input,
    :output,
    :list
  ]
  optparse = OptionParser.new do |opts|
    opts.banner = 'USAGE: tojour [options] name'

    opts.on('-s FILE', '--send FILE', 'Transmit a FILE.') do |filename|
      options[:is_send_file] = true
      options[:send_filename] = filename
    end

    opts.on('-r', '--receive', 'Receive a file.') do |filename|
      options[:is_receive_file] = true
      options[:receive_filename] = filename
    end

    opts.on('-i', '--input', 'Receive input and output to STOUT.') do
      options[:is_input] = true
    end

    opts.on('-o', '--output', 'Transmit output from STDIN.') do
      options[:is_output] = true
    end

    opts.on('-l', '--list-clients', 'List clients.') do
      options[:is_list] = true
    end

    opts.on('-p PORT', '--port PORT', 'Port to wait on.') do |port|
      options[:port] = port
    end

    opts.on_tail('-h', '--help', 'Show this message.') do
      STDERR.puts optparse.help
      exit
    end
  end

  @options = options
  @argv = optparse.parse(argv)
  true_values = methods.select { |name| options[:"is_#{name}"] == true }
  @method = true_values.first.to_s.gsub(/^is_/, '').to_sym
  if true_values.count != 1
    STDERR.puts optparse.help
    exit
  end
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



5
6
7
# File 'lib/tojour/opt_parser.rb', line 5

def argv
  @argv
end

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/tojour/opt_parser.rb', line 5

def method
  @method
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/tojour/opt_parser.rb', line 5

def options
  @options
end