Class: Telnet::ArgumentsParser
- Defined in:
- lib/telnet_client.rb,
lib/telnet/argument_parser.rb
Overview
The ArgumentsParser class
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
- #banner ⇒ Object
- #help ⇒ Object
- #idle_reading ⇒ Object
- #idle_writing ⇒ Object
-
#initialize(option_parser = OptionParser.new) ⇒ ArgumentsParser
constructor
A new instance of ArgumentsParser.
- #log_level ⇒ Object
- #log_requests ⇒ Object
- #parse_positional_arguments! ⇒ Object
- #port ⇒ Object
- #validated_port(val, integer_pattern = /^\d+$/) ⇒ Object
- #version ⇒ Object
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, = ::Telnet.client_config.dup) @parser = parser @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
#options ⇒ Object (readonly)
Returns the value of attribute options.
423 424 425 |
# File 'lib/telnet_client.rb', line 423 def @options end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
423 424 425 |
# File 'lib/telnet_client.rb', line 423 def parser @parser end |
Instance Method Details
#banner ⇒ Object
432 433 434 435 436 |
# File 'lib/telnet_client.rb', line 432 def @parser. = "Usage: #{File.basename($PROGRAM_NAME)} [options] <hostname> [port]" @parser.separator '' @parser.separator 'Options:' end |
#help ⇒ Object
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_reading ⇒ Object
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_writing ⇒ Object
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_level ⇒ Object
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_requests ⇒ Object
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 |
#port ⇒ Object
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
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 |
#version ⇒ Object
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 |