Class: Client::ArgumentsParser

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

Overview

The ArgumentsParser class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser = OptionParser.new, options = ::Client.client_config.dup) ⇒ ArgumentsParser

Returns a new instance of ArgumentsParser.



464
465
466
467
468
469
# File 'lib/client.rb', line 464

def initialize(parser = OptionParser.new, options = ::Client.client_config.dup)
  @parser = parser
  @options = options
  @flags = %i[banner port 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.



462
463
464
# File 'lib/client.rb', line 462

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



462
463
464
# File 'lib/client.rb', line 462

def parser
  @parser
end

Instance Method Details



471
472
473
474
475
# File 'lib/client.rb', line 471

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

#helpObject



511
512
513
514
515
516
# File 'lib/client.rb', line 511

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

#hostObject



477
478
479
480
481
482
# File 'lib/client.rb', line 477

def host
  description = "Connect to server at this host; default: #{@options[:host]}"
  @parser.on('-h', '--hostname=<hostname>', description) do |v|
    @options[:host] = v
  end
end

#log_levelObject



504
505
506
507
508
509
# File 'lib/client.rb', line 504

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

#parse_positional_arguments!Object



525
526
527
528
529
530
# File 'lib/client.rb', line 525

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

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

#portObject



491
492
493
494
495
496
# File 'lib/client.rb', line 491

def port
  description = "Connect to server at this port; default: #{@options[:port]}"
  @parser.on('-p', '--port=<port>', Integer, description) do |v|
    @options[:port] = validated_port(v).to_i
  end
end

#sslObject



498
499
500
501
502
# File 'lib/client.rb', line 498

def ssl
  @parser.on('--ssl', "Secure connection with TLSv1.3; default: #{@options[:ssl]}") do
    @options[:ssl] = true
  end
end

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

Raises:

  • (OptionParser::InvalidArgument)


484
485
486
487
488
489
# File 'lib/client.rb', line 484

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

  val
end

#versionObject



518
519
520
521
522
523
# File 'lib/client.rb', line 518

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