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.



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

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.



460
461
462
# File 'lib/client.rb', line 460

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



460
461
462
# File 'lib/client.rb', line 460

def parser
  @parser
end

Instance Method Details



469
470
471
472
473
# File 'lib/client.rb', line 469

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

#helpObject



509
510
511
512
513
514
# File 'lib/client.rb', line 509

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

#hostObject



475
476
477
478
479
480
# File 'lib/client.rb', line 475

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



502
503
504
505
506
507
# File 'lib/client.rb', line 502

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



523
524
525
526
527
528
# File 'lib/client.rb', line 523

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



489
490
491
492
493
494
# File 'lib/client.rb', line 489

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



496
497
498
499
500
# File 'lib/client.rb', line 496

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)


482
483
484
485
486
487
# File 'lib/client.rb', line 482

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



516
517
518
519
520
521
# File 'lib/client.rb', line 516

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