Class: Telnet::ArgumentsParser
- Defined in:
- lib/telnet_client.rb,
lib/telnet/argument_parser.rb
Overview
The ArgumentsParser class
Constant Summary collapse
- IntegerPattern =
/^\d+$/.freeze
- Flags =
%i[banner ssl log_level help version].freeze
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.
423 424 425 426 427 |
# File 'lib/telnet_client.rb', line 423 def initialize(parser = OptionParser.new, = ::Telnet.client_config.dup) @parser = parser @options = 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.
421 422 423 |
# File 'lib/telnet_client.rb', line 421 def @options end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
421 422 423 |
# File 'lib/telnet_client.rb', line 421 def parser @parser end |
Instance Method Details
#banner ⇒ Object
429 430 431 432 433 |
# File 'lib/telnet_client.rb', line 429 def @parser. = "Usage: #{File.basename($PROGRAM_NAME)} [options] <hostname> [port]" @parser.separator '' @parser.separator 'Options:' end |
#help ⇒ Object
442 443 444 445 446 447 |
# File 'lib/telnet_client.rb', line 442 def help @parser.on_tail('-?', '--help', 'Show this message') do puts @parser exit end end |
#idle_reading ⇒ Object
49 50 51 52 53 |
# File 'lib/telnet/argument_parser.rb', line 49 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
55 56 57 58 59 |
# File 'lib/telnet/argument_parser.rb', line 55 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
435 436 437 438 439 440 |
# File 'lib/telnet_client.rb', line 435 def log_level @parser.on_tail('-v', '--verbose', 'Increase verbosity') do @options[:log_level] ||= 0 @options[:log_level] -= 1 end end |
#log_requests ⇒ Object
61 62 63 64 65 |
# File 'lib/telnet/argument_parser.rb', line 61 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
463 464 465 466 467 468 |
# File 'lib/telnet_client.rb', line 463 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
42 43 44 45 46 47 |
# File 'lib/telnet/argument_parser.rb', line 42 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
456 457 458 459 460 461 |
# File 'lib/telnet_client.rb', line 456 def validated_port(val) raise OptionParser::InvalidArgument, "Invalid port: #{val}" unless \ IntegerPattern.match?(val.to_s) && val.positive? && val < 65_536 val end |
#version ⇒ Object
449 450 451 452 453 454 |
# File 'lib/telnet_client.rb', line 449 def version @parser.on_tail('--version', 'Show version') do puts "#{$PROGRAM_NAME} version #{Version}" exit end end |