Class: Telnet::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/telnet_client.rb,
lib/telnet/argument_parser.rb

Overview

The ArgumentsParser class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_parser = OptionParser.new) ⇒ ArgumentsParser

Returns a new instance of ArgumentsParser.



425
426
427
428
429
430
# File 'lib/telnet_client.rb', line 425

def initialize(parser = OptionParser.new, options = ::Telnet.client_config.dup)
  @parser = parser
  @options = options
  @flags = %i[banner ssl log_level help version]
  @flags.each { |method_name| method(method_name)&.call if respond_to?(method_name) }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



423
424
425
# File 'lib/telnet_client.rb', line 423

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



423
424
425
# File 'lib/telnet_client.rb', line 423

def parser
  @parser
end

Instance Method Details



432
433
434
435
436
# File 'lib/telnet_client.rb', line 432

def banner
  @parser.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] <hostname> [port]"
  @parser.separator ''
  @parser.separator 'Options:'
end

#helpObject



445
446
447
448
449
450
# File 'lib/telnet_client.rb', line 445

def help
  @parser.on_tail('-?', '--help', 'Show this message') do
    puts @parser
    exit
  end
end

#idle_readingObject



51
52
53
54
55
# File 'lib/telnet/argument_parser.rb', line 51

def idle_reading
  @parser.on('--idle-reading=seconds', 'Amount of time channel can idle without incoming data') do |v|
    @options[:idle_reading] = v.to_i
  end
end

#idle_writingObject



57
58
59
60
61
# File 'lib/telnet/argument_parser.rb', line 57

def idle_writing
  @parser.on('--idle-writing=seconds', 'Amount of time channel can idle without outgoing data') do |v|
    @options[:idle_writing] = v.to_i
  end
end

#log_levelObject



438
439
440
441
442
443
# File 'lib/telnet_client.rb', line 438

def log_level
  @parser.on_tail('-v', '--verbose', 'Increase verbosity') do
    @options[:log_level] ||= 0
    @options[:log_level] -= 1
  end
end

#log_requestsObject



63
64
65
66
67
# File 'lib/telnet/argument_parser.rb', line 63

def log_requests
  @parser.on('-r', '--log-requests', 'Include individual request info in log output') do
    @options[:log_requests] = true
  end
end

#parse_positional_arguments!Object



466
467
468
469
470
471
# File 'lib/telnet_client.rb', line 466

def parse_positional_arguments!
  @options[:host] = ARGV.shift or raise OptionParser::MissingArgument, 'hostname'
  return if (given_port = ARGV.shift&.to_i).nil?

  @options[:port] = @parser.validated_port(given_port).to_i
end

#portObject



44
45
46
47
48
49
# File 'lib/telnet/argument_parser.rb', line 44

def port
  description = "Port on which to listen for connections; default: #{@options[:port]}"
  @parser.on('-p', '--port=<port>', Integer, description) do |v|
    @options[:port] = validated_port(v).to_i
  end
end

#validated_port(val, integer_pattern = /^\d+$/) ⇒ Object

Raises:

  • (OptionParser::InvalidArgument)


459
460
461
462
463
464
# File 'lib/telnet_client.rb', line 459

def validated_port(val)
  raise OptionParser::InvalidArgument, "Invalid port: #{val}" unless \
    /^\d+$/.match?(val.to_s) && val.positive? && val < 65_536

  val
end

#versionObject



452
453
454
455
456
457
# File 'lib/telnet_client.rb', line 452

def version
  @parser.on_tail('--version', 'Show version') do
    puts "#{File.basename($PROGRAM_NAME)} version #{::TelnetClient::VERSION}"
    exit
  end
end